… of avoiding unnecessary header includes. Additionally alphabetizes and groups includes and forward declarations.
Note: there are a large amount of changes that have been rebased onto the most recent master. I did my best to ensure that no code was functionally changed; however, a code review of all my changes would be advised.
Theory
By using forward declarations, compilation speed can be increased in both incremental and full builds.
Forward declarations suffice over having the full declaration in cases where the type that is being forward declared is only used as a reference or being pointed to, and the usage is not an lvalue.
By making these modifications, the total number of header files needed to compile any .cpp file is reduced. Fewer header files being included reduces the total amount of code that needs to be compiled for each .cpp file.
Quantitative Results
How much time does this actually account for?
For bitcoind:
This spreadsheet shows detailed results for each of the different builds (note that there are multiple sheets in the spreadsheet), along with the interesting stats and graph for each.
This data was generated automatically via the scripts here.
Method
- Go through each file, group each #include by type (see Order) and sort alphabetically
- Using doxygen with the graphviz dots extension
- Go through each header, comment each #include individually and all headers that indirectly include the commented one
- See if it can build with the #include commented, analyze errors to understand if they can actually be solved with forward declares
- Remove, keep, or add a direct header to the required type, as needed.
Order
Most Generic
0// License text as first
1// lines
2
3#if defined(HAVE_CONFIG_H)
4#include "bitcoin-config.h"
5#endif
6
7#include "matching_file.h"
8
9#include "local_file1.h"
10#include "local_file2.h"
11
12#include "file_in_other_dir1.h"
13#include "file_in_other_dir2.h"
14
15#include <system1.h>
16#include <system2.h>
17
18#ifdef SOMETHING1
19#include <system3.h>
20#endif
21
22#include "json/a_file.h"
23#include <library1.h>
24#include <library2.h>
25
26#ifdef SOMETHING2
27#include <library3.h>
28#endif
29
30class ForwardDeclare1;
31class ForwardDeclare2;
32
33namespace SomeNamespace {
34 class ForwardDeclare3;
35}
36
37...
Example1 - No matching file, no file_in_other_dir, no ifdefs
0// License text as first
1// lines
2
3#include "local_file1.h"
4#include "local_file2.h"
5
6#include <system1.h>
7#include <system2.h>
8
9#include "json/a_file.h"
10#include <library1.h>
11#include <library2.h>
12
13class ForwardDeclare1;
14class ForwardDeclare2;
15
16...