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

+65 -1
View File
@@ -29739,6 +29739,8 @@ NLOHMANN_JSON_NAMESPACE_END
// #include <nlohmann/detail/macro_scope.hpp>
// #include <nlohmann/detail/meta/cpp_future.hpp>
NLOHMANN_JSON_NAMESPACE_BEGIN
namespace detail
@@ -30114,6 +30116,68 @@ class output_string_adapter : public output_adapter_protocol<CharType>
StringType& str;
};
/*!
@brief output adapter for the string that basic_json::dump() returns
A string that grows by appending reallocates and copies its contents each time
it doubles, and with glibc every new big block is fresh memory that faults in
page by page. Once a single write of at least 512 characters arrives (a full
flush of the serializer's buffer or a long string, so the output is at least
that big), this adapter reserves 1 MiB, and four times as much whenever that is
not enough, until 64 MiB; beyond that the string grows by itself. Smaller
outputs never reserve. The capacity is tracked here, as StringType::capacity()
is not required; a StringType without reserve() just appends.
*/
template<typename CharType, typename StringType>
class output_dump_string_adapter : public output_adapter_protocol<CharType>
{
public:
explicit output_dump_string_adapter(StringType& s) noexcept
: str(s)
{}
void write_character(CharType c) override
{
str.push_back(c);
}
void write_characters(const CharType* s, std::size_t length) override
{
if (JSON_HEDLEY_UNLIKELY(length >= 512))
{
grow(str.size() + length);
}
str.append(s, length);
}
private:
void grow(const std::size_t needed)
{
const std::size_t first_reserve = 1048576; // 1 MiB
const std::size_t last_reserve = 64 * first_reserve;
if (needed > reserved && reserved < last_reserve)
{
const std::size_t next = reserved == 0 ? first_reserve : reserved * 4;
reserved = (std::max)(needed, next);
reserve_string(str, reserved, priority_tag<1> {});
}
}
template<typename String>
static auto reserve_string(String& string, std::size_t n, priority_tag<1> /*unused*/)
-> decltype(string.reserve(n), void())
{
string.reserve(n);
}
template<typename String>
static void reserve_string(String& /*string*/, std::size_t /*n*/, priority_tag<0> /*unused*/) noexcept
{}
StringType& str;
std::size_t reserved = 0;
};
/// @brief output sink forwarding to a type-erased output adapter
///
/// Wraps the polymorphic output_adapter_t so the same binary_writer template can
@@ -42972,7 +43036,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)
{