← Latest papers
💻 computer science

What Field Enumeration Costs to Compile in C++26: Native Reflection, Structured-Binding Packs, Reflection Libraries and Hand-Written Serializers on GCC 16

This paper benchmarks C++26 field enumeration mechanisms on GCC 16, revealing that while P1061 structured-binding packs offer compile-time performance comparable to hand-written code, the native P2996 span-form enumeration is several times costlier and performs on par with Boost.PFR, while reflect-cpp and Glaze incur even higher costs in whole translation units partly because their visitors also compute field names.

Original author: Denys Kachalenko

Published 2026-11-24✓ Author reviewed ⓘ
📖 5 min read🧠 Deep dive

Original author: Denys Kachalenko

Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). ✨ This is an AI-generated explanation of the paper below. It is not written by the author. For technical accuracy, refer to the original paper. Read full disclaimer

In the world of computer programming, there is a constant tension between writing code that is easy for humans to read and writing code that is fast for machines to process. One specific task, known as serialization, involves taking a complex data structure—like a digital record containing a person's name, age, and address—and converting it into a stream of bytes so it can be saved to a disk or sent over a network. For decades, programmers have had to write this conversion code by hand, listing every single field of every single data type they use. This is tedious, error-prone, and creates a lot of repetitive text. To solve this, the programming community has long relied on tools that automatically inspect these data structures and generate the necessary code. Recently, a new set of tools has arrived with the C++26 programming standard, promising to make this inspection built directly into the language itself, rather than relying on external add-ons. The question for developers is not just whether these new tools work, but how much they cost the computer in terms of time and resources while the program is being built.

A researcher named Denys Kachalenko set out to measure exactly this cost. He treated the computer's compiler—the software that translates human-written code into machine instructions—as a laboratory subject. He built a testing system that generated thousands of different data structures, ranging from simple records with a few fields to complex graphs with nested lists and optional values. He then forced the compiler to process these structures using every major method available: the brand-new native features of the C++26 standard, older library-based tools that have been used for years, and the traditional method of writing the code by hand. The goal was to see how long each method took to compile and whether the new language features offered a genuine speed advantage or if they introduced hidden delays.

The study revealed that the new native features are a mixed bag, heavily dependent on how they are used. When the researcher tested the span form, which is the form used in several of P2996's own examples, the compiler took nearly eight times longer to process a single data structure compared to using a C++26 feature called structured-binding packs (P1061). This P1061 technique, which groups data fields together automatically, performed almost as fast as the best hand-written code. The slowdown in the span form was due to a combination of factors: about half of the cost comes with the span itself, including GCC's quadratic registration of block-scope statics (PR128928), which is the part that grows with the number of structs; the rest does not grow with the number of structs.

However, the researcher found that this slowdown was not inevitable. By changing the way the code was written to use a constant-array form instead of the span form, the cost dropped significantly. In this form, the new language feature became even more efficient, costing less than half of what the Boost.PFR library tool costs. The study also highlighted that the choice of tool matters significantly. When the researcher tested entire programs containing hundreds of data structures, the difference between using a modern library and the new native code was substantial. Specifically, compiling 200 structs took 0.59 seconds with inline native enumeration, whereas the C++26 paths of libraries like reflect-cpp and Glaze took between 3.86 and 4.13 seconds, partly because their visitors also compute field names. Boost.PFR has two paths: its older C++17 path slowed down almost quadratically as the number of data types grew, while its default C++26 path, the one compared with native enumeration here, grows about linearly.

Another key finding concerned the cost of simply enabling these new features. The study showed that turning on the new reflection capability in the compiler made the entire compilation process slower, even for code that did not use the feature at all. On GCC 16.2.0, this made reflection-free code up to 1.264x costlier per type, and on GCC 16.1.0, up to 2.578x costlier. This indicates that the performance of these new tools is currently tied to the specific version of the compiler being used, and that developers must be cautious about enabling experimental flags in large projects. The research also examined how different programming styles affect compile time. It found that when programmers use a single overloaded function name to handle many different data types, the compiler has to check every possible match for every single use, causing the compile time to explode as the project grows—eventually overtaking native reflection between 1,000 and 2,000 types. Using distinct names, hidden friends, member functions, or class-template specializations avoids this bottleneck, keeping the build times manageable.

Ultimately, the paper concludes that for a programmer building a system with a few hundred data types, the new native C++26 features can be a viable option, provided they are written in a specific, efficient style. The most efficient native approach, using P1061 packs, is about as cheap as hand-written code, while the span form is roughly as expensive as using Boost.PFR. In a whole translation unit, the library outweighs the mechanism. The study notes that comparisons between these mechanisms depend on the specific compiler point release. The research provides a clear map of where these tools stand today, helping developers make informed decisions about whether to adopt the new standard or stick with proven methods.

Drowning in papers in your field?

Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.

Try Digest →