3 ms·
The dmd D compiler goes even farther. Each string is given a name based on the hash of its contents. Then, the linker will coalesce all the strings in the progr
by WalterBright 4y ago
The dmd D compiler goes even farther. Each string is given a name based on the hash of its contents. Then, the linker will coalesce all the strings in the program with the same contents into the one string.
- vlovich123 4y agoThat’s true for C/C++ for clang and GCC if you use -fdata-sections for compiling and linking. -function-sections does a similar thing for duplicate functions. This doesn’t work across executable boundaries (ie shared libraries) for obvious reasons. Not sure if D’s approach makes that work.
- mmoskal 4y ago-fdata-sections can increase code size if you have lots of small global - the compiler doesn't know where they will be placed and has to generate pointer literals for all of them, in every function that use them (due to function sections). Without data sections it can generate one literal and then use relative addressing. I've been hitting that in embedded code with tight size constraints.
- vlovich123 4y agoDoesn’t that all get removed if you LTO?
- JonChesterfield 4y agoDoes D fold strings with a common suffix? I.e. "bar" to a pointer into "foobar"
- WalterBright 4y agoI've thought about doing it a few times, but it didn't seem there was much oil in that well.
- JonChesterfield 4y agoI vaguely remember it as a pattern in a dwarf string table but can't remember if the memory saving was worth the compile time. Thanks
- nneonneo 4y agoFWIW, GCC does implement this optimization; see https://godbolt.org/z/o81xa94v7 https://godbolt.org/z/o81xa94v7 for an example. I've definitely run across real-world samples where it saved some bytes (at the expense of making reverse-engineering a bit more annoying!).
- astrange 4y agoIt only happens if you check "compile to binary" there, so I think the linker is actually doing it.