Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a1b49b720a | ||
|
|
58602b043b |
No files matched your search
@@ -628,11 +628,7 @@ run_found:
|
|||||||
// covers the escapes of a typical document without regrowing it
|
// covers the escapes of a typical document without regrowing it
|
||||||
wanted = (std::max)(wanted, (std::min)((static_cast<std::size_t>(m_end - m_begin) / 4) + 16, std::size_t{4096}));
|
wanted = (std::max)(wanted, (std::min)((static_cast<std::size_t>(m_end - m_begin) / 4) + 16, std::size_t{4096}));
|
||||||
}
|
}
|
||||||
const std::size_t size = (std::max)(m_doc.arena.size() * 2, wanted);
|
m_doc.arena.resize((std::max)(m_doc.arena.size() * 2, wanted));
|
||||||
// (reserved before the resize writes it, so that a large arena gets huge pages)
|
|
||||||
m_doc.arena.reserve(size);
|
|
||||||
document_data::advise_huge(m_doc.arena.data(), m_doc.arena.capacity());
|
|
||||||
m_doc.arena.resize(size);
|
|
||||||
m_arena_write = m_doc.arena.data() + used;
|
m_arena_write = m_doc.arena.data() + used;
|
||||||
m_arena_end = m_doc.arena.data() + m_doc.arena.size();
|
m_arena_end = m_doc.arena.data() + m_doc.arena.size();
|
||||||
}
|
}
|
||||||
@@ -1327,17 +1323,6 @@ template<typename FloatType, bool NulIsEnd>
|
|||||||
inline bool build(document_data& d, const char* src, std::size_t size, bool comments, bool trailing_commas, bool sentinel, bool validate_only,
|
inline bool build(document_data& d, const char* src, std::size_t size, bool comments, bool trailing_commas, bool sentinel, bool validate_only,
|
||||||
parse_failure& failure)
|
parse_failure& failure)
|
||||||
{
|
{
|
||||||
#if defined(__linux__) && defined(MADV_HUGEPAGE)
|
|
||||||
if (!(validate_only && !comments && !trailing_commas))
|
|
||||||
{
|
|
||||||
// a large index is reserved here, with huge pages (see document_data::reserve_huge())
|
|
||||||
const std::size_t n = estimate_nodes(src, size);
|
|
||||||
if (NLOHMANN_VIEW_UNLIKELY(n >= document_data::huge_min_bytes() / sizeof(node)))
|
|
||||||
{
|
|
||||||
d.reserve_huge(n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (validate_only && !comments && !trailing_commas)
|
if (validate_only && !comments && !trailing_commas)
|
||||||
{
|
{
|
||||||
// accept() of plain JSON: nothing is stored. The other option sets
|
// accept() of plain JSON: nothing is stored. The other option sets
|
||||||
|
|||||||
@@ -8,9 +8,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#if defined(__linux__)
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#endif
|
|
||||||
#include <array> // array
|
#include <array> // array
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <cstdint> // uint8_t, uint32_t
|
#include <cstdint> // uint8_t, uint32_t
|
||||||
@@ -182,52 +179,6 @@ struct document_data
|
|||||||
NLOHMANN_VIEW_THROW(std::bad_alloc());
|
NLOHMANN_VIEW_THROW(std::bad_alloc());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// a large block: transparent huge pages for its 2 MiB-aligned interior, so that writing it takes one page
|
|
||||||
/// fault per 2 MiB instead of one per 4 KiB (Linux with THP in "madvise" mode, the default of many
|
|
||||||
/// distributions; a no-op otherwise)
|
|
||||||
static NLOHMANN_VIEW_NOINLINE void advise_huge_now(const void* p, std::size_t size) noexcept
|
|
||||||
{
|
|
||||||
#if defined(__linux__) && defined(MADV_HUGEPAGE)
|
|
||||||
const std::uintptr_t huge = std::uintptr_t {2} << 20u;
|
|
||||||
const auto first = (reinterpret_cast<std::uintptr_t>(p) + huge - 1) & ~(huge - 1); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
|
||||||
const auto last = (reinterpret_cast<std::uintptr_t>(p) + size) & ~(huge - 1); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
|
||||||
if (last > first)
|
|
||||||
{
|
|
||||||
static_cast<void>(::madvise(reinterpret_cast<void*>(first), last - first, MADV_HUGEPAGE)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast,performance-no-int-to-ptr)
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
static_cast<void>(p);
|
|
||||||
static_cast<void>(size);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
/// the size from which advise_huge() asks for huge pages
|
|
||||||
static constexpr std::size_t huge_min_bytes() noexcept
|
|
||||||
{
|
|
||||||
return std::size_t{4} << 20u;
|
|
||||||
}
|
|
||||||
|
|
||||||
static NLOHMANN_VIEW_ALWAYS_INLINE void advise_huge(const void* p, std::size_t size) noexcept
|
|
||||||
{
|
|
||||||
if (NLOHMANN_VIEW_UNLIKELY(size >= huge_min_bytes()))
|
|
||||||
{
|
|
||||||
advise_huge_now(p, size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// reserve() for a large index, before the parse loop starts (which then finds the room it asks for): with
|
|
||||||
/// huge pages where advise_huge() gets them. Out of line and outside the parse loop, so that the loop's code
|
|
||||||
/// stays as it is.
|
|
||||||
NLOHMANN_VIEW_NOINLINE void reserve_huge(std::size_t n)
|
|
||||||
{
|
|
||||||
const node* const before = nodes;
|
|
||||||
reserve(n);
|
|
||||||
if (nodes != before)
|
|
||||||
{
|
|
||||||
advise_huge_now(nodes, node_capacity * sizeof(node));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// make room for n nodes; keeps the first node_count nodes (throws
|
/// make room for n nodes; keeps the first node_count nodes (throws
|
||||||
/// std::bad_alloc for a count that does not fit the address space,
|
/// std::bad_alloc for a count that does not fit the address space,
|
||||||
/// instead of wrapping around in n * sizeof(node))
|
/// instead of wrapping around in n * sizeof(node))
|
||||||
|
|||||||
@@ -11,7 +11,15 @@
|
|||||||
#include <algorithm> // max
|
#include <algorithm> // max
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#ifndef JSON_NO_IO
|
#ifndef JSON_NO_IO
|
||||||
#include <cstdio> // FILE, fread
|
#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
|
||||||
#endif
|
#endif
|
||||||
#include <istream> // istream, streambuf
|
#include <istream> // istream, streambuf
|
||||||
#include <string> // basic_string, char_traits, string
|
#include <string> // basic_string, char_traits, string
|
||||||
@@ -131,6 +139,15 @@ adapter_input<typename std::decay<Adapter>::type> make_adapter_input(Adapter&& i
|
|||||||
return {std::forward<Adapter>(ia)};
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/// append the rest of a stream: its characters need no conversion, so they are
|
/// 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
|
/// read in blocks from the stream buffer, straight into buf (reusing its
|
||||||
/// capacity). As the library's stream adapter, this reads the stream buffer
|
/// capacity). As the library's stream adapter, this reads the stream buffer
|
||||||
@@ -142,10 +159,20 @@ inline void append_stream(std::string& buf, std::istream& is)
|
|||||||
{
|
{
|
||||||
return;
|
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())
|
||||||
|
{
|
||||||
|
buf.reserve(buf.size() + static_cast<std::size_t>(rest) + read_block_size());
|
||||||
|
}
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
const std::size_t used = buf.size();
|
const std::size_t used = buf.size();
|
||||||
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
|
const std::size_t room = (std::max)(buf.capacity() - used, read_block_size());
|
||||||
buf.resize(used + room);
|
buf.resize(used + room);
|
||||||
const std::streamsize got = sb->sgetn(&buf[used], static_cast<std::streamsize>(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})));
|
buf.resize(used + static_cast<std::size_t>((std::max)(got, std::streamsize{0})));
|
||||||
@@ -168,15 +195,55 @@ struct is_c_stream
|
|||||||
{};
|
{};
|
||||||
|
|
||||||
#ifndef JSON_NO_IO
|
#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 unsigned long long 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 long long here = ::_ftelli64(f);
|
||||||
|
if (here >= 0 && st.st_size > here)
|
||||||
|
{
|
||||||
|
return static_cast<unsigned long long>(st.st_size - here);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#elif defined(__unix__) || defined(__APPLE__)
|
||||||
|
struct stat st {};
|
||||||
|
if (::fstat(fileno(f), &st) == 0 && S_ISREG(st.st_mode))
|
||||||
|
{
|
||||||
|
const long here = std::ftell(f);
|
||||||
|
if (here >= 0 && st.st_size > here)
|
||||||
|
{
|
||||||
|
return static_cast<unsigned long long>(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
|
/// append the rest of a C stream: in blocks with fread, not character by
|
||||||
/// character (the library's file adapter reads with fgetc);
|
/// character (the library's file adapter reads with fgetc), and in one block
|
||||||
/// f is not null
|
/// when it is a regular file of known size; f is not null
|
||||||
inline void append_file(std::string& buf, std::FILE* f)
|
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 unsigned long long rest = file_rest(f);
|
||||||
|
if (rest > 0 && rest < buf.max_size() - buf.size() - 1)
|
||||||
|
{
|
||||||
|
buf.reserve(buf.size() + static_cast<std::size_t>(rest) + 1);
|
||||||
|
}
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
const std::size_t used = buf.size();
|
const std::size_t used = buf.size();
|
||||||
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
|
const std::size_t room = (std::max)(buf.capacity() - used, read_block_size());
|
||||||
buf.resize(used + room);
|
buf.resize(used + room);
|
||||||
const std::size_t got = std::fread(&buf[used], 1, room, f);
|
const std::size_t got = std::fread(&buf[used], 1, room, f);
|
||||||
buf.resize(used + got);
|
buf.resize(used + got);
|
||||||
|
|||||||
Generated
+73
-70
@@ -102,9 +102,6 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(__linux__)
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#endif
|
|
||||||
#include <array> // array
|
#include <array> // array
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <cstdint> // uint8_t, uint32_t
|
#include <cstdint> // uint8_t, uint32_t
|
||||||
@@ -526,52 +523,6 @@ struct document_data
|
|||||||
NLOHMANN_VIEW_THROW(std::bad_alloc());
|
NLOHMANN_VIEW_THROW(std::bad_alloc());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// a large block: transparent huge pages for its 2 MiB-aligned interior, so that writing it takes one page
|
|
||||||
/// fault per 2 MiB instead of one per 4 KiB (Linux with THP in "madvise" mode, the default of many
|
|
||||||
/// distributions; a no-op otherwise)
|
|
||||||
static NLOHMANN_VIEW_NOINLINE void advise_huge_now(const void* p, std::size_t size) noexcept
|
|
||||||
{
|
|
||||||
#if defined(__linux__) && defined(MADV_HUGEPAGE)
|
|
||||||
const std::uintptr_t huge = std::uintptr_t {2} << 20u;
|
|
||||||
const auto first = (reinterpret_cast<std::uintptr_t>(p) + huge - 1) & ~(huge - 1); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
|
||||||
const auto last = (reinterpret_cast<std::uintptr_t>(p) + size) & ~(huge - 1); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
|
||||||
if (last > first)
|
|
||||||
{
|
|
||||||
static_cast<void>(::madvise(reinterpret_cast<void*>(first), last - first, MADV_HUGEPAGE)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast,performance-no-int-to-ptr)
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
static_cast<void>(p);
|
|
||||||
static_cast<void>(size);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
/// the size from which advise_huge() asks for huge pages
|
|
||||||
static constexpr std::size_t huge_min_bytes() noexcept
|
|
||||||
{
|
|
||||||
return std::size_t{4} << 20u;
|
|
||||||
}
|
|
||||||
|
|
||||||
static NLOHMANN_VIEW_ALWAYS_INLINE void advise_huge(const void* p, std::size_t size) noexcept
|
|
||||||
{
|
|
||||||
if (NLOHMANN_VIEW_UNLIKELY(size >= huge_min_bytes()))
|
|
||||||
{
|
|
||||||
advise_huge_now(p, size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// reserve() for a large index, before the parse loop starts (which then finds the room it asks for): with
|
|
||||||
/// huge pages where advise_huge() gets them. Out of line and outside the parse loop, so that the loop's code
|
|
||||||
/// stays as it is.
|
|
||||||
NLOHMANN_VIEW_NOINLINE void reserve_huge(std::size_t n)
|
|
||||||
{
|
|
||||||
const node* const before = nodes;
|
|
||||||
reserve(n);
|
|
||||||
if (nodes != before)
|
|
||||||
{
|
|
||||||
advise_huge_now(nodes, node_capacity * sizeof(node));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// make room for n nodes; keeps the first node_count nodes (throws
|
/// make room for n nodes; keeps the first node_count nodes (throws
|
||||||
/// std::bad_alloc for a count that does not fit the address space,
|
/// std::bad_alloc for a count that does not fit the address space,
|
||||||
/// instead of wrapping around in n * sizeof(node))
|
/// instead of wrapping around in n * sizeof(node))
|
||||||
@@ -2013,11 +1964,7 @@ run_found:
|
|||||||
// covers the escapes of a typical document without regrowing it
|
// covers the escapes of a typical document without regrowing it
|
||||||
wanted = (std::max)(wanted, (std::min)((static_cast<std::size_t>(m_end - m_begin) / 4) + 16, std::size_t{4096}));
|
wanted = (std::max)(wanted, (std::min)((static_cast<std::size_t>(m_end - m_begin) / 4) + 16, std::size_t{4096}));
|
||||||
}
|
}
|
||||||
const std::size_t size = (std::max)(m_doc.arena.size() * 2, wanted);
|
m_doc.arena.resize((std::max)(m_doc.arena.size() * 2, wanted));
|
||||||
// (reserved before the resize writes it, so that a large arena gets huge pages)
|
|
||||||
m_doc.arena.reserve(size);
|
|
||||||
document_data::advise_huge(m_doc.arena.data(), m_doc.arena.capacity());
|
|
||||||
m_doc.arena.resize(size);
|
|
||||||
m_arena_write = m_doc.arena.data() + used;
|
m_arena_write = m_doc.arena.data() + used;
|
||||||
m_arena_end = m_doc.arena.data() + m_doc.arena.size();
|
m_arena_end = m_doc.arena.data() + m_doc.arena.size();
|
||||||
}
|
}
|
||||||
@@ -2712,17 +2659,6 @@ template<typename FloatType, bool NulIsEnd>
|
|||||||
inline bool build(document_data& d, const char* src, std::size_t size, bool comments, bool trailing_commas, bool sentinel, bool validate_only,
|
inline bool build(document_data& d, const char* src, std::size_t size, bool comments, bool trailing_commas, bool sentinel, bool validate_only,
|
||||||
parse_failure& failure)
|
parse_failure& failure)
|
||||||
{
|
{
|
||||||
#if defined(__linux__) && defined(MADV_HUGEPAGE)
|
|
||||||
if (!(validate_only && !comments && !trailing_commas))
|
|
||||||
{
|
|
||||||
// a large index is reserved here, with huge pages (see document_data::reserve_huge())
|
|
||||||
const std::size_t n = estimate_nodes(src, size);
|
|
||||||
if (NLOHMANN_VIEW_UNLIKELY(n >= document_data::huge_min_bytes() / sizeof(node)))
|
|
||||||
{
|
|
||||||
d.reserve_huge(n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (validate_only && !comments && !trailing_commas)
|
if (validate_only && !comments && !trailing_commas)
|
||||||
{
|
{
|
||||||
// accept() of plain JSON: nothing is stored. The other option sets
|
// accept() of plain JSON: nothing is stored. The other option sets
|
||||||
@@ -8021,7 +7957,15 @@ NLOHMANN_JSON_NAMESPACE_END
|
|||||||
#include <algorithm> // max
|
#include <algorithm> // max
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#ifndef JSON_NO_IO
|
#ifndef JSON_NO_IO
|
||||||
#include <cstdio> // FILE, fread
|
#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
|
||||||
#endif
|
#endif
|
||||||
#include <istream> // istream, streambuf
|
#include <istream> // istream, streambuf
|
||||||
#include <string> // basic_string, char_traits, string
|
#include <string> // basic_string, char_traits, string
|
||||||
@@ -8142,6 +8086,15 @@ adapter_input<typename std::decay<Adapter>::type> make_adapter_input(Adapter&& i
|
|||||||
return {std::forward<Adapter>(ia)};
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/// append the rest of a stream: its characters need no conversion, so they are
|
/// 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
|
/// read in blocks from the stream buffer, straight into buf (reusing its
|
||||||
/// capacity). As the library's stream adapter, this reads the stream buffer
|
/// capacity). As the library's stream adapter, this reads the stream buffer
|
||||||
@@ -8153,10 +8106,20 @@ inline void append_stream(std::string& buf, std::istream& is)
|
|||||||
{
|
{
|
||||||
return;
|
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())
|
||||||
|
{
|
||||||
|
buf.reserve(buf.size() + static_cast<std::size_t>(rest) + read_block_size());
|
||||||
|
}
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
const std::size_t used = buf.size();
|
const std::size_t used = buf.size();
|
||||||
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
|
const std::size_t room = (std::max)(buf.capacity() - used, read_block_size());
|
||||||
buf.resize(used + room);
|
buf.resize(used + room);
|
||||||
const std::streamsize got = sb->sgetn(&buf[used], static_cast<std::streamsize>(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})));
|
buf.resize(used + static_cast<std::size_t>((std::max)(got, std::streamsize{0})));
|
||||||
@@ -8179,15 +8142,55 @@ struct is_c_stream
|
|||||||
{};
|
{};
|
||||||
|
|
||||||
#ifndef JSON_NO_IO
|
#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 unsigned long long 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 long long here = ::_ftelli64(f);
|
||||||
|
if (here >= 0 && st.st_size > here)
|
||||||
|
{
|
||||||
|
return static_cast<unsigned long long>(st.st_size - here);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#elif defined(__unix__) || defined(__APPLE__)
|
||||||
|
struct stat st {};
|
||||||
|
if (::fstat(fileno(f), &st) == 0 && S_ISREG(st.st_mode))
|
||||||
|
{
|
||||||
|
const long here = std::ftell(f);
|
||||||
|
if (here >= 0 && st.st_size > here)
|
||||||
|
{
|
||||||
|
return static_cast<unsigned long long>(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
|
/// append the rest of a C stream: in blocks with fread, not character by
|
||||||
/// character (the library's file adapter reads with fgetc);
|
/// character (the library's file adapter reads with fgetc), and in one block
|
||||||
/// f is not null
|
/// when it is a regular file of known size; f is not null
|
||||||
inline void append_file(std::string& buf, std::FILE* f)
|
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 unsigned long long rest = file_rest(f);
|
||||||
|
if (rest > 0 && rest < buf.max_size() - buf.size() - 1)
|
||||||
|
{
|
||||||
|
buf.reserve(buf.size() + static_cast<std::size_t>(rest) + 1);
|
||||||
|
}
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
const std::size_t used = buf.size();
|
const std::size_t used = buf.size();
|
||||||
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
|
const std::size_t room = (std::max)(buf.capacity() - used, read_block_size());
|
||||||
buf.resize(used + room);
|
buf.resize(used + room);
|
||||||
const std::size_t got = std::fread(&buf[used], 1, room, f);
|
const std::size_t got = std::fread(&buf[used], 1, room, f);
|
||||||
buf.resize(used + got);
|
buf.resize(used + got);
|
||||||
|
|||||||
@@ -535,6 +535,33 @@ TEST_CASE("json_view")
|
|||||||
CHECK_THROWS_AS(from_null = json_document::parse(static_cast<std::FILE*>(nullptr)), json::parse_error&);
|
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
|
// a stream without a buffer is an empty input
|
||||||
std::istream no_buffer(nullptr);
|
std::istream no_buffer(nullptr);
|
||||||
CHECK(json_document::parse(no_buffer, false).is_discarded());
|
CHECK(json_document::parse(no_buffer, false).is_discarded());
|
||||||
|
|||||||
@@ -598,54 +598,3 @@ TEST_CASE("json_view large objects")
|
|||||||
CHECK(d.memory_usage() >= with_index / 2);
|
CHECK(d.memory_usage() >= with_index / 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("json_view large indexes and arenas")
|
|
||||||
{
|
|
||||||
// inputs whose index (and decoded strings) take several MiB: on Linux, these blocks are reserved before the
|
|
||||||
// parse loop and get transparent huge pages (document_data::reserve_huge(), advise_huge())
|
|
||||||
SECTION("an index of more than 4 MiB")
|
|
||||||
{
|
|
||||||
std::string text = "[";
|
|
||||||
for (int i = 0; i < 400000; ++i)
|
|
||||||
{
|
|
||||||
text += (i != 0 ? "," : "") + std::to_string(i);
|
|
||||||
}
|
|
||||||
text += ']';
|
|
||||||
json_document d = json_document::parse(text);
|
|
||||||
CHECK(d.node_count() == 400001);
|
|
||||||
CHECK(d.root().size() == 400000);
|
|
||||||
CHECK(d.root()[0].get<int>() == 0);
|
|
||||||
CHECK(d.root()[399999].get<int>() == 399999);
|
|
||||||
CHECK(json_document::accept(text));
|
|
||||||
|
|
||||||
// a reused document: a small input, then the large one again
|
|
||||||
d.read(std::string("[1,2]"));
|
|
||||||
CHECK(d.root().size() == 2);
|
|
||||||
d.read(text);
|
|
||||||
CHECK(d.root()[123456].get<int>() == 123456);
|
|
||||||
CHECK(d.root() == json::parse(text));
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("decoded strings of more than 4 MiB")
|
|
||||||
{
|
|
||||||
// 2,000 strings of 3,000 bytes, each with escapes, so that every one is decoded into the arena
|
|
||||||
std::string s;
|
|
||||||
for (int i = 0; i < 1000; ++i)
|
|
||||||
{
|
|
||||||
s += "ab\\n";
|
|
||||||
}
|
|
||||||
std::string text = "[";
|
|
||||||
for (int i = 0; i < 2000; ++i)
|
|
||||||
{
|
|
||||||
text += (i != 0 ? ",\"" : "\"") + std::to_string(i) + s + "\"";
|
|
||||||
}
|
|
||||||
text += ']';
|
|
||||||
const json_document d = json_document::parse(text);
|
|
||||||
REQUIRE(d.root().size() == 2000);
|
|
||||||
const json expected = json::parse(text);
|
|
||||||
CHECK(d.root()[0].get<std::string>() == expected[0].get<std::string>());
|
|
||||||
CHECK(d.root()[1999].get<std::string>() == expected[1999].get<std::string>());
|
|
||||||
CHECK(d.root()[1999].get<std::string>().size() == 4 + 3000);
|
|
||||||
CHECK(d.root() == expected);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user