bitcoin/bitcoin — pull request #2767 by brandondahler (2013-06-14 04:55:51+00:00)
Title: Cleanup code by using forward declarations and other methods...

... 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](http://en.wikipedia.org/wiki/Forward_declaration), 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](http://en.wikipedia.org/wiki/Value_%28computer_science%29).

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:
![Bitcoind Results](http://i.imgur.com/KNDcgBS.png)

[This](https://docs.google.com/spreadsheet/ccc?key=0AnpjY4_hVstbdHllazg4MG80V1UtUTVqM0hfTUE3bEE&usp=sharing) 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](https://gist.github.com/brandondahler/5771108).

### Method
* Go through each file, group each #include by type (see Order) and sort alphabetically
* Using [doxygen](http://www.stack.nl/~dimitri/doxygen/) with the [graphviz dots](http://www.graphviz.org/) 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

// Possible comments as first
// lines
#include "matching_file.h"

#include "local_file1.h"
#include "local_file2.h"

#include "file_in_other_dir1.h"
#include "file_in_other_dir2.h"

#include
#include

#ifdef SOMETHING1
#include
#endif

#include "json/a_file.h"
#include
#include

#ifdef SOMETHING2
#include
#endif

class ForwardDeclare1;
class ForwardDeclare2;

namespace SomeNamespace {
class ForwardDeclare3;
}

...

Example1 - No matching file, no file_in_other_dir, no ifdefs

// Possible comments as first line(s)

#include "local_file1.h"
#include "local_file2.h"

#include
#include

#include "json/a_file.h"
#include
#include

class ForwardDeclare1;
class ForwardDeclare2;

...