Why is moc slow for some classes?

  • Hello,
    I'm currently in the process of optimizing our build time and I figured that moc takes a significant time of our build. While usually, it takes about 0.6 sec for moc to produce moc_* file, for some headers, it takes about 6-10 sec (we have hundreds of such files). Does anyone know what can cause moc to be so slow? Are there any good practices what to avoid in headers parsed by moc?

    The typical file that takes long looks like this:

    #ifndef GUARD
    #define GUARD
    namespace a
    class Reader : public BaseR<A, B, C>
        using BaseClass = BaseR<A, B, C>;
        explicit Reader(int);
        X read(const X& x) const;
        Y load() const;
    struct Traits
        using A = B;
        using C = D;
        using E = F;
        static const int G = 1;
        static const int ID = 2;
        static const bool B = true;
    class Table : public BaseT<Traits>
        using BaseClass = BaseT<Traits>;
        Table (A* a, B* b, C* c, D* d);
        virtual Type getType() const override { return E_TYPE; }
        virtual B* getReader() const override;
        virtual void createDetails(const A* a, B* b) const override;

    There are about 10 more base classes between BaseT and QObject. Can that be an issue?

    The headers are usually not longer than 50 lines. Can it be the templated base class that causes the problem? Does moc need to instantiate the template?

    Thanks for any help.

  • Hi, can you measure and exclude problems related to hard drive I/O? With hundreds of files (and system doing its stuff in the background) that might be the issue I would try to check first as a possible bottleneck.

  • @Dan-Princ
    not entierly sure, but it might help, to remove the Q_OBJECT Makro in classes where you don't define any new Signals & Slots.

  • @J.Hilk I would advice against. Documentation states:
    Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.

  • @artwaw I have not measured, but the files that are slow are all similar and there are a few thousands other files on the project, which mostly take about 0.5-0.6 sec to parse, so it seems unlikely to be caused by I/O. (and obviously this is reproducible, it's not one time measurement)

    @J.Hilk It is a possibility I'm considering, but we still use some other features of metaObject(), so it would require a little more effort.

  • @Dan-Princ Thanks for clarifying that. My next check would be internal includes, maybe there is a loop somewhere?
    Please note though that I never experienced that kind of problems before and perform a logical guess here.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.