What it does
- Prioritized layers with a written merge rule
- <code>Origin</code> reports the layer, file, line and column of the winning value
- Reads by handle and by path, with no allocation
- Struct binding, schema, strict mode and interpolation with cycle detection
- Atomic hot reload. A failed reload keeps the previous document
- Secret masking in <code>Dump</code>
Measured
- Read by handle: 3.52 ns (target 5). By path, the deepest of 16,384 keys: 79.0 ns (target 100)
- Zero allocations across ten thousand reads and when parsing 16,384 keys
- Ten thousand reloads with four readers, one in ten with a corrupted file: no torn read in about 50 million
- Parser throughput, 0.59 MB file (MSVC 19.51, i7-6700): 133 MB/s. Target of 200 MB/s not met
Known limits
- The official TOML conformance suite does not run (there is an 18-case battery written against the spec)
- The file watcher polls on every OS, with no <code>inotify</code>
Spec 12 is closed. TOML v1.0.0, INI, strict and lenient JSON, environment and command line. Layers with a single merge and
Origin. Reads by handle without allocating memory. Binding to a struct. Schema with unknown-key detection. Interpolation with cycle detection. Atomic hot reload with snapshots. Writing with secret masking. C ABI layer. Two targets of the acceptance table are met and one is not. A read by handle takes 3.52 ns (target 5) and a read by path 79.0 ns on the deepest key out of 16,384 (target 100). The parser throughput is 133 MB/s against the 200 required. The section "The 200 MB/s" says how far off it is and why, with the measurement that proves it. This module was rewritten from scratch. The audit that preceded this delivery is summarized in "What was here before". It explains why the design is what it is.
---
What this module is
A configuration that is loaded from several places and read in a loop.
Config::CConfig C;
C.Init( Config::SConfigParams{} );
C.AddFileLayer( "base.toml", Config::EFormat::eToml, 10u, true );
C.AddFileLayer( "local.toml", Config::EFormat::eToml, 20u, false );
C.AddEnvLayer( "ETL_", 30u );
C.AddCommandLineLayer( argc, argv, "", 40u );
C.Resolve(); // merges, interpolates and validates. Once.
const auto H = C.Resolve( "server.port" ); // resolves the path. Once.
... C.GetInt64( H, &iPort ) ... // and here nothing is looked up.**CIniFile is the class that was called CConfig before this delivery. It still exists, with the same behavior and a name that says what it is: a mutable** INI file, which is read, modified and written again. Both classes are useful and serve different purposes. The inifile.hpp header explains the difference.
---
What was here before, and why it was not reused
The module that existed was an INI reader with linear search. The audit measured that reader against spec 12:
| measured before | |
|---|---|
| read by path | 13.9 ns on the first key, 565.9 ns on the last |
| allocations per read | 2 per GetString, 1 per GetInt |
| parser throughput | 29.1 MB/s, and in INI (the spec requires TOML) |
| allocations to parse 16,384 keys | 35,855 |
The problem was in the requirements, and the execution was correct. The header declared linear search as a choice (a configuration file has dozens of entries, not millions), and for an INI reader that is correct. Spec 12 starts from the opposite premise and says so in the first line of the performance mandate: "Applications read configuration in loops, in hot paths, and in per-request code, and a config library that does a string hash lookup per read is a library that gets worked around."
The storage was what was not reused. vector<SSection> with one std::string per name and per value is incompatible with zero-copy parsing, with the flat array and with the atomic swap of an immutable document. The rest was reused: the documented INI dialect, the order preserved on write and the discipline of "a parse that fails leaves the object empty", which is exactly what hot reload needs.
One of the measured allocations went away without any rewrite. CIniFile's TryGetInt built a std::string just to pass it to the converter, when the value was already there as a std::string. Today it is zero allocations in ten thousand reads, and the benchmark requires that.
---
The four decisions that govern everything
1. Values live in a flat array, and the tree is in the indices. An SValue is sixteen bytes and none of them is a pointer. Walking a table is moving forward in a vector. The whole document is three vectors (values, keys and text), so it is copyable and swappable through an atomic pointer.
2. Keys live in a separate sorted index, built once at the end of Resolve. The search is binary by hash and always confirms the path afterwards. Two keys with the same hash are rare, but they are unavoidable, and returning the wrong value on a collision would be worse than being slow.
3. All text lives in a single arena. A text value is an offset and a length inside it. That is why reading does not allocate memory: not by path, not by handle, not for text.
4. The resolved document is immutable. That is what makes hot reload a pointer swap, with no lock on the read path.
---
The 200 MB/s
"Parse throughput: > 200 MB/s for TOML."
It is not reached. Measured: 133 MB/s, on a 0.59 MB file with 16,384 keys. Release, MSVC 19.51, x64, Intel i7-6700.
What was fixed along the way, and was much worse
The first version had two quadratic cost walls. The benchmark that found them measures the same file split in different ways:
| shape | parser, before | parser, after |
|---|---|---|
| 16,384 sections of 1 key | 0.8 MB/s | 112.9 MB/s |
| 4,096 × 4 | 6.9 MB/s | 144.4 MB/s |
| 1,024 × 16 | 58.3 MB/s | 169.0 MB/s |
| 256 × 64 | 97.6 MB/s | 139.2 MB/s |
| 64 × 256 | 53.8 MB/s | 137.4 MB/s |
| 16 × 1,024 | 12.7 MB/s | 146.3 MB/s |
The cause was the same at both ends. The parser looks for a sibling with the same name before each insertion (that is how TOML detects a repeated key), and that search walked the list of siblings. With 16,384 sections, the list that grows is the list of children of the root, and that is 134 million text comparisons.
Neither shape is absurd. A flattened conf.d has thousands of keys at the root, and a translation table has thousands in a single section. A library that only handles the middle shape is no use. The fix was a chained bucket index inside the node pool. It costs eight bytes per node and removes both walls.
What is missing for the 200, measured
| measured | |
|---|---|
| the parser alone | 133 MB/s |
the whole Resolve | 25 to 50 MB/s |
| what is not the parser | 55% to 69% of the total time |
The spec target is for the parser, and that is the number reported. Most of the time spent loading a configuration is outside it: in merging the layers, in flattening into the flat array and in building the path index. The spec does not budget for this work anywhere, and it is this work that makes reading free afterwards.
One thing was cut because of this, and it was the one that paid off the most. With a single layer there is nothing to merge, and the parsed tree is used directly, without a node-by-node copy. Merging one layer into an empty tree is copying everything, and that was two thirds of Resolve in the most common case of all: the program that reads one file and nothing else.
What is missing for 200 MB/s in the parser is fine-grained profiling work, and there is no identified fat here waiting to be cut. It is noted with the number next to it, so that it is not delivered silently.
---
What was measured and is required by the tests
| read by handle | 3.52 ns (the spec target is 5) |
| read by path, deepest key out of 16,384 | 79.0 ns (the target is 100) |
| read by path, first key | 77.3 ns |
| allocations in ten thousand reads by path | zero |
| allocations in ten thousand reads by handle | zero |
| allocations in ten thousand text reads | zero |
| allocations to parse 16,384 keys | zero (it is one arena, not one per pair) |
| ten thousand reloads with four readers | no torn read, over ~50 million reads |
| corrupted reloads, one in every ten | all failed, none passed |
| old document after a reload that failed | whole and in effect |
| documents retained at the end of ten thousand reloads | 0 to 1 (they were reclaimed) |
| three writes in 15 ms | one change notification (the window merged the three) |
| missing key versus wrong type | distinct states, both named |
| integer that does not fit in the binding field | rejected, and the default stays |
| key with a typo, in strict mode | detected |
secret in a Dump | does not come out, and the rest does |
The allocation counter fails the test. The global allocation functions are replaced, the complete set, including the nothrow variants.
**The timing rows are only required with NDEBUG.** In Debug, MSVC adds checks to every string_view, and failing on that number would blame the module for a cost that is not its own. The RPC module already uses the same rule.
---
The merge rule, which the spec requires to be written down
"Array merge ambiguity is the most common source of config surprise, so it must be stated and testable."
- Scalars replace. The highest-priority layer wins, whole.
- Tables are merged, key by key, recursively. A layer that only defines
server.portdoes not eraseserver.bindfrom the layer below. - Arrays replace, by default. An array is a list someone wrote in full. Concatenating by default would make it impossible to remove an element in an upper layer. Whoever wants to concatenate turns it on in the layer with
AppendArraysOnLastLayer, and then it is a choice visible in the code.
Layers with the same priority stay in insertion order, regardless of what the sorting algorithm produces. A configuration that changes depending on the implementation is not reproducible.
Origin(path) says which layer, which file, which line and which column gave the value that won. It is cheap to store and answers "why is this 8080 if I wrote 9090".
---
Hot reload, and its only hard problem
"Reload is atomic or it does not happen. Parse and validate the new document completely, and only then swap. A failed reload keeps the old document live and reports the error. Never leave the application with a half-applied configuration."
The hard problem is what happens to whoever was reading, more than the swap itself. One thread is in the middle of a loop with handles from the document. Another has just reloaded and wants to throw the old one away.
The solution cannot be a lock on the reader side, because the spec forbids it. A lock per read puts every thread hammering the same cache line, and that is the cost this module exists to avoid. So whoever reads in a loop takes a Snapshot, the snapshot holds a reference, and the old document only dies when its last snapshot goes away.
The pointer the readers load is raw and atomic, on purpose: an std::atomic<shared_ptr> on MSVC is a hidden spinlock. The object's lifetime is guaranteed by the shared_ptr that CConfig keeps and by the retired list.
The benchmark runs the bad case: ten thousand reloads with four readers, and one in every ten with the file corrupted on purpose. The thousand corrupted ones all failed, the ten thousand good ones all passed, and there was not a single torn read across ~50 million concurrent reads.
The debounce window is mandatory. An editor saves a file in several operations (Vim writes a temporary file, deletes the original and renames), and without the window that gives three reloads, one of them over a truncated file. Measured: three writes in 15 ms give one notification.
On the writer side, one operation at a time. The watcher calls Reload from its own thread, and the user may be adding layers, resolving or reloading at the same time. Until 2026-10-06 AddLayer checked the 256-layer limit outside the lock (two threads got past layer 255 together), and Reload added to the counters outside it (a reload went missing from the count). Now AddLayer, Resolve, Reload and everything else that touches the layers go through a build lock of their own, separate from the one Snapshot and Stats take: a Snapshot does not wait for an entire Build. The test runs eight threads adding layers at the limit, and two threads reloading together with the watcher, with TSan enabled in the linux-tsan tree.
The two readers that were left out. Until 2026-10-11 LayerCount() read the list size without a lock, and Problems() returned a reference to the list that the watcher's Build clears and fills on every reload. The test saw, in Release, the count go down while another thread was adding layers. Now LayerCount() takes the writer lock, and Problems() returns a copy taken under the build lock (it waits for the Build in progress). Code that did const auto & Ps = C.Problems(); still compiles: the reference keeps the copy alive until the end of the scope.
---
How this was verified
tests/config.cpp, 144 checks, and tests/config_c_abi.c, 49 in pure C, compiled as C11 and as C17 without extensions. Green in Debug and Release.
The section that matters most is the rejections. A parser that only accepts is not validating anything. That is why there are thirteen invalid TOML cases that must be rejected (a table defined twice, [a] after a.b has closed a, extending an inline table, a decimal with a leading zero, consecutive underscores in a number) and five similar-looking valid cases that must pass, such as [a] after [a.b].
Three defects the benchmark caught. The first was of the worst kind:
- a text value with escapes was left pointing to freed memory. Parsing is zero-copy, and only the texts with escapes are materialized in an arena. That arena lived in a pool declared inside the layer loop. The texts without escapes point into the layer's source, which survives. That is why they worked, and the vast majority of files looked correct. Only those that had
\norácame out with garbage. - the two quadratic walls from the previous section.
- the tail after the quotes, in INI, was cut from the line with an index computed on the trimmed version. It took the wrong tail, shifted by as many characters as there were spaces before the quote.
---
---
Monitoring on Linux: the header said inotify, and there was no inotify
On the first run of the benchmark on Linux, both monitoring checks failed. The change notification never arrived, and the document kept the old value.
First: it was not WSL
inotify does not deliver events on /mnt/c, and that was the convenient explanation. The same test ran with the process working on native ext4 and failed the same way. The convenient explanation was wrong, and checking it cost one command.
The header described an intention as if it were code
It was written, at the top of watch.cpp, that the Linux path was inotify, written from the documentation and never run, because the development machine is Windows.
It was never actually written. There is not a single call to inotify in the tree. What runs on Linux is polling (the same as on macOS), and anyone reading that note came away thinking the fast path existed and only needed testing.
This document was right all along. The list of pending items, further below, always said that monitoring runs by polling. The two texts contradicted each other, and whoever read the code believed the code.
The note in watch.cpp was corrected. A note that describes what was intended, written in the tense of someone who has already done it, is worse than no note: it silences the question.
And polling was blind to any change within the same second
Windows: ftLastWriteTime -> 100 ns units
POSIX: st_mtime -> SECONDSTwo writes in the same second, with the same file size, gave the same timestamp. A hot reload happens in milliseconds: it was exactly the case the module exists to handle, and exactly what did not work.
It is now st_mtim, the POSIX.1-2008 timespec with nanoseconds. macOS calls the field st_mtimespec, and the difference lives in a macro next to the includes. It cannot live in an #if inside the function, because the macos.syntax probe extracts each macOS-only branch and compiles that branch on its own. An #if in the middle of a function produces a branch that refers to local variables, and that branch does not compile outside the function body. A macro is a directive-only branch, which the probe accepts.
The macOS path is still written and never run. Now it is written in a form the benchmark can watch, and that is the difference between untested and untestable.
What is missing
- The official TOML conformance test suite does not run here. The acceptance table asks for "the official test suite, both valid and invalid cases, all passing", and what exists is the set of eighteen cases described above, written against the standard. Running the official one requires bringing its repository into the tree and a runner that speaks its JSON protocol. It is bounded work, and it is not done.
- **Linux
inotifyand macOSFSEvents.** Monitoring runs by polling the modification time and the size on all systems (on Windows, withGetFileAttributesExW). It is written this way on purpose. Polling is what makes the ten-thousand-reload benchmark deterministic. The spec, however, names the two native interfaces, and they are not there. - Trivia retention is of the original text, without per-value granularity.
DumpLayerreturns the layer exactly as it came in, with the comments, the spacing and the order the person wrote. It is the most faithful way to preserve that, and it comes for free because parsing is zero-copy. What does not exist is rewriting a key in the middle of a file while keeping the comments around it. In addition,DumpLayerdoes not mask secrets, because the source text carries no value marks at all. This is stated in the code, and a raw layer should not go to a log. - The INI round trip does not preserve arrays. INI has no arrays, and inventing a syntax for them would mean writing a file that only this module could read. They are written out joined by commas and, when read back, they are a text value.
- **
fuzzingof each parser**, which the acceptance table asks for with a long run. - The sanitizers. ASan, UBSan and TSan are a row in the table and do not run on this machine. MSVC ships ASan, but not TSan, and TSan is the one that would matter for hot reload.
- A perfect hash for the path index. The spec lists this option as the first alternative to binary search. Binary search already meets the 100 ns target with room to spare over 16,384 keys, so the other one is noted with the number next to it, without guessing.