Repository navigation
Conversation
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>
gregmarr
approved these changes
Nov 25, 2026
| // 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) |
Contributor
There was a problem hiding this comment.
explain the 4k magic number?
Owner
Author
There was a problem hiding this comment.
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) |
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
json_document::parse(FILE*)andjson_document::parse(std::istream&)(andparse_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. Parsingtwitter.jsonfrom 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 fromfstat()(_fstat64()on Windows) minus the position fromftell()(_ftelli64()), in the newdetail::view::file_rest(). On other systems, and for pipes, terminals and the like, nothing changes.std::streambuf::in_avail(). libstdc++ answers it for astd::filebufwith the rest of the file;std::stringbufwith the rest of the string. libc++ and MSVC'sfilebufanswer 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*beforestd::ifstreambeforerepeat.json(11 KB)twitter_api_compact_response.json(10 KB)github_events.json(65 KB)twitter.json(0.6 MB)canada.json(2.2 MB)semanticscholar-corpus.json(8.6 MB)jeopardy.json(56 MB)strace -cof 20 parses oftwitter.jsonfrom aFILE*: 342 → 62readcalls, 98 → 5brkcalls.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 usesfstat()andin_avail()instead.Tests
tests/src/unit-json_view.cpp, section "streams": aFILE*positioned after a prefix is read from there to its end, andfile_rest()gives the rest of the file before the parse (on Windows, Linux, macOS and the other Unix systems) and 0 after it; anstd::istringstreampositioned after a prefix is read from there.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 ofcmake/gcc_flags.cmakeandcmake/clang_flags.cmake.ci_clang_tidybuild (Clang 22, in Docker) is clean.make amalgamate: onlysingle_include/nlohmann/json_view.hppchanges.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, unlessJSON_NO_IOis defined.🤖 Generated with Claude Code