DISCLAIMER: these notes reflect my knowledge and understanding of subjects, not the ultimate truth. Most are written in process of learning things. Corrections are welcome


Runtime code sharing in Rust

📅 2025-12-22 📄 source

Are we dynamic linking shared libraries yet? Or should we?

Split from 2025-03-07_sdl3-image-viewer.html

Software is modular and modules are reused all the time, making it desirable to share them in runtime between programs depending on them for efficiency. However this brings problems. Every module evolves and can be built in many different ways, including version and build-time config choices; program developed against one concrete version of module can fail to work with other concrete version. Rust ecosystem seems to have almost entirely ditched runtime code sharing.

Note: in Rust world word "module" has different special meaning, but I use it here synonymically with "library"

Dependency substitution mechanisms can be classified by software lifecycle point at which concrete dep ver is picked, known in CS as dep binding time:

Common tech solutions for runtime code sharing seem to grow from that "C/Unix culture" from 70s. This includes "dynamic linking", "dynamic linker" and "dynamic library" concepts. It's strongly associated with "runtime binding" and opposed by many Rust devs because of associated reliability problems which are growing pain with growing complexity of software. And actually historical implementation is really not perfect for "build time binding" ( https://github.com/NixOS/nixpkgs/issues/24844 abs paths in ELF DT_NEEDED? https://lwn.net/Articles/961117/ ).

There are more issues with Rust runtime code sharing:

But how much efficiency benefit? Maybe it's not really worth? My 1st Rust project is image viewer which I moved from SDL_Image to image-rs; binary grew from 30kb to 18Mb. I believe this overhead is obviously mostly code of Rust image decoders (some of which it could share with most desktop apps, some with almost none) and std lib parts. And it doesn't even use Rust libs for UI at this point. Better analysis is needed for numeric estimates for desktop system with typical set of apps, but for now I tend to think that difference would be significant...

https://www.reddit.com/r/rust/comments/13vkut6/shared_libraries/

https://github.com/pop-os/cosmic-epoch/issues/649 I wondered what if Cosmic desktop at least splits common code into some kind of "runtime". It doesn't

https://drewdevault.com/dynlib.html looks like typical "top few % get most (few % of dynamic libs are shared by most progs, libc obviously being the king), others get rest (most dynamic libs are actually not shared at all)", doesn't convince me that sharing is not worth. Yes, average benefit is very small, but average program is rarely executed, and those which run most of the time? Every web browser worker process getting dozens or hundreds Mb overhead?

Perhaps currently growing RAM shortage crisis will force community to reconsider...


Comments are not implemented, but you can create issue on GitHub or check existing ones

Return to index