5 ms·
This would be true if the entirety of the library was used by the application. Wouldn't the linker throw out anything unused?
by ryandrake 9y ago
This would be true if the entirety of the library was used by the application. Wouldn't the linker throw out anything unused?
- teamhappy 9y ago> Wouldn't the linker throw out anything unused? There (usually) is no dead code elimination for libraries.
- mikeash 9y agoDepends on the language. If you're using something like C++, you can probably remove a lot of dead code. But with more dynamic languages, like Objective-C or Ruby or JavaScript, it becomes difficult or impossible to prove that a given chunk of code is never used. In Objective-C (which I'm most familiar with), the linker will keep all Objective-C classes and methods because it has no idea if you might be looking things up by name at runtime and invoking them that way. Even if you can throw away dead code, you can run into bloat because a library has a massive set of foundational APIs that the rest is built on, and using one little feature of the library ends up bringing in half the library code because it's used everywhere.
- Retric 9y agoYou could use static analysis to find out if anything calls objects dynamically by name at run time which could be a useful optimization. To be safe this would be one bool value for the entire code base, but with some care it would help. A larger issue is size is simply not considered a significant issue.
- mikeash 9y agoWith Objective-C, that will always come back "true." Even if you never use dynamic lookups, Apple's frameworks do. I suspect the same is true of other languages.
- Retric 9y agoInteresting, I find that surprising due to the overhead involved. In most languages calling functions via string names is very expensive. PS: Do you have an example?
- mikeash 9y agoNibs and storyboards are full of this stuff. They instantiate classes by name, call methods by name, etc. Some example documentation if you're curious: https://developer.apple.com/library/content/documentation/General/Conceptual/Devpedia-CocoaApp/TargetAction.html https://developer.apple.com/library/content/documentation/Ge... https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/ https://developer.apple.com/library/content/documentation/Co... https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4 https://developer.apple.com/library/content/documentation/Co... Objective-C's dispatch is built around looking up methods by their selector, which is effectively an interned name. Looking up the selector can be slow, but once you have one, invoking a method using a dynamic selector is as fast as invoking one with a selector that's known at compile time.
- saagarjha 9y ago> Interesting, I find that surprising due to the overhead involved. Due to the frequency of this, objc_msgSend (which handles dynamic method calls) is hand-written in assembly, with caching and "fast paths" to improve speed. The overhead can usually be brought down to that of a virtual function call in C++.
- Retric 9y agoThat's like saying shooting yourself in the foot only hurts the first time. But, it's probably not a major performance issue in practice.
- mikeash 9y agoThat's the thing about performance. If you do something a million times, it's usually OK if the first time takes a thousand times longer than the fast case, as long as subsequent times are fast. Look at how much code is written in Ruby and Python. Their method dispatches are way slower. To put it in perspective, it takes CPython about an order of magnitude longer to add two numbers together than it takes Objective-C to do a dynamic method dispatch.
- maxxxxx 9y agoA lot of modern runtimes don't have linkers. With Java, C# and JavaScript you get the whole library. There is no elimination of unused code.
- slaymaker1907 9y agoJava as of Java 9 does have a linker.
- msbarnett 9y agoDepends on if you're statically or dynamically linking. When statically linking the final binary shouldn't contained unused functions from external libraries.