Rewrite and memoize consteval_only_p for C++ reflection.
Speeds up compile times with C++ reflection by rewriting and memoizing the consteval_only_p predicate.
Speeds up compile times when using C++ reflection. The consteval_only_p predicate, which checks if a type can only be evaluated at compile time, was found to be slow due to its recursive nature. This commit rewrites the predicate for clarity and efficiency, introduces a tri-state to handle incomplete class types, and caches results to avoid redundant calculations. Compile time for a test case is reduced from 40x to 1.1x compared to builds without reflection.
In Details
The predicate consteval_only_p recursively walks TYPE_FIELDS, and this commit replaces the walk with direct recursion for clarity. Introduces a tri-state return to handle incomplete class types, and caches results (except for incomplete types). The memoization avoids redundant work in reflect.cc.
For Context
C++ reflection allows programs to inspect and manipulate their own structure at compile time. This commit optimizes a predicate used during reflection to determine if a type can only be evaluated at compile time (consteval_only_p). The predicate examines the fields of a type, and this optimization avoids redundant work, improving compile times when reflection is used.