Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why is moc slow for some classes?

Why is moc slow for some classes?

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 6 Posters 4.6k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dan Princ
    wrote on last edited by Dan Princ
    #1

    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>;
    public:
        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>
    {
        Q_OBJECT
        using BaseClass = BaseT<Traits>;
    public:
        Table (A* a, B* b, C* c, D* d);
    protected:
        virtual Type getType() const override { return E_TYPE; }
        virtual B* getReader() const override;
        virtual void createDetails(const A* a, B* b) const override;
    };
    
    }
    #endif
    

    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.

    J.HilkJ 1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      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.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      0
      • D Dan Princ

        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>;
        public:
            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>
        {
            Q_OBJECT
            using BaseClass = BaseT<Traits>;
        public:
            Table (A* a, B* b, C* c, D* d);
        protected:
            virtual Type getType() const override { return E_TYPE; }
            virtual B* getReader() const override;
            virtual void createDetails(const A* a, B* b) const override;
        };
        
        }
        #endif
        

        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.

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @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.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        artwawA D 2 Replies Last reply
        0
        • J.HilkJ J.Hilk

          @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.

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @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.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          2
          • J.HilkJ J.Hilk

            @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.

            D Offline
            D Offline
            Dan Princ
            wrote on last edited by
            #5

            @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.

            artwawA 1 Reply Last reply
            0
            • D Dan Princ

              @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.

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #6

              @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.

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dan Princ
                wrote on last edited by
                #7

                Since I haven't figured out much more about this problem, I removed the Q_OBJECT macro from these classes. Not the best solution, but the only one I know of...

                mrjjM 1 Reply Last reply
                0
                • D Dan Princ

                  Since I haven't figured out much more about this problem, I removed the Q_OBJECT macro from these classes. Not the best solution, but the only one I know of...

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @Dan-Princ

                  Hi
                  moc does not expand templates. its not a compiler.

                  I tried a few combinations but I could not make moc hang/be slow on any files.

                  Do you have a concrete example that compiles and is known to take extra time?

                  D 1 Reply Last reply
                  2
                  • mrjjM mrjj

                    @Dan-Princ

                    Hi
                    moc does not expand templates. its not a compiler.

                    I tried a few combinations but I could not make moc hang/be slow on any files.

                    Do you have a concrete example that compiles and is known to take extra time?

                    D Offline
                    D Offline
                    Dan Princ
                    wrote on last edited by
                    #9

                    @mrjj Unfortunately not, I can't share the code that does it and it's not happening if I try to create some minimal example...

                    mrjjM jsulmJ 2 Replies Last reply
                    0
                    • D Dan Princ

                      @mrjj Unfortunately not, I can't share the code that does it and it's not happening if I try to create some minimal example...

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Dan-Princ
                      ok, i understand.

                      I did try with some class with lots of base classes but saw no effect.

                      Only by make extremely large .h file i could get time up but it sounds your case
                      is different.

                      1 Reply Last reply
                      0
                      • D Dan Princ

                        @mrjj Unfortunately not, I can't share the code that does it and it's not happening if I try to create some minimal example...

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Dan-Princ Just an idea: do you include many header files which include other header files and so on?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        D 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @Dan-Princ Just an idea: do you include many header files which include other header files and so on?

                          D Offline
                          D Offline
                          Dan Princ
                          wrote on last edited by Dan Princ
                          #12

                          @jsulm Well if I compile the file with /showIncludes, it prints ~3800 includes (mostly from libraries - 2600 from boost, 500 from Qt). If I run preprocessor on the header file, it has about 780000 lines. But I don't know how relevant it is as these numbers will be similar for most of the files I think...

                          kshegunovK 1 Reply Last reply
                          0
                          • D Dan Princ

                            @jsulm Well if I compile the file with /showIncludes, it prints ~3800 includes (mostly from libraries - 2600 from boost, 500 from Qt). If I run preprocessor on the header file, it has about 780000 lines. But I don't know how relevant it is as these numbers will be similar for most of the files I think...

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by
                            #13

                            @Dan-Princ said in Why is moc slow for some classes?:

                            • 2600 from boost

                            Boost is a template library (mostly) and as any template library is notorious for terrible compilation times, which is a consequence of how templates work.

                            it has about 780000 lines

                            And you have your answer. That's a huge file to parse.
                            And by the way, as @mrjj already said, you can't mix templates and QObject.

                            Read and abide by the Qt Code of Conduct

                            D 1 Reply Last reply
                            1
                            • kshegunovK kshegunov

                              @Dan-Princ said in Why is moc slow for some classes?:

                              • 2600 from boost

                              Boost is a template library (mostly) and as any template library is notorious for terrible compilation times, which is a consequence of how templates work.

                              it has about 780000 lines

                              And you have your answer. That's a huge file to parse.
                              And by the way, as @mrjj already said, you can't mix templates and QObject.

                              D Offline
                              D Offline
                              Dan Princ
                              wrote on last edited by
                              #14

                              @kshegunov That's a huge file to parse, true, but there are many other files that have similar (500000) number of lines after preprocessor and moc takes 0.6sec. So I'm not sure this is the main factor.

                              You can't put Q_OBJECT inside a template, but AFAIK you can have templated base class.

                              kshegunovK 1 Reply Last reply
                              0
                              • D Dan Princ

                                @kshegunov That's a huge file to parse, true, but there are many other files that have similar (500000) number of lines after preprocessor and moc takes 0.6sec. So I'm not sure this is the main factor.

                                You can't put Q_OBJECT inside a template, but AFAIK you can have templated base class.

                                kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on last edited by kshegunov
                                #15

                                @Dan-Princ said in Why is moc slow for some classes?:

                                You can't put Q_OBJECT inside a template, but AFAIK you can have templated base class.

                                Yes if it doesn't derive from QObject you can do something like:

                                class A : public QObject, public MyTemplatedClass< ... > //< QObject derived class always goes first!
                                {
                                };
                                

                                But even then it's rather clumsy. QObject doesn't play well with templates.

                                Read and abide by the Qt Code of Conduct

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved