9 ms·
There were two motivations for shared libraries; one no longer applies and the other, arguably was never reliable, so after 35 years of dynamic linking, a coupl
by gumby 7y ago
There were two motivations for shared libraries; one no longer applies and the other, arguably was never reliable, so after 35 years of dynamic linking, a couple of years ago I went static only (except for system calls of course).
The first reason was disk space: object files could simply be smaller. Nowadays, for all intents and purposes, disk space is free and unlimited.
The second was the ability to ship fixes to system facilities. These were fragile, leading to even more complex methods such as versioning, so though this was a good idea in principle, in practice it was very painful. I believe the Windows folks had their own name for it, "DLL Hell". The npm people still suffer from this from time to time, sometimes notoriously.
As for the fixes: software isn't updated on magtape any more, and most systems have robust upgrade systems. In addition there are all manner of quasi-hermetic isolation systems (VMs, docker images, venvs and the like) so why not just ship a static binary?
Plus with a static binary, if you really care about order of loading/unloading etc (which you ideally shouldn't) its trivial to manage with a custom linker script.
My only exception to this is the kernel: it can be upgraded, and mostly promises not to vary system call semantics too much, so it's OK to me not to link the kernel into my binary :-).
- jcranmer 7y ago> The first reason was disk space: object files could simply be smaller. Nowadays, for all intents and purposes, disk space is free and unlimited. It's not for disk space, it's runtime RAM occupation usage. The code for a .so is going to be slightly larger than the equivalent .a. At the very least, you'll need to burn extra space for the PLT and GOT. And PIC relocations are going to tend to burn more space than static linking relocations. The advantage of a .so you're talking about is that you can load a library shared by multiple processes at a fixed virtual address location that's the same for all processes, and thus you only burn the library once in RAM. For static libraries, the embedded addresses are going to be slightly different depending on their corresponding libraries, so even magic deduplication isn't going to be able to keep you from having the library in multiple places in physical memory.
- ThemalSpan 7y agoTo your first point: iOS takes this idea to its logical conclusion and it seems to be an important aspect of iOS’s performance. https://iphonedevwiki.net/index.php/Dyld_shared_cache https://iphonedevwiki.net/index.php/Dyld_shared_cache
- comex 7y agomacOS as well.
- loeg 7y agoicache, TLB entries, and even L1 are much more limited than main memory, especially on embedded devices. By linking everything into a single object, iOS can save on wasted padding and use superpages to map the shared library into each process in a TLB-efficient way.
- CamperBob2 7y agoRAM is free, too, at least as far as library code size is concerned.
- mikeash 7y agoA typical system has dozens or hundreds of processes running even when nothing is going on. If they all had their own copy of every system library in RAM, it wouldn’t look so free. Especially on mobile devices.
- zingermc 7y agoWhat if the OS could dedupe pages?
- kelnos 7y agoDo any OSes actually do this, though? If not, this is academic until one actually does. Linux does have something called "KSM" that de-dupes memory pages, but it appears that it's only so a hypervisor can de-dupe guest OS pages? Odd that it's not more general than that. Though I guess there is a cost: the KSM code needs to scan through memory to find duplicate pages, which I can't imagine is fast.
- pranjalv123 7y agoShared libraries also reduce RAM usage; multiple executables that use the same shared library can load it into memory just once.
- tntn 7y agoHow would i.e. OpenGL IHV drivers work as static libraries? No driver updates without a recompile? No driver switches (between vendors) without a recompile?
- namibj 7y agoRe-link, but yeah.
- derefr 7y agoShared libraries by themselves are not the problem here, so I’m not sure why you bring them up. The problem described in the article appears when you dynamically load/unload shared libraries. This only comes up in the context of plugin systems, or sometimes hot code reloading, and in those cases there is no alternative; there is nothing you can do with static binaries to emulate this behaviour.
- inlined 7y agoTo be fair, DLL Hell has a lot more issues than just versioning or registering the unload handler. First, there’s load order which can mean that you’re not actually loading the DLL you think you are. The second is WOW64 (Windows on Windows). Rather than using fat binaries like Linux and Mac OS, Microsoft decided to use a “brilliant” strategy where the file system lies to you in the “right” ways so that old 32-bit folders are moved to new addresses and the old 32-bit named golfers hold 64-bit binaries. This means that unless you use the “I really mean it” escape mechanisms (e.g. the “sysnative” directory) 32-bit binaries on 64-bit windows is actually loading DLLs from different paths than the code says.
- deleted 7y ago[deleted]
- ridiculous_fish 7y agoThis is crazy! If every app in the iOS or Android app store "just shipped a static binary", all apps would be enormous, and the OS vendors would be unable to evolve the system whatsoever.
- pjmlp 7y agoAlready now, the main reason users uninstall apps is to play disk space tetris.
- Nullabillity 7y agoThey already do, except for the slowly shrinking set of platform libraries.
- saagarjha 7y ago> the slowly shrinking set of platform libraries Not on iOS?
- ajross 7y ago> a couple of years ago I went static only You mean for your code, deployed on your systems. Yeah, that works fine. Now ship a binary to a client system with your static TLS implementation, JPEG decoder, LZ decompressor, whatever. And then go hire someone to watch the CVE feeds every day to know when you have to get them to install an upgrade. Of, if you don't want it to be your fault when they get pwned, you could just link against the system libraries and tell them to stay updated. "Static only" makes sense for web apps developed and deployed within the same organization, for embedded solutions where the whole update/upgrade process is known to be under the control of the developer, and... basically nowhere else. This advice is bad, sorry.
- Volt 7y agoNot the parent, but I think dynamic linking with system libraries is the only time it makes good sense. Otherwise you're just asking for trouble.
- saagarjha 7y agoOf course, you need to make sure that your system actually provides up-to-date implementations of these libraries.
- ajross 7y agoStill a much better bet than that you will.
- imtringued 7y agoThe GP argues that it should be abolished everywhere, even in the cases where it makes good sense.
- warmwaffles 7y agoDo you play any steam games or most other modern games? Yea those static compile almost everything.
- 7y ago
- quotemstr 7y agoI hack on the Android operating system. Without shared libraries, the system would fall over and die pretty much instantly, and Zygote helps only a little. Static linking the world is a luxury one can only afford in cornucopian backend environments where one can just throw money at resource limitations. On mobile, we have to work with the hardware we have. Economy is still important --- that's why I love it. Constraints breed creativity. * Speaking in my own capacity
- pjmlp 7y agoAs short info, Zygote is gone on Android Q.
- deleted 7y ago[deleted]
- loeg 7y agoI want to live in this world where disk space is free and unlimited, but it isn't and has never been. A primary goal that you don't really cover is conserving not just disk space, but memory. The disk space saved is a proxy for memory savings, which are (even more) valuable. Shared libraries linked in to multiple independent binaries can consume little to no additional memory due to the magic of virtual memory. The same does not hold true for static-linked libraries in multiple binaries.
- jchw 7y agoHonest question: have you ever ran into a situation where reducing binary sizes dramatically improved either performance or disk-space utilization? The reason I ask is, even as "bloated" as software is these days, I could fit ~100,000 fat 10 megabyte Go binaries on my SSD, without bothering to reach for compression. In practice, binaries on disk always pale in comparison to the amount of disk space I spend on media files, caches, backups and imaging, etc. Perhaps your concerns about virtual memory are true, but looking at top on a machine running Firefox with many tabs open, Discord (an Electron based application,) 3 GNOME Terminal windows, HexChat, Wine, and Telegram... I still don't crack 100 tasks. Still.* How about a server environment? Well, saving memory is certainly valuable on a server, but I would fail to imagine a scenario where you want so many different distinct binaries running that it would make a major difference in utilization. The shared memory savings that come from dynamically linked libraries are effectively removed when using Docker because each Docker container is going to have its own isolated system libraries and many of them probably won't be the same anyways, and yet I've never had any Kubernetes system where the majority of memory usage came from binary sizes. I suppose if every single system binary was statically linked, you would maybe be able to notice some kind of difference. But honestly, probably not, unless you were really searching. Usually disk usage of binaries is a much less important concern than CPU and RAM usage, especially across large fleets. *Though, that was only checking under my local user; it does turn out that there are a fair bit more tasks, but a good amount of them are actually kernel tasks. There's 400 counting kernel tasks, but only 191 not counting kernel tasks. Still not a very large number, in my opinion.
- kazinator 7y agoThat world is here. Though people are running out of space, same as ever, if you took away movies and videos, they would not know what to do with all that space.
- pjmlp 7y agoYou missed the third one, which is used a lot in desktop software and application servers, plugins and dynamic configuration on production. In name of stability, one may say to run external processes with shared memory instead, but now imagine the resource usage of something like InteliJ or Eclipse using only processes for their plugins.
- zimpenfish 7y agoWouldn't this be analogous to web browsers running each tab in its own process? I don't run IntelliJ or Eclipse - do people generally have more plugins than they'd have browser tabs?
- pjmlp 7y agoIt kind of is, but do you happen to have around 300 tabs or more open? And on an 8GB machine, which most business still use nowadays. This kind of desktop software, each feature can be its own plugin, meaning menu entries, UI widgets, integration with external tool, database drivers, …. Now imagine that to the extreme that every class could be a possible plugin, and even 300 will be a low bar. IDEs are only one exemple, there are plenty of use cases on desktop software, like music, graphics editing for two other common cases.
- de_watcher 7y agoPlugins have interactions. Browsers are VM managers. Their tabs are the tabs of the VM manager like a VirtManager for KVM or VmWare vSphere. Browsers boot a JS VM fast over HTTP and then communicate over the same HTTP.
- giomasce 7y agoThat is ok if you're just dealing with the upper layer of code, i.e., the final application. Then statically linking can very well be the best solution. But if you're building an operating system or distribution and do not want to recompile the whole of it (and let users download and reinstall the whole of it) each time you fix a bug in a core library, then static linking becomes pretty quickly a big pain. More or less the same happens with C++ header-only libraries, which are basically uncompiled static libraries. They are very nice and possibly allow strong optimization, but are rather painful to handle in a distribution. > Plus with a static binary, if you really care about order of loading/unloading etc (which you ideally shouldn't) its trivial to manage with a custom linker script. Why? It seems to me rather sensible to deinitialize resources in the opposite order in which you initialized them. This way the time span of different resources are contained each within the other instead of just overlap. When using RAII in C++ (which is basically just atexit on a finer grain) it often makes sense to have this requirement.
- choeger 7y agoIf you alawys write exactly one monolith, you are probably fine. But when you don't buy into the hype of "every service/app/executable in it's own language", you probably want to share some code between all your executables. Say the DB driver, the model, or even just a video codec. Without dynamic linking you duplicate all that stuff and, even worse, cannot be certain that all your executables use the same version.