Files
json/include
Niels Lohmann ad3b07f7e5 Keep the descent bookkeeping in one place
Copying carried a depth count, a depth limit and a guard of its own, and
the comparison in the follow-up added a second set beside them. Neither
operation needs its own: they are never nested inside one another by the
library - copying a value does not compare one, and comparing two values
does not copy them - and where user code nests them anyway, sharing the
count only ends a descent sooner than it had to.

So there is now one nesting_depth(), one nesting_depth_limit(), one
nesting_depth_exhausted() and one nesting_depth_guard, which the follow-up
uses instead of adding its own. Inverting the test in copy_structured
leaves the too-deep case and the no-thread-local case as the same code.

The guard takes the count where the caller has already looked it up to
test it, and looks it up itself where the caller cannot - the comparison
operators are written as a macro, and a macro cannot use the preprocessor.
Whether an operator descends at all is passed to nesting_depth_exhausted()
rather than tested at the call site, where the constant makes MSVC report
C4127.

The switch that copies the value of anything that is not an object or an
array was written twice - once in the copy constructor, once in
copy_shallow - so that adding a value_t meant editing both, and missing
one would have been silent. It is copy_leaf_value now, and inlined: both
callers have already sorted the containers out, and folding that test into
the switch is what keeps a value made mostly of numbers copying as fast as
it did.

Copying canada.json, citm_catalog.json and twitter.json is within 0.6% of
what it was before, measured as a paired ratio over 18 interleaved rounds
against a run-to-run spread of 0.3%.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-08-21 08:49:54 +02:00
..