I have some non-trivial C++17 functions marked as constexpr
. They are doing graph-related computation (depth-first traversal) and general algorithms (e.g., find, sort, unique...).
If I try to force evaluation at compile-time by putting the result in a constexpr
global variable, 3 things can happen:
- For small-size computation (to give an idea, lets say graphs of ~100 nodes, nodes being more or less integers) the compilation is fine (takes ~2s)
- With ~500 nodes, the compilation takes ~1min and takes 30GB of memory (!).
- With ~1000 nodes, the compilation requires too much memory for me to let it finish.
If I remove the constexpr
qualifiers and ask for a run-time computation, compilation and execution are very fast (less than 5s)
I used g++ 8.2 with -O3 -std=c++17.
Why is it taking so long ? Is g++ known for compile-time optimization problems of constexpr
?
What performance should I expect from constexpr
functions during compilation? From what I understand, the compiler turns itself into an interpreter for constexpr
computations. But I have absolutely no doubt that evaluating the same program in Python would be very fast, given the very small size of the data.
Edit: Such problems are mentionned here (Blog of a GCC developer)