VS2012 Generation Code Slow with Large Hard Coded Arrays

We have a tool that generates a class in a header file that is generated with hard-coded arrays. This autogenerated is inherited by a real implementation that uses auto-generated values.

Auto-generated example:

class MyTestAutoGen
{
    std::vector<int> m_my_parameter1;
    std::vector<int> m_my_parameter2;
    ...

public:
    MyTestAutoGen()    
    {
        SetDefaultValueFor_my_parameter1();
        SetDefaultValueFor_my_parameter2();
        ...
    }

    void SetDefaultValueFor_my_parameter1()
    {
        int tmp[] = {121,221,333,411,225,556,227,.......};
        m_my_parameter1.assign(tmp, tmp + 65025);
    }

    void SetDefaultValueFor_my_parameter2()
    {
        int tmp[] = {333,444,333,987,327,16728,227,.......};
        m_my_parameter2.assign(tmp, tmp + 65025);
    }

    ...
 };

Compilation takes a lot of time, and in the VS output windows I see that it hangs in the "Generating Code" phase of compilation, but it will complete compilation in about 15-30 minutes if the compiler does not work with a stack overflow.

"Multiprocessing Compilation" "Parallel Code Generation", . " " , .

, , , , , TEXT STATIC / . , ( ):

class MyTestAutoGen
{
    std::vector<int> m_my_parameter1;
    std::vector<int> m_my_parameter2;
    ...

public:
    MyTestAutoGen()    
    {
        SetDefaultValueFor_my_parameter1();
        SetDefaultValueFor_my_parameter2();
        ...
    }

    void SetDefaultValueFor_my_parameter1()
    {
        std::string codedDefaultValue = "\x079\0\0\0.......";
        std::stringstream str(codedDefaultValue);

        int tmp;
        for (int i = 0; i < codedDefaultValue.length() / sizeof(int); i++)
        {
            str.read((char*) &tmp, sizeof(int));
            m_my_parameter1.push_back(tmp);
        }
    }

    void SetDefaultValueFor_my_parameter2()
    {
        std::string codedDefaultValue = "\x04d\x001.......";
        std::stringstream str(codedDefaultValue);

        int tmp;
        for (int i = 0; i < codedDefaultValue.length() / sizeof(int); i++)
        {
            str.read((char*) &tmp, sizeof(int));
            m_my_parameter2.push_back(tmp);
        }
    }

    ...
 };

, ( ).

, -, ?

+4
3

, . static const:

static const int tmp[] = {121,221,333,411,225,556,227,.......};

, "" (.. ); static (, , ). , , const .

, , "" !

+4

: , .

void fill(std::vector<int>& dest, const std::string& src)
{
  std::stringstream str(src);
  const size_t size = src.length() / sizeof(int);
  int tmp;
  dest.clear();
  dest.reserve(size);
  for (int i = 0; i < size; i++)
  {
    str.read((char*) &tmp, sizeof(int));
    dest.push_back(tmp);
  }
}

void SetDefaultValueFor_my_parameter1()
{
  fill(m_my_parameter1, "\x079\0\0\0.......");
}
+2

, . . MSVC . .

const int count = 65025;
int param1_initializer[count] = {121,221,333,411,225,556,227, ... }

, , . , , , .

[] , , MSVC, #pragma optimize, . , .

+1
source

Source: https://habr.com/ru/post/1608283/


All Articles