Reduce the number of headers transitively included by other headers #37344
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If we look at some profiling data of our build (using ClangBuildAnalyzer), we can see that most of the time is spent parsing source code:
One reason why we spend so much time parsing source code is that our headers transitively include many other headers, even if the included header is only tangentially relevant to the current header file. A good example is strv.h including hashmap.h because it has 4 functions that take a hashmap as input.
This results in the compiler having to do duplicated work for every single source file parsing the same headers over and over again, even if only a small amount of the included declarations are actually used.
This PR removes transitive includes from various core headers and splits of a few new headers to try and reduce the work the amount of declarations the compiler has to parse for each source file. The general idea is to replace macros and static inline functions with functions defined in the corresponding source file which means all the includes used to implement that function can be moved to the source file as well.
This gives us the following results after applying all these commits:
This work also improves the effectiveness of incremental compilation as when a header is modified, all files that (transitively) include it are recompiled, so by reducing the amount of (transitively) included headers, we reduce the amount of files that need to be recompiled when headers are changed as well.
Note that there's more work to be done. Specifically, stdlib.h is still transitively included into every source file and probably doesn't need to be.
The current list of expensive headers with all these commits applied is as follows: