Files
json/tests
Niels Lohmann 58602b043b json_document: read files and streams in one block
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>
2026-11-25 19:16:11 +01:00
..