Why is a static libraries only a bag of object files, without any usual interdependent optimization or reordering?

StackOverflow https://stackoverflow.com/questions/8037683

Question

When linking a shared library (or at least a Windows DLL), lots of optimizations are possible, and all functions and classes are thrown together and reorganized for optimal performance (or so I would think/hope).

Why are static libraries only a bag of objects? Wouldn't it be better for the linker (both time and performance-wise) to have list of variables, functions, classes etc. that are contained within the object files, so when linking an executable or a shared object that uses this library can profit from an already performed optimization pass on interdependent things in the static library?

The static library creation would not take much longer (only a limited number of optimizations would be possible anyhow), but subsequent build steps using this library would be much faster/optimal.

PS: I'm mostly talking link-time-optimization here, but as all popular toolchains are proud to have this feature, I'm sure this question would pop up somehow? Please don't answer this question with: that's how it's always been, and or compatibility nobody ever thought to change it. That's not what I'm looking for...

Was it helpful?

Solution

There is a conflict between link time code generation and pre-link optimizations.

Assume from that library you call a function Foo only once with a fixed argument: Foo(12). LTCG now allows to reduce the Foo() implementation to that single case, invalidating all optimizations and call tree information you might previously have gathered.

With that in mind only the information that Foo() does not call Bar() can carry over. I don't see how that would significantly reduce link stage pressure, though.

Ordering information is similarly subject to profiler-guided optimizations: which functions are hot spots that need to be grouped together can only be determined by the final binary.


Generally it seems that optimization is moving towards the link stage, away from previous stages - and while the optimizations you suggest are feasible, it probably wouldn't pay off when you weight it against the amount of scenarios where this could be used and the additional complexity in code (roughly proportional to number of bugs)

OTHER TIPS

There are prototypes in those files. They probably act like the list you mentioned

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top