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

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>
This commit is contained in:
Niels Lohmann committed 2026-11-25 14:09:41 +01:00
1 parent 322063822f
commit 62b5974c0e
4 files changed
+181 -2

No files matched your search

+16 -1
View File
@@ -628,7 +628,11 @@ run_found:
// covers the escapes of a typical document without regrowing it
wanted = (std::max)(wanted, (std::min)((static_cast<std::size_t>(m_end - m_begin) / 4) + 16, std::size_t{4096}));
}
m_doc.arena.resize((std::max)(m_doc.arena.size() * 2, wanted));
const std::size_t size = (std::max)(m_doc.arena.size() * 2, wanted);
// (reserved before the resize writes it, so that a large arena gets huge pages)
m_doc.arena.reserve(size);
document_data::advise_huge(m_doc.arena.data(), m_doc.arena.capacity());
m_doc.arena.resize(size);
m_arena_write = m_doc.arena.data() + used;
m_arena_end = m_doc.arena.data() + m_doc.arena.size();
}
@@ -1323,6 +1327,17 @@ template<typename FloatType, bool NulIsEnd>
inline bool build(document_data& d, const char* src, std::size_t size, bool comments, bool trailing_commas, bool sentinel, bool validate_only,
parse_failure& failure)
{
#if defined(__linux__) && defined(MADV_HUGEPAGE)
if (!(validate_only && !comments && !trailing_commas))
{
// a large index is reserved here, with huge pages (see document_data::reserve_huge())
const std::size_t n = estimate_nodes(src, size);
if (NLOHMANN_VIEW_UNLIKELY(n >= document_data::huge_min_bytes() / sizeof(node)))
{
d.reserve_huge(n);
}
}
#endif
if (validate_only && !comments && !trailing_commas)
{
// accept() of plain JSON: nothing is stored. The other option sets