Skip to content

json_view: transparent huge pages for large indexes and arenas on Linux - #6160

Open
nlohmann wants to merge 1 commit into
developfrom
claude/thp-large-index
Open

nlohmann wants to merge 1 commit into
developfrom
claude/thp-large-index

Conversation

@nlohmann

Copy link
Copy Markdown
Owner

Summary

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: about 23,000 page faults per parse of jeopardy.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:

  • Index: build() reserves an index of 4 MiB or more before the parse loop starts, through the new out-of-line document_data::reserve_huge(), which calls madvise(MADV_HUGEPAGE) on the fresh block's 2 MiB-aligned interior. The parse loop's own reserve() then finds the room it asks for.
  • Decode arena: when it grows to 4 MiB or more, it is reserved first, advised, then resized (the resize is what writes it).

With THP in madvise mode (the default of Ubuntu, Debian and many other distributions) one fault then covers 2 MiB. With THP never, 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 into builder::run(). Even with the madvise out of line, GCC 16 then compiled the whole loop differently (4 more fail() 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 in build(), 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:

input cycles time page faults per parse
jeopardy.json (53 MB), fresh document −21.3% −21.5% 23,055 → 8,569
semanticscholar-corpus.json (8.6 MB) −23.9% −24.2% 1,601 → 338
jeopardy.json, reused document −1.4% −1.0% 3,228 → 2,513
gsoc-2018.json (3.3 MB) −1.2% +0.1%
twitter, citm_catalog, canada, update-center, github_events, apache_builds, marine_ik, instruments, random, twitter_api_response −0.6% … +0.5% unchanged; identical instruction counts
twitter.json, accept() ±0.0%

On an 8-vCPU Haswell VPS (noisy, no counters), a fresh jeopardy.json parse 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 in build() is under #if defined(__linux__) && defined(MADV_HUGEPAGE)), and the arena's growth only gains a reserve() before its resize(), with the same allocation size.

Tests

  • New test case "json_view large indexes and arenas" in 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 with json::parse().
  • All 24 json_view test binaries (C++11 and C++17) pass with ASan and UBSan with GCC 16 on Linux (Docker gcc:16, arm64), where the new code is compiled; the new test also passes on macOS.
  • clang-tidy was not run locally; CI runs it.
  • make amalgamate: only single_include/nlohmann/json_view.hpp changes.

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

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>
@github-actions github-actions Bot added L tests aspect: json_view json_document, json_view (zero-copy view) labels 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaner to put the entire function inside the #if and then have a similar #if around the one call?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I missed that there were two callers.

@nlohmann nlohmann added this to the Release 3.13.0 milestone Nov 25, 2026
@nlohmann nlohmann added 🚀 ready to merge Ready to merge - just waiting for CI to complete. performance labels Nov 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aspect: json_view json_document, json_view (zero-copy view) L performance 🚀 ready to merge Ready to merge - just waiting for CI to complete. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants