Description
See https://github.com/sdroege/rust-staticlib-bloat-test for the code. This doesn't use cargo to make the actual compiler invocations clear (the same behaviour can be observed with cargo).
With Rust 1.69 and gcc 13.1.1 on Linux x86-64 the following can be observed:
unstripped | stripped (--strip-all ) |
|
---|---|---|
Rust executable (my_test ) |
4.1MB | 315kB |
Rust cdylib (libstaticlib.so ) |
4.0MB | 290kB |
gcc executable (my_staticlib_test ) |
5.2MB (+ 27%) | 1.1MB (+ 355%) |
gcc shared library (libmy_staticlib_test_dylib.so ) |
5.6MB (+ 40%) | 1.5MB (+ 517%) |
The actual size comes from the enabled overflow checks (-C debug-assertions=on
) , and thus all the panic and string formatting machinery that is included. However, this should be approximately the same code in either case so that doesn't explain the difference. (Disabling the overflow checks gives a more expected result because the function from the library compiles down to a few instructions) Also note that this compiles with -C panic=abort
so having the assertion in an extern "C"
function is not UB.
The question here now is what exactly the difference between both cases is, and how the staticlib
case can be improved. Maybe this requires changes in the compiler, maybe this just requires invoking the linker differently (in which case this would be useful to add to the documentation).