C++ class with template methods having many instances consuming huge amount of memory on compilation

182 views Asked by At

I have a class with template methods for which I instantiate many other classes, like more than a hundred. The problem is that the compilation of the template class consumes a huge amount of memory, like 3GB. I assume this occurs because of many template instances. Just for detailing, the instantiated classes are Qt and QxOrm objects.

Has someone else had this problem too?

Some suggestions about how could I reduce the memory consumption?

Here are some parts of the code:

//The class with template methods

class SGSRequestHandler : public HttpRequestHandler
{
    public:
        SGSRequestHandler(QObject* parent = 0);
        virtual ~SGSRequestHandler();

        template<class T>
        ResponseInfo processDatabase(ODataRequestInfo<T>& odata, qx::QxSession& session) {...}

        template<class T>
        ResponseInfo httpGet(ODataRequestInfo<T> &odata, qx::QxSession& session) {...}
    ...
   }

Here is a example of what I do with the template class:

else if (className == "class1")
     rInfo = process<Class1>(request, session);
else if (className == "class2")
     rInfo = process<Class2>(request, session);
...
else if (className == "class100")
    rInfo = process<Class100>(request, session);
3

There are 3 answers

0
vanderdill On BEST ANSWER

I've came with a solution. Not even close from elegant, but resolved my problem.

I simply split the class that instantiate all the template in three other classes. So, the memory consumption on compilation is a third part for each of the classes.

Remembering that I need to work with templates because I'm using QxOrm library, otherelse I could do this in a much simplier manner with polimorfism.

//processorr.h

class ProcessorR {
    void process(const QString& className) {
        if(className == "rClass1")
            rInfo = process<rClass1>(request, session);
        else if(className == "rClass2")
            rInfo = process<rClass2>(request, session);
        /// And so on
    }
}

//processort.h

class ProcessorT {

    void process(const QString& className) {
        if(className == "tClass1")
           rInfo = process<tClass1>(request, session);
        else if(className == "tClass2")
           rInfo = process<tClass1>(request, session);
        /// And so on
    }
}

//And then in usage:

if(className.startsWith("t"))
{
    ProcessorT processor;
    processor.process(className);
}
else if(className.startsWith("r"))
{
    ProcessorR processor;
    processor.process(className);
}
else if(className.startsWith("u"))
{
    ProcessorU processor;
    processor.process(className);
}
1
MSalters On

The easiest solution may very well be to use extern template and move the 100 instantiations to multiple .cpp files.

1
Tom Tanner On

Looking at your solution suggests that really templates are the wrong answer to your problem.

Maybe you should consider making all the processing class instances register with the SGSHandler class, so that that can just do

processors_[name].process(request, session);

There is obviously a runtime cost to this but it's a lot easier to maintain