How to reduce C + + code compilation time!

Time:2021-5-2

C++   The code includes two parts: header file and implementation file,   Header files are generally provided for others to use,   But once the header file changes, no matter how small, all the files that reference it must be recompiled, which takes time.

If you do a large project (such as the development of secondary packaging chrome), the time of recompiling will waste most of your working time. It’s very tiring to work all day,   But your boss said you didn’t produce, and you were fired,   Isn’t it very resentful. So

Let’s get down to business. How do you introduce compile time,   I know three ways:

▷   Delete unnecessary “include” as an alternative   Use forward declaration  ( forward   declared  )

▷   Delete a bunch of unnecessary private member variables and use them instead  ” impl”   method

▷   Remove unnecessary inheritance between classes

In order to clarify these three points, it is better to give an example.

Now suppose you find a new job and take over a class written by a former programmer, as follows:

//Old. H: This is the class you receive

     //

     #include 

     #include 

     #include 

     //The five are file, DB, CX, reduce or error, with limited level and no template class

     //Only file and CX have virtual functions

     #include "file.h"// class file

     #include "db.h"// class db

     #include "cx.h"// class cx

     #include "deduce.h"// class deduce

     #include "error.h"// class error

     classold : publicfile, privatedb {

     public:

          old( constcx& );

       db  get_db( int, char* );

       cx  get_cx( int, cx );

       cx& fun1( db );

       error  fun2( error );

       virtualstd::ostream& print( std::ostream& ) const;

     private:

       std::list cx_list_;

       deduce       deduce_d_;

     };

        inlinestd::ostream& operator<

 

This class is finished,   If you’ve seen where the problem is,   Don’t look at the next one,   You are a master,   These basic knowledge is too small for you. If you are asked in an interview, please continue to read it

First, let’s see how to use the first one   Delete unnecessary “include”

This class reference   5 header files,   That means that all the header files referenced by the five header files are also referenced,   actually,   No need to reference 5   Only 2 references are enough

1. Delete unnecessary “include” as an alternative   Use forward declaration  ( forward   declared  )

✪ delete header file   iostream,   I just started to learn C++   According to “C”++   primer》   Copy, as long as you see about input, output   iostream   Header file plus,   A few years have passed,   Now I know that’s not the case,   Here we just define the output function,   Just refer to ostream   That’s enough

✪   Ostream header file is not required,   Replace with   iosfwd  ,  Why?   The reason is,   Parameters and return types can be compiled as long as they are declared forward,   In iosfwd   In the file   678 line (my environment is vs2013. Different compilation environments may have different specific locations, but they all have this statement)   There is such a sentence

typedef basic_ostream > ostream;

inline std::ostream& operator<

 

Besides, if you say that this function operates ostream   Object,   That still needs to be included  ,   You’re only half right,   you bet,   This function operates on ostream   Object,   But look at his function implementation,

There is no such definition in it   std::ostream   os,   Such a statement, in other words, whenever there is such a definition statement,   You have to include   The corresponding header file is saved  , Because this is a request to the compiler to allocate space, and if only forward declaration   class   XXX; How does the compiler know how much space to allocate to this object!

See here,   The old. H header file can be updated as follows:

//Old. H: This is the class you receive

     //

     #Include // newly replaced header file

     #include 

     //The five are file, DB, CX, reduce or error, with limited level and no template class

     //Only file and CX have virtual functions

     #Include "file. H" // class file. As a base class, it cannot be deleted. If you delete it, the compiler will not know how much space to allocate when instantiating old objects

     #Include "dB. H" // class dB, as the base class, cannot be deleted, as above

     #include "cx.h"// class cx

     #include "deduce.h"// class deduce

     //Error is only used as a parameter and return value type, replacing # include "error. H" with a forward declaration 

     classerror;

     classold : publicfile, privatedb {

     public:

          old( constcx& );

       db  get_db( int, char* );

       cx  get_cx( int, cx );

       cx& fun1( db );

       error  fun2( error );

       virtualstd::ostream& print( std::ostream& ) const;

     private:

       std::list cx_ list_; //   CX is a template type, neither a function parameter type nor a function return value type, so the CX. H header file cannot be deleted

       deduce       deduce_ d_; //   Reduce is a type definition and does not delete its header file

     };

        inlinestd::ostream& operator<

 

up to now,   Removed some code,   Are you in a good mood? It’s said that you can see the level of a programmer,   It’s not how much code he wrote, but how much less code he wrote.

If you’re interested in C++   I have a deeper interest in programming,   You will still read the following text, and then delete the code further,   But this time we’re going to find another way

 

2. Delete a bunch of unnecessary private member variables and use them instead  ” impl”   method

✪use  ” impl”   The implementation mode is to write the code to reduce the compilation dependence of the client code

impl   The simple way is to put   Class private member variables are all put into an impl   Class,   Then, the private member variable of this class is reserved with only one impl*   Pointer,

The code is as follows:

// file old.h

     classold {

        //Public and protected members

       // public and protected members

     private:

     //Private members, as long as any one of the header files changes or the number of members increases or decreases, all clients that reference old. H must recompile

       // private members; whenever these change,

       // all client code must be recompiled

     };

Rewrite it as follows:

// file old.h

     classold {

     //Public and protected members

       // public and protected members

     private:

       classoldImpl* pimpl_;

       //Replace all the original private member variables with the impl pointer. The pointer can be compiled only by forward declaration. This writing method puts forward declaration and definition pointer together, which is completely OK.

       //Of course, it can be written separately

         // a pointer to a forward-declared class

     };

     // file old.cpp

     structoldImpl {

     //The real member variables are hidden here, changing at will, and the client code does not need to be recompiled

       // private members; fully hidden, can be

       // changed at will without recompiling clients

     };

The implementation of impl is as follows:

//Only file and CX have virtual functions

     #include "file.h"

     #include "db.h"

     classcx;

     classerror;

     classold : publicfile, privatedb {

     public:

          old( constcx& );

       db  get_db( int, char* );

       cx  get_cx( int, cx );

       cx& fun1( db );

       error  fun2( error );

       virtualstd::ostream& print( std::ostream& ) const;

     private:

classoldimpl* pimpl; // Forward declaration and definition here

      };

        inlinestd::ostream& operator< cx_list_;

deduce        dudece_d_;

};

 

 

3. Remove unnecessary inheritance between classes

Object oriented provides inheritance mechanism, but inheritance should not be abused,   old   class   It’s one of the abuses,   class   old   Inherit file   and   db   Class,   Inheriting file is public inheritance, inheriting dB   It’s private inheritance. It’s file   Understandably,   Because file   There are imaginary functions in,   old   To redefine it, but based on our assumptions,   Only file   and   cx   There are virtual functions, private inheritance dB   How to explain it?!   So the only possible reason is:

adopt   Private inheritance – let a class not be used as a base class to derive other classes, which is similar to the function of the final keyword in Java. However, from the example, it obviously has no such intention, so this private inheritance is not necessary at all. You should use the function provided by the DB class in the way of inclusion instead.

Delete the “dB. H” header file,   Put DB   The instance of can also be put into impl class. The final class is as follows:

//Only file and CX have virtual functions

     #include "file.h"

     classcx;

     classerror;

     classdb;

     classold : publicfile {

     public:

          old( constcx& );

       db  get_db( int, char* );

       cx   get_cx( int, cx );

       cx& fun1( db );

       error  fun2( error );

       virtualstd::ostream& print( std::ostream& ) const;

     private:

       classoldimpl* pimpl; // Forward declaration and definition here

      };

        inlinestd::ostream& operator< cx_list_;

deduce        dudece_d_;

};

 

 

To sum up:

This article simply introduces several ways to reduce compilation time

one   Delete unnecessary “include” as an alternative   Use forward declaration  ( forward   declared  )

two   Delete a bunch of unnecessary private member variables and use them instead  ” impl”   method

three   Remove unnecessary inheritance between classes

I hope these will help you!

 

Finally, whether you are a new professional or a beginner, you can also be advanced if you want to learn programming~

[worthy of attention] myC / C + + programming learning exchange club![Click to enter]

Q & A, learning and communication, technical discussion, and a large number of programming resources, zero basic video is also excellent~

 

Recommended Today

Vue basic introduction notes 04: V-for

Vue basic introduction notes 04 Four cases of V-for array Array object object number A key is required in a component <!DOCTYPE html> <html lang=”en”> <head> <meta charset=”UTF-8″> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> <meta http-equiv=”X-UA-Compatible” content=”ie=edge”> <title>Document</title> <script src=”https://cdn.jsdelivr.net/npm/vue”></script> </head> <body> <div id=”app”> <!– Array — > < p V-for = “(item, index) in list” > […]