From: Kristian Nielsen Date: June 30 2009 9:00am Subject: Re: MySql coding style: Request for deprecation of UPPERCASE typedefs List-Archive: http://lists.mysql.com/internals/37045 Message-Id: <87tz1yw0xs.fsf@knielsen-hq.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Tor Didriksen writes: > On Mon, 29 Jun 2009 12:55:45 +0200, Kristian Nielsen > wrote: >> Mats, could you describe what the benefits are of using forward >> declarations >> of structs/classes in place of #including the definition? > Please see > http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Header_File_Dependencies > for an explanation. > > If a class 'Bar' is implemented in terms of another class 'Foo', or > uses it's interface in some way, > it will typically store a pointer to it (in its private section). > By forward declaring the class > class Foo; > rather than > #include "foo.h" > you can often drastically cut down on the number of header files included. Ok, so to reduce compile time. > The savings in compile time can be quite substantial. > I remember doing this on a project sometime in the 90's cutting down > compile/build time from a day (yes 24 hours) to about one hour. If you expect this kind of improvements on today's computers you may be disappointed. CPUs are _much_ faster, C++ compilation can be parallelised across multiple cores (as opposed to eg. linking), and I suspect compilers these days spend proportionally less time on parsing #include compared to code generation. Just for fun, I did some quick benchmarking on building the latest mysql 5.1 bzr tree on a cheap 2-year old quad-core. First, compiling one file in sql/ vs. compiling all: `touch sql/mysqld.cc ; make -j5` -> 11.35 seconds, 121% CPU. `touch sql/*.cc ; make -j5` -> 120.87 seconds, 352% CPU. So that is a factor of only 10 between compiling one and all C++ sources. Of course that is still something if we can get down to only one (or a few) files #including something vs. all doing so. Second, I tried adding #include (random 100k bytes, 2.5k lines file) at the start of every .cc file in sql/, and timing `make -j5` after `make clean`: Without #include : 5 min 14 sec With #include : 5 min 17 sec So not much difference there (may even be less than the statistical noise). Anyway, thanks for the explanation, - Kristian.