Static Library Performance - Possible to inline calls?

点点圈 提交于 2019-12-10 11:28:44

问题


I've been trying to make an attempt to make a Game Engine, and I started out by making the Mathmatical foundation ( Vector, Matrix and Point classes. ) I would like to make a static library file (.lib) which I can then use in the rest of my Game Engine where it's needed.

What I was wondering is the following. Since most functions in the library need to be as fast as possible. I'd like to see the functions for example a Vector class being inlined. Can the compiler automatically do this? Or is some kind of hint needed?

I know I can put all the source in the header files but I think that's not really an elegant solution since some functions might be too large to be inlined. ( Header files wouldn't kind of be messy for the poeple using them. )

I hope you guys could help me out.

Christian


回答1:


Inlining of functions is only guaranteed to work with current compiler technology if the compileunit has visibility of the source code. Once you compile code into a library, it will not inline the function, no matter what the function does.

There is some "whole program optimization" schemes (available in at least MSVC and GCC, probably other compilers too) that produce a "object file" that is only part of the way to machine-code, so that some of the information about "what the source code wanted" is available to the final code-generation/linking stage to move code around, perform inlining and such, but this is only applicable when all source-files are available at the compile-time. Once something is made into final object code (which your static library is), it won't be making it into the "inliner" part.

Obviously, if you have classes, they will be in header files, so if you put functions suitable for inlining into the header file, the compiler will do the right thing.




回答2:


Yes, you can do this with link time optimization. If you generate a "fat" library, it would include both the compiled source and also an intermediate form that can be used to inline functions at link time (essentially, at link time a final compilation step is done, where everything is visible).

The technique is somewhat fragile since you need to ensure you are using the same flags at compile and link time and there are a few other restrictions: it wouldn't be a good way to publicly distribute a library. If it's for a controlled internal use though, it may work well.



来源:https://stackoverflow.com/questions/16384839/static-library-performance-possible-to-inline-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!