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

+16
View File
@@ -484,6 +484,22 @@ TEST_CASE("alternative string type")
}
}
TEST_CASE("alternative string type with a big dump")
{
// dump() reserves capacity in the result once the output is big
alt_json j = alt_json::array();
nlohmann::json reference = nlohmann::json::array();
for (int i = 0; i < 300000; ++i)
{
j.push_back(i);
reference.push_back(i);
}
j.push_back(alt_string(600, 'x'));
reference.push_back(std::string(600, 'x'));
const std::string expected = reference.dump(2);
CHECK(j.dump(2) == expected);
}
TEST_CASE("alternative string type with json_view")
{
// regression test for #5994: basic_json_view::dump() called
+36
View File
@@ -800,6 +800,30 @@ class failing_adapter : public nlohmann::detail::output_adapter_protocol<char>
} // namespace
#endif
namespace
{
// a string type with only what the adapter of dump() needs, and no reserve()
struct appending_string
{
std::string str;
std::size_t size() const noexcept
{
return str.size();
}
void push_back(char c)
{
str.push_back(c);
}
void append(const char* s, std::size_t length)
{
str.append(s, length);
}
};
} // namespace
TEST_CASE("output adapters")
{
SECTION("writing a single character")
@@ -825,6 +849,18 @@ TEST_CASE("output adapters")
CHECK(as_string == "ef");
}
SECTION("the adapter of dump() with a string type without reserve()")
{
// dump() reserves capacity for big outputs only if the string type
// has reserve(); without it, the adapter just appends
appending_string as_string {std::string()};
nlohmann::detail::output_dump_string_adapter<char, appending_string> dump_adapter(as_string);
dump_adapter.write_character('g');
const std::string block(600, 'h');
dump_adapter.write_characters(block.data(), block.size());
CHECK(as_string.str == "g" + block);
}
#if !defined(JSON_NOEXCEPTION)
SECTION("a sink that is destroyed with characters left over")
{
+42
View File
@@ -1717,3 +1717,45 @@ TEST_CASE("runs of multi-byte characters are escaped like the reference under en
}
}
}
TEST_CASE("dump() of outputs around the sizes where the result reserves capacity")
{
// the result reserves 1 MiB from the first write of at least 512
// characters, and four times as much whenever that is not enough; the
// output must not depend on it
const std::size_t mib = 1048576;
for (const std::size_t length :
{
std::size_t{0}, std::size_t{1}, std::size_t{509}, std::size_t{510}, std::size_t{511}, std::size_t{512},
std::size_t{513}, std::size_t{1023}, std::size_t{1024}, mib - 3, mib - 2, mib - 1, mib, mib + 1,
(4 * mib) - 2, (4 * mib) + 1
})
{
CAPTURE(length)
const std::string s(length, 'x');
const json j = s;
CHECK(j.dump() == "\"" + s + "\"");
}
// many writes of the serializer's buffer, compact and indented
for (const std::size_t count :
{
std::size_t{0}, std::size_t{1}, std::size_t{100}, std::size_t{200000}
})
{
CAPTURE(count)
json j = json::array();
std::string compact = "[";
std::string indented = "[";
for (std::size_t i = 0; i < count; ++i)
{
j.push_back(i);
compact += (i == 0 ? "" : ",") + std::to_string(i);
indented += (i == 0 ? "\n " : ",\n ") + std::to_string(i);
}
compact += "]";
indented += count == 0 ? "]" : "\n]";
CHECK(j.dump() == compact);
CHECK(j.dump(1) == indented);
}
}