Hi! Sorry if I chosen inproper category.
I am want to talk about constant slices deduplication. Consider this code (playground link ).
fn main() {
let arg = std::env::args().next().unwrap();
if arg == "Hello"{
println!("Hello");
}
else if arg == "World"{
println!("World");
}
else{
println!("Hello World!");
}
}
We have here three str
constants: "Hello World!"
, "World"
, "Hello"
. Having str reference be something like pair of pointers to the beginning and the end (or pointer to beginning or size), I think it is possible to deduplicate all these contants into the first.
So let be reference to first be (ptr1, ptr1+12), reference to second be (ptr1 + 6, ptr1 + 11) and reference to last be (ptr1, ptr1 + 5). It would allow result binaries to be more compact at least.
Currently I see that assembly has independed blocks for each value:
.L__unnamed_2:
.ascii "Hello"
.L__unnamed_11:
.ascii "Hello\n"
.L__unnamed_6:
.quad .L__unnamed_11
.asciz "\006\000\000\000\000\000\000"
.L__unnamed_5:
.L__unnamed_3:
.ascii "World"
.L__unnamed_12:
.ascii "World\n"
.L__unnamed_7:
.quad .L__unnamed_12
.asciz "\006\000\000\000\000\000\000"
.L__unnamed_13:
.ascii "Hello World!\n"
Is such kind of optimization desirable and possible?