Stop dump() from heap-allocating its output adapter per call (#5449)

* Stop dump() from heap-allocating its output adapter per call

The serializer held its output sink as output_adapter_t<char>
(a std::shared_ptr<output_adapter_protocol<char>>), which dump() and
operator<< built via make_shared -- one heap allocation per call for a
sink that only wraps a reference to the caller's string or stream.

Hold the sink as a non-owning output_adapter_protocol<char>* instead and
construct the concrete adapter on the stack at the call site. The write
path (o->write_characters) is unchanged, so output is byte-for-byte
identical; a compact dump() of a small object drops from 2 heap
allocations to 1 (only the returned string remains), ~3% faster.

Completes the per-call allocation cleanup on this branch, which already
removed the indent_string buffer (both were reported in #5413).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1oJ2ggRHS37zeVe94QTA1
Signed-off-by: Claude <noreply@anthropic.com>

* Take the output adapter by reference at the serializer ctor

Per review: the serializer still holds the adapter as a non-owning
pointer, but the constructor now takes output_adapter_protocol<char>&
and takes its address internally, so every call site passes a
reference. A reference cannot be null and reads as a borrow, which
makes the lifetime contract harder to get wrong than handing over a
raw pointer. The stored member and the write path are unchanged.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XAYM1qhSA2FDaDcGfPW3fG
Signed-off-by: Niels Lohmann <mail@nlohmann.me>

---------

Signed-off-by: Claude <noreply@anthropic.com>
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Niels Lohmann
2026-09-11 08:22:50 +02:00
committed by GitHub
co-authored by Claude
parent ff80ed3295
commit da42a627dc
4 changed files with 24 additions and 17 deletions
+5 -3
View File
@@ -1343,16 +1343,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
const error_handler_t error_handler = error_handler_t::strict) const
{
string_t result;
detail::output_string_adapter<char, string_t> string_adapter(result);
if (indent >= 0)
{
serializer s(detail::output_adapter<char, string_t>(result), indent_char,
serializer s(string_adapter, indent_char,
true, ensure_ascii, static_cast<std::size_t>(indent), error_handler);
s.dump(*this);
}
else
{
serializer s(detail::output_adapter<char, string_t>(result), indent_char,
serializer s(string_adapter, indent_char,
false, ensure_ascii, 0, error_handler);
s.dump(*this);
}
@@ -4083,7 +4084,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
o.width(0);
// do the actual serialization
serializer s(detail::output_adapter<char>(o), o.fill(),
detail::output_stream_adapter<char> stream_adapter(o);
serializer s(stream_adapter, o.fill(),
pretty_print, false, static_cast<std::size_t>(indentation));
s.dump(j);
return o;