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:
7 files changed
+228
-2
No files matched your search
@@ -25,6 +25,7 @@
|
||||
#endif // JSON_NO_IO
|
||||
|
||||
#include <nlohmann/detail/macro_scope.hpp>
|
||||
#include <nlohmann/detail/meta/cpp_future.hpp>
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
namespace detail
|
||||
@@ -400,6 +401,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
|
||||
|
||||
Reference in new issue
Block a user