Compare commits

..
Author SHA1 Message Date
Niels Lohmann 072ee2af83 Collect up to 64 members of an ordered_json object on the stack
The DOM parser collects the members of an ordered_json object on a stack
and moves them into a vector of the exact size at the closing brace. Objects
with more than 32 members were spilled into a growing vector, which then
took the linear-search operator[] until the key index starts at 64 members.
Collecting up to 64 members closes that gap: an object either fits on the
stack or is spilled straight into the indexed path.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-11-29 11:31:47 +01:00
6 changed files with 18 additions and 201 deletions

No files matched your search

+3 -2
View File
@@ -1322,8 +1322,9 @@ class json_sax_dom_parser : public json_sax_dom_error_state
}
/// the number of members an object collects on the stack before it
/// switches to storing them directly
static constexpr std::size_t object_buffer_limit = 32;
/// switches to storing them directly; this is the size from which the
/// stored object looks up its keys in a hash table (key_index_threshold)
static constexpr std::size_t object_buffer_limit = 64;
void reserve_members()
{
+5 -84
View File
@@ -10,17 +10,8 @@
#include <algorithm> // max
#include <cstddef> // size_t
#include <cstdint> // uint64_t
#ifndef JSON_NO_IO
#include <cstdio> // FILE, fread, ftell
#if defined(_WIN32)
#include <stdio.h> // _fileno, _ftelli64 // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers)
#include <sys/types.h> // (for sys/stat.h)
#include <sys/stat.h> // _fstat64, _S_IFMT, _S_IFREG
#elif defined(__unix__) || defined(__APPLE__)
#include <stdio.h> // fileno // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers)
#include <sys/stat.h> // fstat, S_ISREG
#endif
#include <cstdio> // FILE, fread
#endif
#include <istream> // istream, streambuf
#include <string> // basic_string, char_traits, string
@@ -140,26 +131,6 @@ adapter_input<typename std::decay<Adapter>::type> make_adapter_input(Adapter&& i
return {std::forward<Adapter>(ia)};
}
/// the size of a read block, and the least room offered to a read: one page,
/// the usual size of a filesystem block. It is a conventional size, not a tuned
/// one: it only has to be large enough that a read is never tiny and that the
/// read which finds the end of the input needs no regrowth of the string
constexpr std::size_t read_block_size() noexcept
{
return 4096;
}
/// make room for at least n characters in buf: never asks for less than its
/// capacity, because before C++20 such a request may shrink the string (as
/// libstdc++ before GCC 11 does) and move its characters
inline void reserve_at_least(std::string& buf, std::size_t n)
{
if (n > buf.capacity())
{
buf.reserve(n);
}
}
/// append the rest of a stream: its characters need no conversion, so they are
/// read in blocks from the stream buffer, straight into buf (reusing its
/// capacity). As the library's stream adapter, this reads the stream buffer
@@ -171,20 +142,10 @@ inline void append_stream(std::string& buf, std::istream& is)
{
return;
}
// room for the rest when the stream buffer knows how much is left (for a
// file with libstdc++, the rest of the file), and for the read that finds
// the end: the input is then read in one block, without a reallocation and
// copy at each doubling of the string (only a hint: the loop below reads
// what is there)
const std::streamsize rest = sb->in_avail();
if (rest > 0 && static_cast<std::size_t>(rest) < buf.max_size() - buf.size() - read_block_size())
{
reserve_at_least(buf, buf.size() + static_cast<std::size_t>(rest) + read_block_size());
}
for (;;)
{
const std::size_t used = buf.size();
const std::size_t room = (std::max)(buf.capacity() - used, read_block_size());
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
buf.resize(used + room);
const std::streamsize got = sb->sgetn(&buf[used], static_cast<std::streamsize>(room));
buf.resize(used + static_cast<std::size_t>((std::max)(got, std::streamsize{0})));
@@ -207,55 +168,15 @@ struct is_c_stream
{};
#ifndef JSON_NO_IO
/// the number of bytes left in a C stream that is a regular file (from its
/// size and position), 0 for anything else and on other systems: a hint for
/// the room to reserve; f is not null
inline std::uint64_t file_rest(std::FILE* f) noexcept
{
#if defined(_WIN32)
struct _stat64 st {};
if (::_fstat64(_fileno(f), &st) == 0 && (st.st_mode & _S_IFMT) == _S_IFREG)
{
const auto here = ::_ftelli64(f);
if (here >= 0 && st.st_size > here)
{
return static_cast<std::uint64_t>(st.st_size - here);
}
}
#elif defined(__unix__) || defined(__APPLE__)
struct stat st {};
if (::fstat(fileno(f), &st) == 0 && S_ISREG(st.st_mode))
{
const auto here = std::ftell(f);
if (here >= 0 && st.st_size > here)
{
return static_cast<std::uint64_t>(st.st_size - here);
}
}
#else
static_cast<void>(f);
#endif
return 0;
}
/// append the rest of a C stream: in blocks with fread, not character by
/// character (the library's file adapter reads with fgetc), and in one block
/// when it is a regular file of known size; f is not null
/// character (the library's file adapter reads with fgetc);
/// f is not null
inline void append_file(std::string& buf, std::FILE* f)
{
// room for the rest of a regular file and the byte that shows its end:
// the file is then read in one block, without a reallocation and copy at
// each doubling of the string (only a hint: the loop below reads what is
// there)
const std::uint64_t rest = file_rest(f);
if (rest > 0 && rest < buf.max_size() - buf.size() - 1)
{
reserve_at_least(buf, buf.size() + static_cast<std::size_t>(rest) + 1);
}
for (;;)
{
const std::size_t used = buf.size();
const std::size_t room = (std::max)(buf.capacity() - used, read_block_size());
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
buf.resize(used + room);
const std::size_t got = std::fread(&buf[used], 1, room, f);
buf.resize(used + got);
+3 -2
View File
@@ -17540,8 +17540,9 @@ class json_sax_dom_parser : public json_sax_dom_error_state
}
/// the number of members an object collects on the stack before it
/// switches to storing them directly
static constexpr std::size_t object_buffer_limit = 32;
/// switches to storing them directly; this is the size from which the
/// stored object looks up its keys in a hash table (key_index_threshold)
static constexpr std::size_t object_buffer_limit = 64;
void reserve_members()
{
+5 -84
View File
@@ -7956,17 +7956,8 @@ NLOHMANN_JSON_NAMESPACE_END
#include <algorithm> // max
#include <cstddef> // size_t
#include <cstdint> // uint64_t
#ifndef JSON_NO_IO
#include <cstdio> // FILE, fread, ftell
#if defined(_WIN32)
#include <stdio.h> // _fileno, _ftelli64 // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers)
#include <sys/types.h> // (for sys/stat.h)
#include <sys/stat.h> // _fstat64, _S_IFMT, _S_IFREG
#elif defined(__unix__) || defined(__APPLE__)
#include <stdio.h> // fileno // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers)
#include <sys/stat.h> // fstat, S_ISREG
#endif
#include <cstdio> // FILE, fread
#endif
#include <istream> // istream, streambuf
#include <string> // basic_string, char_traits, string
@@ -8087,26 +8078,6 @@ adapter_input<typename std::decay<Adapter>::type> make_adapter_input(Adapter&& i
return {std::forward<Adapter>(ia)};
}
/// the size of a read block, and the least room offered to a read: one page,
/// the usual size of a filesystem block. It is a conventional size, not a tuned
/// one: it only has to be large enough that a read is never tiny and that the
/// read which finds the end of the input needs no regrowth of the string
constexpr std::size_t read_block_size() noexcept
{
return 4096;
}
/// make room for at least n characters in buf: never asks for less than its
/// capacity, because before C++20 such a request may shrink the string (as
/// libstdc++ before GCC 11 does) and move its characters
inline void reserve_at_least(std::string& buf, std::size_t n)
{
if (n > buf.capacity())
{
buf.reserve(n);
}
}
/// append the rest of a stream: its characters need no conversion, so they are
/// read in blocks from the stream buffer, straight into buf (reusing its
/// capacity). As the library's stream adapter, this reads the stream buffer
@@ -8118,20 +8089,10 @@ inline void append_stream(std::string& buf, std::istream& is)
{
return;
}
// room for the rest when the stream buffer knows how much is left (for a
// file with libstdc++, the rest of the file), and for the read that finds
// the end: the input is then read in one block, without a reallocation and
// copy at each doubling of the string (only a hint: the loop below reads
// what is there)
const std::streamsize rest = sb->in_avail();
if (rest > 0 && static_cast<std::size_t>(rest) < buf.max_size() - buf.size() - read_block_size())
{
reserve_at_least(buf, buf.size() + static_cast<std::size_t>(rest) + read_block_size());
}
for (;;)
{
const std::size_t used = buf.size();
const std::size_t room = (std::max)(buf.capacity() - used, read_block_size());
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
buf.resize(used + room);
const std::streamsize got = sb->sgetn(&buf[used], static_cast<std::streamsize>(room));
buf.resize(used + static_cast<std::size_t>((std::max)(got, std::streamsize{0})));
@@ -8154,55 +8115,15 @@ struct is_c_stream
{};
#ifndef JSON_NO_IO
/// the number of bytes left in a C stream that is a regular file (from its
/// size and position), 0 for anything else and on other systems: a hint for
/// the room to reserve; f is not null
inline std::uint64_t file_rest(std::FILE* f) noexcept
{
#if defined(_WIN32)
struct _stat64 st {};
if (::_fstat64(_fileno(f), &st) == 0 && (st.st_mode & _S_IFMT) == _S_IFREG)
{
const auto here = ::_ftelli64(f);
if (here >= 0 && st.st_size > here)
{
return static_cast<std::uint64_t>(st.st_size - here);
}
}
#elif defined(__unix__) || defined(__APPLE__)
struct stat st {};
if (::fstat(fileno(f), &st) == 0 && S_ISREG(st.st_mode))
{
const auto here = std::ftell(f);
if (here >= 0 && st.st_size > here)
{
return static_cast<std::uint64_t>(st.st_size - here);
}
}
#else
static_cast<void>(f);
#endif
return 0;
}
/// append the rest of a C stream: in blocks with fread, not character by
/// character (the library's file adapter reads with fgetc), and in one block
/// when it is a regular file of known size; f is not null
/// character (the library's file adapter reads with fgetc);
/// f is not null
inline void append_file(std::string& buf, std::FILE* f)
{
// room for the rest of a regular file and the byte that shows its end:
// the file is then read in one block, without a reallocation and copy at
// each doubling of the string (only a hint: the loop below reads what is
// there)
const std::uint64_t rest = file_rest(f);
if (rest > 0 && rest < buf.max_size() - buf.size() - 1)
{
reserve_at_least(buf, buf.size() + static_cast<std::size_t>(rest) + 1);
}
for (;;)
{
const std::size_t used = buf.size();
const std::size_t room = (std::max)(buf.capacity() - used, read_block_size());
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
buf.resize(used + room);
const std::size_t got = std::fread(&buf[used], 1, room, f);
buf.resize(used + got);
-27
View File
@@ -535,33 +535,6 @@ TEST_CASE("json_view")
CHECK_THROWS_AS(from_null = json_document::parse(static_cast<std::FILE*>(nullptr)), json::parse_error&);
}
// a regular file is read in one block, from its position to its end
std::FILE* tail_file = std::tmpfile(); // NOLINT(cppcoreguidelines-owning-memory)
if (tail_file != nullptr)
{
const std::string prefix = "garbage ";
CHECK(std::fwrite(prefix.data(), 1, prefix.size(), tail_file) == prefix.size());
CHECK(std::fwrite(text.data(), 1, text.size(), tail_file) == text.size());
CHECK(std::fseek(tail_file, static_cast<long>(prefix.size()), SEEK_SET) == 0);
#if defined(_WIN32) || defined(__unix__) || defined(__APPLE__)
CHECK(nlohmann::detail::view::file_rest(tail_file) == text.size());
#endif
const json_document from_tail = json_document::parse(tail_file);
CHECK(from_tail.source() == text);
CHECK(from_tail.root().materialize() == json::parse(text));
CHECK(nlohmann::detail::view::file_rest(tail_file) == 0);
CHECK(std::fclose(tail_file) == 0); // NOLINT(cppcoreguidelines-owning-memory)
}
// a stream is read from its position to its end as well, in one block
// when its buffer knows how much is left
std::istringstream tail_stream("garbage " + text);
tail_stream.seekg(8);
const json_document from_tail_stream = json_document::parse(tail_stream);
CHECK(from_tail_stream.source() == text);
CHECK(from_tail_stream.root().materialize() == json::parse(text));
CHECK(tail_stream.eof());
// a stream without a buffer is an empty input
std::istream no_buffer(nullptr);
CHECK(json_document::parse(no_buffer, false).is_discarded());
+2 -2
View File
@@ -247,8 +247,8 @@ TEST_CASE("regression test for issue #5673 - ordered_json::emplace with a non-rv
namespace
{
// the sizes around which the DOM parser changes how it stores the elements of
// an array (16) and the members of an object in a vector (32, and 64 for the
// key index)
// an array (16) and the members of an object in a vector (64, which is also
// where the key index starts)
const std::array<std::size_t, 17> container_sizes = {{0, 1, 2, 3, 15, 16, 17, 18, 31, 32, 33, 34, 63, 64, 65, 66, 200}};
template<typename Json>