C++ Logo

sg14

Advanced search

[SG14] Questioning the validity of constexpr-everything - at least in reference to colony

From: Matt Bentley <mattreecebentley_at_[hidden]>
Date: Thu, 4 Mar 2021 12:59:33 +1300
There's been a question come up in LEWG surrounding whether, like
std::vector, colony should be made constexpr ie. all it's
member/non-member functions should be constexpr.

The problem is there hasn't been a lot of thought put into the
side-effects of constexpr function calling, and the field is still
pretty new - only GCC & clang >= v10 support it so far, and GCC10
doesn't appear to fully support constexpr vector as yet - at least in
the version I'm using - so there isn't any real field experience as to
how this affects things in the real world.

Having done some testing myself, running benchmarks on the colony test
suite with a constexpr version of colony vs original under GCC10 with
-O2 -march=native -std=c++20, there is a consistent 2% speed decrease,
and other benchmarks show similar results.

This could be due to a number of things:
1. Storing results of functions in code causes increased cache usage
whereas (at least for some results) simply doing the calculation on the
CPU would be faster than pulling it from memory.
2. Cache effects of storage of function results interfering with
locality of actual code.
3. Early bad optimisation from the compiler.

My concerns are as follows: constexpr functions
(a) shift the responsibility of storage of pre-calculated results from
the programmer to the compiler, and removes the ability for the
programmer to think about the cache locality/optimal storage of
precalculated results
(b) shift the decision of whether or not to evaluate at
runtime/compile-time to the compiler, where in some cases doing it at
compile-time and storing the result may decrease performance (see Doom 3
BFG edition technical notes for their experience with pre-calc'ing
meshes vs calculating them on the fly on modern CPUs:
https://fabiensanglard.net/doom3_documentation/DOOM-3-BFG-Technical-Note.pdf)
(c) may dramatically increase code size/file size in some cases if the
return results of constexpr functions are large
(d) may dramatically increase compile times
(e) do not provide guarantees as to how long the results of functions
are stored in memory, or where they are stored. Are they stored until
use and then popped off the heap? Or are they on the stack and stay
there for the lifetime of the program?

Given this I am reluctant to make colony constexpr at this stage.

It is possible that I simply don't understand enough about the
technology and the compiler design, but I would like SG14 feedback on this.

Thanks,
M

Received on 2021-03-03 17:59:39