Repository navigation
Conversation
A fresh json_document for a large input writes a large new index, and on Linux every 4 KiB page of it faults in on first write. For jeopardy.json (53 MB, minified) that is about 23,000 page faults per parse. On Linux, build() now reserves an index of 4 MiB or more before the parse loop starts, through document_data::reserve_huge(), which asks for transparent huge pages (madvise(MADV_HUGEPAGE)) on the fresh block's 2 MiB-aligned interior; the parse loop's own reserve() then finds the room. The decode arena does the same when it grows to 4 MiB or more (reserve, advise, then resize). With THP in "madvise" mode, the default of many distributions, a page fault then covers 2 MiB. Elsewhere, and for smaller inputs, nothing changes. The reservation stays outside the parse loop on purpose: changing the reserve() that is inlined into builder::run() made GCC 16 compile the loop differently (more instructions, a spilled counter) and small inputs 2-4% slower; with this change run() is byte-for-byte the same. EC2 c7i.2xlarge (Sapphire Rapids), GCC 16, -march=haswell, perf stat, best of 5, json_document::parse(): - jeopardy.json fresh: -21.3% cycles, -21.5% time (faults 23,055 -> 8,569) - semanticscholar-corpus.json: -23.9% cycles - jeopardy.json reused: -1.4% - twitter, citm_catalog, canada, gsoc-2018, update-center, github_events, apache_builds, marine_ik, instruments, random, twitter_api_response: -1.2% to +0.5% cycles, identical instruction counts Signed-off-by: Niels Lohmann <mail@nlohmann.me>
gregmarr
reviewed
Nov 25, 2026
| /// distributions; a no-op otherwise) | ||
| static NLOHMANN_VIEW_NOINLINE void advise_huge_now(const void* p, std::size_t size) noexcept | ||
| { | ||
| #if defined(__linux__) && defined(MADV_HUGEPAGE) |
Contributor
There was a problem hiding this comment.
Cleaner to put the entire function inside the #if and then have a similar #if around the one call?
Owner
Author
There was a problem hiding this comment.
Not here, because advise_huge_now has several callers: advise_huge and reserve_huge, which builder.hpp calls from two places. Guarding the function would mean an #if at each of them, plus a non-Linux fallback for reserve_huge (which must still reserve). With the #if inside the leaf function, the other platforms still compile and check all the code above it, and the non-Linux body is just the two unused-parameter casts.
(Written by Claude Code on behalf of @nlohmann.)
Contributor
There was a problem hiding this comment.
Agreed, I missed that there were two callers.
gregmarr
approved these changes
Nov 25, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A fresh
json_documentfor a large input writes a large new index, and on Linux every 4 KiB page of it faults in on first write: about 23,000 page faults per parse ofjeopardy.json(53 MB, minified). Reusing the document avoids this, so a fresh document was ~30% slower than a reused one.On Linux, this asks for transparent huge pages for large blocks:
build()reserves an index of 4 MiB or more before the parse loop starts, through the new out-of-linedocument_data::reserve_huge(), which callsmadvise(MADV_HUGEPAGE)on the fresh block's 2 MiB-aligned interior. The parse loop's ownreserve()then finds the room it asks for.With THP in
madvisemode (the default of Ubuntu, Debian and many other distributions) one fault then covers 2 MiB. With THPnever, on other systems, and for smaller inputs, nothing changes.Why outside the parse loop: a first version advised the block inside
reserve(), which is inlined intobuilder::run(). Even with themadviseout of line, GCC 16 then compiled the whole loop differently (4 morefail()call sites, a counter spilled to memory, +1–2% instructions) and small inputs got 2–4% slower, also with-falign-loops=64 -falign-functions=64. With the reservation inbuild(),run()is byte-for-byte the same as before (only addresses of string constants differ).Measurements
EC2
c7i.2xlarge(Xeon Platinum 8488C, Sapphire Rapids), Ubuntu 26.04, GCC 16.2,-O3 -march=haswell,perf stat(user + kernel), pinned, best of 5,json_document::parse()of one file in a loop:jeopardy.json(53 MB), fresh documentsemanticscholar-corpus.json(8.6 MB)jeopardy.json, reused documentgsoc-2018.json(3.3 MB)twitter,citm_catalog,canada,update-center,github_events,apache_builds,marine_ik,instruments,random,twitter_api_responsetwitter.json,accept()On an 8-vCPU Haswell VPS (noisy, no counters), a fresh
jeopardy.jsonparse went from 108 to 77 ms, and minor faults over 10 parses from 252,000 to 109,000. Elsewhere the index code is unchanged (the reservation inbuild()is under#if defined(__linux__) && defined(MADV_HUGEPAGE)), and the arena's growth only gains areserve()before itsresize(), with the same allocation size.Tests
tests/src/unit-json_view_large.cpp: a minified array of 400,000 integers (an index above 4 MiB), parsed fresh, validated, and read into a reused document; and 2,000 escaped strings of 3,000 bytes (an arena above 4 MiB), compared withjson::parse().json_viewtest binaries (C++11 and C++17) pass with ASan and UBSan with GCC 16 on Linux (Dockergcc:16, arm64), where the new code is compiled; the new test also passes on macOS.make amalgamate: onlysingle_include/nlohmann/json_view.hppchanges.Breaking changes
No breaking changes. The public API and the ABI are unchanged; only how the memory of large documents is requested from the OS differs on Linux. Memory use is the same (the reservation is the one the parse made anyway); with THP, the kernel may back the touched part of a large index with 2 MiB pages.
🤖 Generated with Claude Code