Why not mark everything inline?

后端 未结 11 646
长发绾君心
长发绾君心 2020-12-08 13:38

First off, I am not looking for a way to force the compiler to inline the implementation of every function.

To reduce the level of misguided answers make s

11条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 13:51

    It is done already in some cases. It is very similar to the idea of unity builds, and the advantages and disadvantages are not fa from what you descibe:

    • more potential for the compiler to optimize
    • link time basically goes away (if everything is in a single translation unit, there is nothing to link, really)
    • compile time goes, well, one way or the other. Incremental builds become impossible, as you mentioned. On the other hand, a complete build is going to be faster than it would be otherwise (as every line of code is compiled exactly once. In a regular build, code in headers ends up being compiled in every translation unit where the header is included)

    But in cases where you already have a lot of header-only code (for example if you use a lot of Boost), it might be a very worthwhile optimization, both in terms of build time and executable performance.

    As always though, when performance is involved, it depends. It's not a bad idea, but it's not universally applicable either.

    As far as buld time goes, you have basically two ways to optimize it:

    • minimize the number of translation units (so your headers are included in fewer places), or
    • minimize the amount of code in headers (so that the cost of including a header in multiple translation units decreases)

    C code typically takes the second option, pretty much to its extreme: almost nothing apart from forward declarations and macros are kept in headers. C++ often lies around the middle, which is where you get the worst possible total build time (but PCH's and/or incremental builds may shave some time off it again), but going further in the other direction, minimizing the number of translation units can really do wonders for the total build time.

提交回复
热议问题