Reserve the result of dump() in big steps once the output is big

dump() appended to an empty string, which reallocates and copies the output
each time it doubles; with glibc every new big block is fresh memory that is
faulted in page by page. dump() now uses its own output adapter: the first
write of at least 512 characters reserves 1 MiB, and four times as much
whenever that is not enough, up to 64 MiB. Smaller outputs never reserve.
String types without reserve() keep appending.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-29 12:28:26 +01:00
1 parent 322063822f
commit 027d730547
7 files changed
+228 -2

No files matched your search

+5
View File
@@ -64,6 +64,10 @@ so the result can be larger than the value by a factor of its nesting depth (see
## Notes
Once the output reaches 512 characters, the result reserves 1 MiB, and four times as much whenever that is not enough,
up to 64 MiB. This saves copies and page faults while the string grows, but the returned string can have a much larger
`capacity()` than `size()`. Call `shrink_to_fit()` on a result that is kept for long.
Floating-point numbers are written with the fewest digits that read back as the same value (for `#!cpp double`; see
[number handling](../../features/types/number_handling.md#number-serialization)).
@@ -106,3 +110,4 @@ Binary values are serialized as an object containing two keys:
- Error handler `keep` added in version 3.13.0.
- Doubles are written with the shortest digits (Żmij instead of Grisu2) since version 3.13.0; about 0.1% of doubles are
written differently, most of them with fewer digits.
- The result reserves capacity for outputs of 512 characters or more since version 3.13.0.