From 62b5974c0e0aa19fc773c9fa8c9c413de2bfcb57 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 25 Nov 2026 14:09:41 +0100 Subject: [PATCH] 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 --- include/nlohmann/detail/view/builder.hpp | 17 ++++- .../nlohmann/detail/view/document_data.hpp | 49 ++++++++++++++ single_include/nlohmann/json_view.hpp | 66 ++++++++++++++++++- tests/src/unit-json_view_large.cpp | 51 ++++++++++++++ 4 files changed, 181 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/view/builder.hpp b/include/nlohmann/detail/view/builder.hpp index 14be4ea7c..66b8d74c4 100644 --- a/include/nlohmann/detail/view/builder.hpp +++ b/include/nlohmann/detail/view/builder.hpp @@ -628,7 +628,11 @@ run_found: // covers the escapes of a typical document without regrowing it wanted = (std::max)(wanted, (std::min)((static_cast(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 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 diff --git a/include/nlohmann/detail/view/document_data.hpp b/include/nlohmann/detail/view/document_data.hpp index aea6c6972..5cf940a4d 100644 --- a/include/nlohmann/detail/view/document_data.hpp +++ b/include/nlohmann/detail/view/document_data.hpp @@ -8,6 +8,9 @@ #pragma once +#if defined(__linux__) + #include +#endif #include // array #include // size_t #include // uint8_t, uint32_t @@ -179,6 +182,52 @@ struct document_data NLOHMANN_VIEW_THROW(std::bad_alloc()); } + /// a large block: transparent huge pages for its 2 MiB-aligned interior, so that writing it takes one page + /// fault per 2 MiB instead of one per 4 KiB (Linux with THP in "madvise" mode, the default of many + /// 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) + const std::uintptr_t huge = std::uintptr_t {2} << 20u; + const auto first = (reinterpret_cast(p) + huge - 1) & ~(huge - 1); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + const auto last = (reinterpret_cast(p) + size) & ~(huge - 1); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + if (last > first) + { + static_cast(::madvise(reinterpret_cast(first), last - first, MADV_HUGEPAGE)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast,performance-no-int-to-ptr) + } +#else + static_cast(p); + static_cast(size); +#endif + } + + /// the size from which advise_huge() asks for huge pages + static constexpr std::size_t huge_min_bytes() noexcept + { + return std::size_t{4} << 20u; + } + + static NLOHMANN_VIEW_ALWAYS_INLINE void advise_huge(const void* p, std::size_t size) noexcept + { + if (NLOHMANN_VIEW_UNLIKELY(size >= huge_min_bytes())) + { + advise_huge_now(p, size); + } + } + + /// reserve() for a large index, before the parse loop starts (which then finds the room it asks for): with + /// huge pages where advise_huge() gets them. Out of line and outside the parse loop, so that the loop's code + /// stays as it is. + NLOHMANN_VIEW_NOINLINE void reserve_huge(std::size_t n) + { + const node* const before = nodes; + reserve(n); + if (nodes != before) + { + advise_huge_now(nodes, node_capacity * sizeof(node)); + } + } + /// make room for n nodes; keeps the first node_count nodes (throws /// std::bad_alloc for a count that does not fit the address space, /// instead of wrapping around in n * sizeof(node)) diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index d56c63272..16a445f1c 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -102,6 +102,9 @@ +#if defined(__linux__) + #include +#endif #include // array #include // size_t #include // uint8_t, uint32_t @@ -523,6 +526,52 @@ struct document_data NLOHMANN_VIEW_THROW(std::bad_alloc()); } + /// a large block: transparent huge pages for its 2 MiB-aligned interior, so that writing it takes one page + /// fault per 2 MiB instead of one per 4 KiB (Linux with THP in "madvise" mode, the default of many + /// 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) + const std::uintptr_t huge = std::uintptr_t {2} << 20u; + const auto first = (reinterpret_cast(p) + huge - 1) & ~(huge - 1); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + const auto last = (reinterpret_cast(p) + size) & ~(huge - 1); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + if (last > first) + { + static_cast(::madvise(reinterpret_cast(first), last - first, MADV_HUGEPAGE)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast,performance-no-int-to-ptr) + } +#else + static_cast(p); + static_cast(size); +#endif + } + + /// the size from which advise_huge() asks for huge pages + static constexpr std::size_t huge_min_bytes() noexcept + { + return std::size_t{4} << 20u; + } + + static NLOHMANN_VIEW_ALWAYS_INLINE void advise_huge(const void* p, std::size_t size) noexcept + { + if (NLOHMANN_VIEW_UNLIKELY(size >= huge_min_bytes())) + { + advise_huge_now(p, size); + } + } + + /// reserve() for a large index, before the parse loop starts (which then finds the room it asks for): with + /// huge pages where advise_huge() gets them. Out of line and outside the parse loop, so that the loop's code + /// stays as it is. + NLOHMANN_VIEW_NOINLINE void reserve_huge(std::size_t n) + { + const node* const before = nodes; + reserve(n); + if (nodes != before) + { + advise_huge_now(nodes, node_capacity * sizeof(node)); + } + } + /// make room for n nodes; keeps the first node_count nodes (throws /// std::bad_alloc for a count that does not fit the address space, /// instead of wrapping around in n * sizeof(node)) @@ -1964,7 +2013,11 @@ run_found: // covers the escapes of a typical document without regrowing it wanted = (std::max)(wanted, (std::min)((static_cast(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(); } @@ -2659,6 +2712,17 @@ template 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 diff --git a/tests/src/unit-json_view_large.cpp b/tests/src/unit-json_view_large.cpp index b5f58386c..f0c25cc09 100644 --- a/tests/src/unit-json_view_large.cpp +++ b/tests/src/unit-json_view_large.cpp @@ -598,3 +598,54 @@ TEST_CASE("json_view large objects") CHECK(d.memory_usage() >= with_index / 2); } } + +TEST_CASE("json_view large indexes and arenas") +{ + // inputs whose index (and decoded strings) take several MiB: on Linux, these blocks are reserved before the + // parse loop and get transparent huge pages (document_data::reserve_huge(), advise_huge()) + SECTION("an index of more than 4 MiB") + { + std::string text = "["; + for (int i = 0; i < 400000; ++i) + { + text += (i != 0 ? "," : "") + std::to_string(i); + } + text += ']'; + json_document d = json_document::parse(text); + CHECK(d.node_count() == 400001); + CHECK(d.root().size() == 400000); + CHECK(d.root()[0].get() == 0); + CHECK(d.root()[399999].get() == 399999); + CHECK(json_document::accept(text)); + + // a reused document: a small input, then the large one again + d.read(std::string("[1,2]")); + CHECK(d.root().size() == 2); + d.read(text); + CHECK(d.root()[123456].get() == 123456); + CHECK(d.root() == json::parse(text)); + } + + SECTION("decoded strings of more than 4 MiB") + { + // 2,000 strings of 3,000 bytes, each with escapes, so that every one is decoded into the arena + std::string s; + for (int i = 0; i < 1000; ++i) + { + s += "ab\\n"; + } + std::string text = "["; + for (int i = 0; i < 2000; ++i) + { + text += (i != 0 ? ",\"" : "\"") + std::to_string(i) + s + "\""; + } + text += ']'; + const json_document d = json_document::parse(text); + REQUIRE(d.root().size() == 2000); + const json expected = json::parse(text); + CHECK(d.root()[0].get() == expected[0].get()); + CHECK(d.root()[1999].get() == expected[1999].get()); + CHECK(d.root()[1999].get().size() == 4 + 3000); + CHECK(d.root() == expected); + } +}