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

+1 -1
View File
@@ -2766,7 +2766,7 @@ public:
const error_handler_t error_handler = error_handler_t::strict) const
{
string_t result;
detail::output_string_adapter<char, string_t> string_adapter(result);
detail::output_dump_string_adapter<char, string_t> string_adapter(result);
if (indent >= 0)
{