Skip to content

json_document: read files and streams in one block - #6164

Open
nlohmann wants to merge 2 commits into
developfrom
claude/read-input-in-one-block
Open

nlohmann wants to merge 2 commits into
developfrom
claude/read-input-in-one-block

Conversation

@nlohmann

Copy link
Copy Markdown
Owner

Summary

json_document::parse(FILE*) and json_document::parse(std::istream&) (and parse_copy(), read()) read their input into a string that grows by doubling: about 16 reads for a 0.6 MB file, each growth a reallocation and a copy. On Linux it is worse than the copies suggest: glibc grows and trims the heap around these blocks, so the memory faults in again on every parse. Parsing twitter.json from a file took 3.8× as long as it does with this change.

This reserves the rest of the input before the read loop when its size is known:

  • FILE*: for a regular file, the size from fstat() (_fstat64() on Windows) minus the position from ftell() (_ftelli64()), in the new detail::view::file_rest(). On other systems, and for pipes, terminals and the like, nothing changes.
  • Streams: std::streambuf::in_avail(). libstdc++ answers it for a std::filebuf with the rest of the file; std::stringbuf with the rest of the string. libc++ and MSVC's filebuf answer 0, so their file streams keep today's loop.

The read loop itself is unchanged, and the size is only a hint: a file that grows or shrinks while it is read is read as before, to its end.

Measurements

File to document (open, read, json_document::parse()), best of 3, warm page cache, µs.

Linux (8-vCPU Haswell VPS, Ubuntu 26.04, GCC 16.2, -O3 -march=haswell, glibc, libstdc++), pinned:

file FILE* before after std::ifstream before after
repeat.json (11 KB) 15.6 15.5 ±0% 16.4 14.6 −11%
twitter_api_compact_response.json (10 KB) 14.5 13.6 −6% 14.4 12.7 −12%
github_events.json (65 KB) 98.5 40.1 −59% 75.4 62.5 −17%
twitter.json (0.6 MB) 1,561 415 −73% 1,568 445 −72%
canada.json (2.2 MB) 6,031 1,694 −72% 6,282 1,727 −73%
semanticscholar-corpus.json (8.6 MB) 30,154 8,001 −73% 30,632 7,898 −74%
jeopardy.json (56 MB) 217,658 163,356 −25% 223,829 158,835 −29%

strace -c of 20 parses of twitter.json from a FILE*: 342 → 62 read calls, 98 → 5 brk calls.

macOS (Apple M-series, Apple clang, -O3), FILE*: repeat.json −5%, twitter_api_compact_response.json −9%, github_events.json −10%, twitter.json −18%, canada.json −16 to −21%, gsoc-2018.json −22%, jeopardy.json +1% (within noise). Streams are unchanged with libc++ (see above).

A first version got the size by seeking to the end and back (fseek/ftell, pubseekoff), which needs no platform code. The seeks cost small files on macOS up to 8%, so this uses fstat() and in_avail() instead.

Tests

  • In tests/src/unit-json_view.cpp, section "streams": a FILE* positioned after a prefix is read from there to its end, and file_rest() gives the rest of the file before the parse (on Windows, Linux, macOS and the other Unix systems) and 0 after it; an std::istringstream positioned after a prefix is read from there.
  • The existing tests of long files (lengths around a multiple of the 4,096-byte block) and streams pass unchanged.
  • unit-json_view.cpp (C++11 and C++17) passes on macOS (Apple clang) and on Linux with GCC 16 and Clang 22 with ASan and UBSan, and compiles without warnings with the flags of cmake/gcc_flags.cmake and cmake/clang_flags.cmake.
  • clang-tidy: the ci_clang_tidy build (Clang 22, in Docker) is clean.
  • Windows (MSVC, MinGW) is left to CI.
  • make amalgamate: only single_include/nlohmann/json_view.hpp changes.

Breaking changes

No breaking changes. The public API and the ABI are unchanged. What a parse of a file or stream returns is unchanged; only the capacity of the owned source string differs (it is now about the size of the input instead of the next power of two). <nlohmann/json_view.hpp> now also includes <sys/stat.h> (and <stdio.h>) on Windows and Unix systems, unless JSON_NO_IO is defined.

🤖 Generated with Claude Code

parse(FILE*) and parse(std::istream&) read their input into a string that
grew by doubling, with a reallocation and copy at each step (about 16 reads
for a 0.6 MB file). On Linux, glibc then grows and trims the heap around
these blocks, so the memory faults in again on every parse.

Reserve the rest of the input first when its size is known: for a FILE* that
is a regular file, from fstat (_fstat64 on Windows) and its position; for a
stream, from in_avail() (which libstdc++ answers for files with the rest of
the file). The read loop is unchanged; the size is only a hint.

File to document on Linux (GCC 16): twitter.json -72%, canada.json -72%,
semanticscholar-corpus.json -73%, jeopardy.json -25..-29%, small files
-0..-17%.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Comment thread include/nlohmann/detail/view/input.hpp Outdated
// copy at each doubling of the string (only a hint: the loop below reads
// what is there)
const std::streamsize rest = sb->in_avail();
if (rest > 0 && static_cast<std::size_t>(rest) < buf.max_size() - buf.size() - 4096)

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.

explain the 4k magic number?

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.

Fixed in a1b49b720: the four 4096 literals are now read_block_size(), with a comment. It is a conventional size, one page and the usual filesystem block size, not a tuned value, and it does two jobs: the least room offered to a read (so sgetn is never called with a tiny size once the capacity is used up), and, for the stream path, the slack reserved beyond the known remaining size so the read that finds the end needs no regrowth. I did not benchmark other sizes; the gain comes from reading in one block instead of doubling.

(Written by Claude Code on behalf of @nlohmann.)

Replace the four 4096 literals with read_block_size() and explain that it
is a conventional page-sized chunk (EOF slack and minimum read room), not a
tuned value. No behaviour change.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
}

// a regular file is read in one block, from its position to its end
std::FILE* tail_file = std::tmpfile(); // NOLINT(cppcoreguidelines-owning-memory)
@github-actions github-actions Bot added L tests aspect: json_view json_document, json_view (zero-copy view) 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 tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants