Why do we now isolate into levels? Knuth, working alone, was justified to implement a feature across as many levels as he wished. In fact, the TeX code doesn’t really have levels in the strict sense. (See Notes 1 and 4 above.) But modern software design would establish those levels in design and enforce them syntactically. This makes more sense (essential?) for multi-person teams.
See Section 264 (p. 110) about assumption that no_new_control_sequence is false. No assert?
BLERG Just one assert—saw it recently. Wait, conditions are tested in Section 111.
BLERG building is a challenge
Ultimately, it’s unclear to me how the current maintainers of TeX edit the code. I’ll return to this in the section on the current versions of TeX.
, while the constexpr keyword indicates the
function can be executed at compile-time. (Note: This
explanation glosses over the frustrating subtleties of constexpr,
but those do not apply in this simple case.)
The detailed comparison of the C++ preprocessor and the constexpr
qualifier suggests a larger point, separate from this series’s focus on TeX.
The two approaches implement contrasting approaches to separation of concerns.
The preprocessor approach breaks
BLERG separation of concerns by systems versus separation of semantics from implementation
to the WEB macro width defined above
ultimately generate the same object code as if
A counterargument to this approach is that C++ functions might be substantially slower
than preprocessor macros.
Macros are expanded at compile-time, generating inline code, whereas function calls
add the overhead of parameter passing, a branch, and a return. If I want
to fairly benchmark “TeX-style” code versus “modern” code, my “TeX-style”
code must ultimately generate inline code. Fortunately, the addition
of the constexpr keyword allowed me to write C++ functions
that ultimately compiled the same inline code as macros. I will first
describe how I used constexpr and then address the subtleties
of the code it generates.
Implementing WEB constant definitions with a C++ compile-time constant
is straightforward: Simply define the identifier with the appropriate
C++ type, initialize it with the required constant, and prefix the
definition with constexpr. The defined identifier
value can be used anywhere a constant is required, including the
dimensions of a static array.
Functions were almost as simple. Wherever TeX used a Web macro for the basic data structures,
I instead wrote a constexpr function, a regular C++
function that can be inlined or even executed at compile time. The
function definitions are placed in the .hpp header file,
not the .cpp file with regular function definitions.
Two WEB macros that required a bit more sophistication to implement were TeX’s incr() and decr() macros, which increment or decrement a value. Of course, these could be simply written in C++ as pre- or post- increment/decrement but I wanted my C++ code to appear as close as possible to the original TeX code. These two macros are a challenge because they are applied to multiple types, including bytes and halfwords.
C++’s function overloading could handle this by writing a separate
function for every type. However, the generic function templates of
C++ 20 provide an even simpler solution, a function template that can
specialize to any type satisfying the std::integral
concept:
constexpr void incr(std::integral auto& v) { v++; }
The std::integral auto clause indicates this template can be
instantiated for any integral type. By the way,
my choice of post-increment was arbitrary; I could just
as easily have used pre-increment.
Implementing WEB macros as C++ constexpr functions proved straightforward but
how efficient will the resulting code be? The short answer is that
modern compilers will almost certainly generate code inline code identical to
that produced by the preprocessor macros
but by design the language does not guarantee that and in fact inline code is not
necesarily more efficient than function calls anyways. The qualifiers in that answer
make it less satisfying but they reflect the complex realities of
efficient code.
The design of C++ aims to support efficient code. To achieve this, the design
must manage to both not require compilers to generate unnecessary code but also
not get in the way of optimizations the compiler might perform. The definition
of constexpr functions threads this needle. It specifies that a compiler
is allowed to execute a function at compile time, not that it’s required to.
To enable compile time execution, a constexpr function is disallowed from using some
language features that only are practical or possible at runtime.
There are restricted circumstances where the compiler must execute the function at compile time: When the function is called within an expression whose result is required at compile time, such as the size of a static array. For example,
int a_plus1(int a) { return a + 1; }
constexpr int b_plus1(int b) { return b + 1; }
int array_1 [a_plus1(5)]; // Compiler error---a_plus1 cannot be executed at compile time
int array_2 [b_plus1(5)]; // Accepted---b_plus1 is executed at compile time
None of the WEB macros are called in such contexts, so implementing them as
restricted to a broad subset of C++ Such a function is restricted to a subset of the language that’s
The implications of declaring a function constexpr function
are subtle. It’s not a direct functional equivalent to a macro.
It can sometimes be completely executed a compile-time, as a macro is, but not always.
In cases where the compiler generates run-time code, that code will often
be faster than the same function without constexpr but
not always.
When instead generating run-time code
In particular, the compiler will handle a call differently depending upon the arguments:
can be elided by the compiler for two reasons:
The constexpr functions were a clear win over the C-preprocessor: Type-checked,
using C++ syntax rather than a distinct notation, while
costing nothing at runtime.
also perfectly capable of being called
at run-time in any context that demands run-time computation. The only remaining use of the
preprocessor is to #include header files. Some day, some happy day, we
will be able to use modules instead—but this
is not that day.
BLERG consteval??? Are the constexpr functions actually compiled at compile-time?
texnodes.c line 2912: page fault reduction
Haskell Criterion has more sophisticated analysis.