Author Topic: GNU GCC Compiler is compiling REALLY slow.  (Read 33692 times)

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GNU GCC Compiler is compiling REALLY slow.
« Reply #30 on: October 09, 2005, 03:55:59 pm »
I would be very interested to learn how you do that.

According to the gcc docs, .pch are used if they
a) have the same filename (plus .pch)
b) were compiled using the same compiler version
c) were compiled with the very same compiler settings

I have tried using precompiled headers many times and never managed to squeeze even one second out of it.
In my understanding, selecting the commandline in the compiler log, copying that, and pasting it into a DOS window should produce a .pch with exactly the same compile options from a header file. It does indeed create .pch files, but it makes no difference in compile times.
A project which compiles in 38 +1/-1 seconds before, compiles in 38 +1/-1 seconds afterwards.

Is there a special magical incantation you need?
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline Urxae

  • Regular
  • ***
  • Posts: 376
Re: GNU GCC Compiler is compiling REALLY slow.
« Reply #31 on: October 09, 2005, 04:27:55 pm »
To start, it's .gch in gcc not .pch.
Off the top of my head you also didn't mention an important condition: the precompiled header must be #included before any C(++) symbols in a file. In practice this means it must be the first #include (or #included as the first thing in the first #include, or in its first #include, etc.) and you can't put anything before that #include but non-#include preprocessor directives.
Also, any #defines before the #include may not affect the precompiled header.

While posting i've looked up the exact conditions from the gcc 3.4.4 manual section on precompiled headers:
Quote
A precompiled header file can be used only when these conditions apply:

    * Only one precompiled header can be used in a particular compilation.
    * A precompiled header can't be used once the first C token is seen. You can have preprocessor directives before a precompiled header; you can even include a precompiled header from inside another header, so long as there are no C tokens before the #include.
    * The precompiled header file must be produced for the same language as the current compilation. You can't use a C precompiled header for a C++ compilation.
    * The precompiled header file must be produced by the same compiler version and configuration as the current compilation is using. The easiest way to guarantee this is to use the same compiler binary for creating and using precompiled headers.
    * Any macros defined before the precompiled header (including with -D) must either be defined in the same way as when the precompiled header was generated, or must not affect the precompiled header, which usually means that the they don't appear in the precompiled header at all.
    * Certain command-line options must be defined in the same way as when the precompiled header was generated. At present, it's not clear which options are safe to change and which are not; the safest choice is to use exactly the same options when generating and using the precompiled header.
(emphasis added)

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GNU GCC Compiler is compiling REALLY slow.
« Reply #32 on: October 09, 2005, 04:34:58 pm »
Right, I forgot to mention that C token condition.

The only thing that appears before #include is /* description of file */, and that is surely not considered a C token, is it?
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb
Re: GNU GCC Compiler is compiling REALLY slow.
« Reply #33 on: October 09, 2005, 07:12:43 pm »
I would be very interested to learn how you do that.

According to the gcc docs, .pch are used if they
a) have the same filename (plus .pch)
b) were compiled using the same compiler version
c) were compiled with the very same compiler settings

I have tried using precompiled headers many times and never managed to squeeze even one second out of it....

attached is a sample project, where you can look how the preceompiled headers with gcc (3.4.4) are working.
the sample it is an ordinary wxWidgets minimal program with a frame.
every source file includes precomp.h first, this file we'll precompile.

Remarks to the following steps:
  • 1.) first, i build the project as normal (without precompiled header precomp.h.gch),
    it takes 13sec on my old AMD 1800 (Windows XP, MinGW gcc 3.4.4)
  • 2.) building the precompiled header:
    (i played a while to automate this with CodeBlocks custum build commands,
    but i didn't find a general solution, so i propose for now the manual creation of the .gch)

    from the Build Log i took the commandline for compiling PrecomiledHeadersFrm.cpp and
    changed only the file arguments for input (-c) and output (-o) files
    from: -c PrecompiledHeadersFrm.cpp -o pch_test_d_dll\PrecompiledHeadersFrm.o
    ...to: -c precomp.h -o precomp.h.gch
  • 3.) generate the precompiled header file by running precomp.bat file
  • 4.) Rebuild the CodeBlocks project
    it takes 2-3sec now

;-)



Code: [Select]
How to use precompiled headers. // tiwag 051009

1.)
--------------------------------------------------------------------------------
Rebuild without precompiled headers:
--------------------------------------------------------------------------------
Project   : Precompiled Headers test
Compiler  : GNU GCC Compiler (called directly)
Directory : D:\cpp\_projects\Codeblocks\precompiled headers\test\
--------------------------------------------------------------------------------
Switching to target: Debug Dll
windres.exe -i PrecompiledHeadersApp.rc -J rc -o pch_test_d_dll\PrecompiledHeadersApp.res -O coff   -ID:\wx261\Include
mingw32-g++.exe   -Wall -g -pipe -mthreads -fno-pcc-struct-return -fno-rtti -fmessage-length=0 -ggdb -Winvalid-pch -D__GNUWIN32__ -D__WXMSW__ -D__WXDEBUG__ -DWXUSINGDLL -DWX_PRECOMP    -ID:\wx261\Include -ID:\wx261\lib\gcc_dll\mswd  -c PrecompiledHeadersFrm.cpp -o pch_test_d_dll\PrecompiledHeadersFrm.o
mingw32-g++.exe   -Wall -g -pipe -mthreads -fno-pcc-struct-return -fno-rtti -fmessage-length=0 -ggdb -Winvalid-pch -D__GNUWIN32__ -D__WXMSW__ -D__WXDEBUG__ -DWXUSINGDLL -DWX_PRECOMP    -ID:\wx261\Include -ID:\wx261\lib\gcc_dll\mswd  -c PrecompiledHeadersApp.cpp -o pch_test_d_dll\PrecompiledHeadersApp.o
mingw32-g++.exe   -LD:\wx261\lib\gcc_dll  -o pch_test_d_dll.exe pch_test_d_dll\PrecompiledHeadersFrm.o pch_test_d_dll\PrecompiledHeadersApp.o  pch_test_d_dll\PrecompiledHeadersApp.res    -mthreads    -lkernel32 -luser32 -lgdi32 -lcomctl32 -lcomdlg32 -lwinspool -lwinmm -lshell32 -lole32 -loleaut32 -luuid -lrpcrt4 -ladvapi32 -lwsock32 -lodbc32 -lwxmsw26d  -mwindows
Process terminated with status 0 (0 minutes, 13 seconds)
0 errors, 0 warnings
 

2.)
--------------------------------------------------------------------------------
make precomp.bat:
-----------------
take compiler call g++ string from Build Log and
change: -c PrecompiledHeadersFrm.cpp -o pch_test_d_dll\PrecompiledHeadersFrm.o
    to: -c precomp.h -o precomp.h.gch
--------------------------------------------------------------------------------
g++.exe -Wall -g -pipe -mthreads -fno-pcc-struct-return -fno-rtti -fmessage-length=0 -ggdb -Winvalid-pch -D__GNUWIN32__ -D__WXMSW__ -D__WXDEBUG__ -DWXUSINGDLL -DWX_PRECOMP    -ID:\wx261\Include -ID:\wx261\lib\gcc_dll\mswd -c precomp.h -o precomp.h.gch


3.)
--------------------------------------------------------------------------------
precompiled header for wx-widgets
--------------------------------------------------------------------------------
precomp.h

precomp.h.gch  43.463.029 bytes


4.)
--------------------------------------------------------------------------------
Rebuild with precompiled headers:
--------------------------------------------------------------------------------
Project   : Precompiled Headers test
Compiler  : GNU GCC Compiler (called directly)
Directory : D:\cpp\_projects\Codeblocks\precompiled headers\
--------------------------------------------------------------------------------
Switching to target: Debug Dll
windres.exe -i PrecompiledHeadersApp.rc -J rc -o pch_test_d_dll\PrecompiledHeadersApp.res -O coff   -ID:\wx261\Include
mingw32-g++.exe   -Wall -g -pipe -mthreads -fno-pcc-struct-return -fno-rtti -fmessage-length=0 -ggdb -Winvalid-pch -D__GNUWIN32__ -D__WXMSW__ -D__WXDEBUG__ -DWXUSINGDLL -DWX_PRECOMP    -ID:\wx261\Include -ID:\wx261\lib\gcc_dll\mswd  -c PrecompiledHeadersFrm.cpp -o pch_test_d_dll\PrecompiledHeadersFrm.o
mingw32-g++.exe   -Wall -g -pipe -mthreads -fno-pcc-struct-return -fno-rtti -fmessage-length=0 -ggdb -Winvalid-pch -D__GNUWIN32__ -D__WXMSW__ -D__WXDEBUG__ -DWXUSINGDLL -DWX_PRECOMP    -ID:\wx261\Include -ID:\wx261\lib\gcc_dll\mswd  -c PrecompiledHeadersApp.cpp -o pch_test_d_dll\PrecompiledHeadersApp.o
mingw32-g++.exe   -LD:\wx261\lib\gcc_dll  -o pch_test_d_dll.exe pch_test_d_dll\PrecompiledHeadersFrm.o pch_test_d_dll\PrecompiledHeadersApp.o  pch_test_d_dll\PrecompiledHeadersApp.res    -mthreads    -lkernel32 -luser32 -lgdi32 -lcomctl32 -lcomdlg32 -lwinspool -lwinmm -lshell32 -lole32 -loleaut32 -luuid -lrpcrt4 -ladvapi32 -lwsock32 -lodbc32 -lwxmsw26d  -mwindows
Process terminated with status 0 (0 minutes, 2 seconds)
0 errors, 0 warnings





[attachment deleted by admin]

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GNU GCC Compiler is compiling REALLY slow.
« Reply #34 on: October 09, 2005, 10:59:08 pm »
Thanks, will be first thing I try tomorrow morning :)
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb
Re: GNU GCC Compiler is compiling REALLY slow.
« Reply #35 on: October 10, 2005, 01:34:19 pm »
Thanks, will be first thing I try tomorrow morning :)
did it work for you ?

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GNU GCC Compiler is compiling REALLY slow.
« Reply #36 on: October 10, 2005, 01:54:06 pm »
Before: "0 minutes 5 seconds", after: "0 minutes 0 seconds".
Hmm.... unquestionable. So now I have to figure what I am doing wrong.

The only two differences I see are you use // instead of /**/ for the top comment and you include everything via one file. Will try either of these and see what it does. Thank you :)
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb
Re: GNU GCC Compiler is compiling REALLY slow.
« Reply #37 on: October 10, 2005, 02:06:30 pm »
Before: "0 minutes 5 seconds", after: "0 minutes 0 seconds".
Hmm.... unquestionable. So now I have to figure what I am doing wrong.
probably nothing :)

Quote from: thomas
The only two differences I see are you use // instead of /**/ for the top comment
forget that, the lines should be deleted, they are commented because i tried a while and several ways until i got it working...

Quote from: thomas
you include everything via one file.
That's the important one, only one header-file (in this case precomp.h)can be pre-compiled! of course there can be added more headers if your project grows and needs additional headers...


Did you manage to produce the precomp.h.gch ? (size about 43 MB)
After this file exists in the same directory where precomp.h also resides, you can recompiling your project using the precompiled header-file.

It is important to use the -Winvalid-pch  compiler option in order to get information if the precompiled header could be used or not.
If there are warnings regarding the precompiled header,
NOT ALL compiler options were the same when pre-compiling the header-file as they are now at the time of compiling.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GNU GCC Compiler is compiling REALLY slow.
« Reply #38 on: October 10, 2005, 02:30:02 pm »
Got it working now, thanks :) It was indeed the "only one header file" thing.

Now it takes 5 seconds instead of 20 seconds compiling cb::svn. 75% saved :)
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb
Re: GNU GCC Compiler is compiling REALLY slow.
« Reply #39 on: October 10, 2005, 02:35:41 pm »
Quote from: thomas
Got it working now...