0

I have a generated class, that provides access to a database and maps the field values for each row with the field names for that column. We use it to avoid typing mistakes in the queries and write code like for(auto& entity: db.table1) { doSomething(entity.field1()); }.

However, each CPP file that includes this header starts to compile slow, generates a big object file (~14 MB) and causes very long linker times (GCC on Windows).

Here's a very shortened version of this class structure. Is there any way to speed up my linker times?

// MyDatabase.hxx
#include <vector>
#include <memory>
// this template is a generic header
template <class TableData> class Table {
public:
    TableData getFirstRowFromDatabase() {return m_rows.begin();}
    // following many, very complex member functions
    std::vector<TableData> m_rows;
};

// this class declaration is generated by a program by parsing the SQL schema
class MyDatabase {
public:
    // Data structure holding the fields of a table and relationship to database names
    class Data1 /*: public BaseEntity */ {
    public:
        int i;
        struct TEXT {
            const char* i()const {return "i";}
        };
        // bool readFromDatabase() overrides {...}
    };
    class Data2 {
    public:
        double d;
        // ...
    };
    // ... many more Data definitions here ...
    Table<Data1> m_data1;
    Table<Data2> m_data2;
    // ... many table members here
};
// try to avoid an instance of MyDatabase in any other CPP file
std::shared_ptr<MyDatabase> openDatabase();

// MyDatabase.cpp
// #include "MyDatabase.h"
std::shared_ptr<MyDatabase> openDatabase() {
    return std::make_shared<MyDatabase>();
}

// main.cpp
// #include "MyDatabase.h"
int main() {
    auto table = openDatabase();
}

[edit] changed title, thank you.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
KungPhoo
  • 516
  • 4
  • 18
  • It's explicit template *instantiation* you need, not specialization. For some discussion that is relevant to your question, have a look at answers at https://stackoverflow.com/questions/2351148/explicit-template-instantiation-when-is-it-used – Peter Mar 30 '21 at 10:00
  • Changed title, thank you. – KungPhoo Mar 30 '21 at 10:05
  • It may help if you use `Table` for the same `T` in more than one source file. But if `T` is always different, then there will be little difference. In addition, explicitly instantiated methods are more difficult to inline, so the resulting code may become slower. – rustyx Mar 30 '21 at 10:12

0 Answers0