[Interest] Tricks to improve moc performance?

Kevin Funk kevin.funk at kdab.com
Fri Dec 6 10:16:31 CET 2019


On Friday, 6 December 2019 09:48:51 CET Giuseppe D'Angelo via Interest wrote:
> Il 06/12/19 09:42, Uwe Rathmann ha scritto:
> > Something you can try is to include the moc file at the end of your cpp
> > file. This can be done like this:
> > 
> > #include "moc_XYZ.cpp"
> > 
> > This type of construction is at least supported by qmake.
> > 
> > This does not reduce the number of moc runs, but it reduces the number
> > of compiler runs. As a rule of thumb for not too large classes I would
> > expect, that ~50% of the compile time is spent inside the Qt headers and
> > by including the moc file the overall compile time goes down by 33% as
> > the headers need to be processed only once.
> 
> This trick is used in several places in Qt itself (look for "includemoc"
> in commits). Not only it helps build times but also it produces slightly
> better code overall.

Heya,

On that note, CMake goes one step further and removes the need to do this 
manually. Using CMake's AUTOMOC feature, CMake will automatically create ONE 
mocs_compilations.cpp file per target which in turn includes all generated 
"moc_XYZ.cpp" files.

Quoting:

"Header files that are not included by an #include "moc_<basename>.cpp" 
statement are nonetheless scanned for Q_OBJECT or Q_GADGET macros. The 
resulting moc_<basename>.cpp files are generated in custom directories and 
automatically included in a generated <AUTOGEN_BUILD_DIR>/mocs_compilation.cpp 
file, which is compiled as part of the target."

... from: 
  https://cmake.org/cmake/help/v3.9/prop_tgt/AUTOMOC.html

Just as a side note. So if you thought about moving to CMake anyway this might 
be a good reason for a start. In general switching to e.g. CMake with the 
Ninja generator will give you better overall build times, esp. on Windows.

And you said:

> We probably don't strictly need the Q_OBJECT macro in some of those
> classes, but I would prefer not to start removing Q_OBJECT unless that's
> the last option.

I'd rethink that opinion: Adding the Q_OBJECT (and thus inheriting from 
QObject) everywhere adds the dependency to moc (=> slower compilation time) 
and has a runtime performance impact (QObject creation is not cheap!).

I'd consider only requiring Q_OBJECT where needed, it leads to overall better 
code.

Regards,
Kevin


> HTH,


-- 
Kevin Funk | kevin.funk at kdab.com | Senior Software Engineer
KDAB (Deutschland) GmbH, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt, C++ and OpenGL Experts
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5322 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20191206/4f3c700e/attachment.bin>


More information about the Interest mailing list