c++arrayscompilation

Why is the compiled file of this code so huge?


I recently wrote the belowing code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <array>
#include <set>
#include <map>
#include <unordered_map>
using namespace std;

using ll = long long;

constexpr int N = 5e4 + 5;

array<basic_string<ll>, N> sl, sr, sm;

int main()
{
    return 0;
}

The code inside the main function is not important, so I have omitted them.

When I tried to compile it in gcc version 14.2.0 (Rev1, Built by MSYS2 project), with option -std=c++20 -O3 -Wall -Wextra -pedantic, in Windows. It compiled for over 10 seconds and obtained an executable file of at least 8 MB, which is much larger than most other codes. But if I use c++17 or lower option, it can compile normally. And if I change the array into C-type array, it can compile normally. Also, if I use gcc version 12.2.0 (x86_64-posix-seh-rev1, Built by MinGW-W64 project) or clang, it also works.

We believe this is related to C++20, but we don't know excactly. So what is wrong? Thanks.

I don't think the compilation result should be like this, it shouldn't require so much time and memory. I tried it in godbolt and got a result like this.


Solution

  • Roughly speaking, std::string has a structure like dummy_string shown below, and in C++20 where the constructor can be written in constexpr, you can place an object like s in the .data section and omit the constructor. In C++17, it cannot be written in constexpr, but is placed in the .bss section and initialized in the constructor.

    struct dummy_string {
        union {
            char data[15];
            int size;
        };
        char* p;
        #if __cplusplus >= 202002L
        constexpr
        #endif
        dummy_string()
            :p{&data[0]}
        {
            for (unsigned long i = 0; i < sizeof(data); ++i)
                data[i] = 0;
        }
    };
    
    struct _s {
        dummy_string data[3];
    } s;
    
    int main() {
        return 0;
    }
    

    https://godbolt.org/z/jYTq5rrhx