Repository navigation
Reduce the number of headers transitively included by other headers - #37344
Closed
daandemeyer wants to merge 14 commits into
Closed
daandemeyer wants to merge 14 commits into
daandemeyer wants to merge 14 commits into
Conversation
This was referenced May 21, 2025
Merged
Split out of systemd#37344.
Split out of systemd#37344.
For libsystemd's headers, the changes made depend on whether the header is installed or not. For installed headers, the only change made is that commonly included headers were moved to _sd-common.h. For these headers, there should be no noticeable change in behavior when including them. For non-installed headers, includes were replaced with forward declarations where possible as usual. Split out of systemd#37344.
Split out of systemd#37344.
If we don't use any symbols from a header, let's not include it.
Let's make things more consistent and have all dlopen_xxx() functions return EOPNOTSUPP on failure and verify this behavior in test-dlopen-so.
Let's put it together with the macro that needs it (mfree()). Also, get rid of the unnecessary <stdlib.h> include in iovec-util-fundamental.h.
We want the relevant code to be compiled so that it can be analyzed by clang-tidy.
We're very limited in our ability to change these due to backwards compat, so let's skip them from analysis since we won't be able to fix the errors anyway.
We disable the missing includes check so we only get errors on unused includes for now.
Now that the entire free doesn't trigger any clang unused include violations anymore, let's re-enable the clangd option as it's much more useful now that the tree is clean.
This was referenced May 24, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: