Search code examples
c++arraysuint8t

Initialize huge uint8_t array statically with reasonable compilation time


I would like to statically initialize huge (megabytes) uint8_t array.

At the beginning I tried this:

constexpr uint8_t arr[HUGE_SIZE] = { 0, 255, ... };

Unfortunatelly, compilation time of above is very long (no optimization - around 30 seconds, optimizations on - above hour).

I found out that compilation time can be reduced to negligible (in both optimization off and on cases) if we use c style string initialization:

constexpr uint8_t arr[HUGE_SIZE + 1] = "\x00\xFF\x...";

Is this good approach in C++? Should I use some string literal to make types of both sides of above assignment equal?


Solution

  • If the array is really large, consider using a utility to directly generate an object file from the array. For example, with the GNU assembler you can do something like this:

        .section .rodata # or .data, as needed
        .globl arr
    arr:
        .incbin "arr.bin" # assuming arr.bin is a file that contains the data
        .size arr,.-arr
    

    Then assemble this file with the GNU assembler and link it to your program. To use this data elsewhere in your program, just declare it as extern "C":

    extern "C" const uint8_t arr[];