Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5612cc3967 | ||
|
|
62b238dce8 |
No files matched your search
@@ -223,6 +223,157 @@ inline std::size_t validate_one_utf8(const unsigned char* data, std::size_t avai
|
||||
return 0; // invalid, incomplete, or must be diagnosed by the byte path
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the transition table of the UTF-8 DFA of utf8_dfa_run() and utf8_dfa_prefix()
|
||||
|
||||
They accept exactly what validate_one_utf8() accepts. This is a shift-based
|
||||
DFA (after Per Vognsen; see also https://orlp.net/blog/utf8-dfa/): a state is
|
||||
6 times its number, and table[b] holds, for every state s, the next state on
|
||||
byte b at bit offset s. One step is a load and a shift without a branch on the
|
||||
byte. On CJK text, it made the lexer and the serializer without SSE2/NEON 30%
|
||||
faster and the BON8 reader 15% faster than a loop over validate_one_utf8()
|
||||
(Apple M, Apple Clang).
|
||||
|
||||
The states are 0 (error, absorbing), 6 (between sequences), 12/18/24 (1/2/3
|
||||
continuation bytes of 0x80-0xBF missing), and the states after the lead bytes
|
||||
that restrict the second byte: 30 (0xE0: 0xA0-0xBF), 36 (0xED: 0x80-0x9F), 42
|
||||
(0xF0: 0x90-0xBF), and 48 (0xF4: 0x80-0x8F). The test "UTF-8 DFA" in
|
||||
unit-class_lexer.cpp checks every entry of the table.
|
||||
*/
|
||||
inline const std::uint64_t* utf8_dfa_table() noexcept
|
||||
{
|
||||
static const std::array<std::uint64_t, 256> table =
|
||||
{
|
||||
{
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000780ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000900ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000A80ull, 0x0000000000000600ull, 0x0000000000000600ull, 0x0000000000000600ull,
|
||||
0x0000000000000C00ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
}
|
||||
};
|
||||
return table.data();
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief validate a run of non-ASCII bytes as UTF-8
|
||||
|
||||
Validates [p, e) up to the first byte below 0x80 (see utf8_dfa_table()).
|
||||
@return the end of the run (@a e or a byte below 0x80) if it consists of
|
||||
complete well-formed UTF-8 sequences, otherwise nullptr
|
||||
|
||||
It stops at the first ill-formed byte: the serializer's error handlers scan
|
||||
again after every ill-formed byte, and scanning on to the end of the run would
|
||||
make them quadratic. Without the position of the error, it is faster than
|
||||
utf8_dfa_prefix(), which made the lexer 15% slower on CJK text.
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE inline const unsigned char* utf8_dfa_run(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint64_t* const table = utf8_dfa_table();
|
||||
std::uint64_t state = 6;
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
// a shift uses only the low 6 bits of its count on x86-64 and ARM64, so
|
||||
// the mask is free there
|
||||
state = table[*p] >> (state & 63u);
|
||||
++p;
|
||||
if (JSON_HEDLEY_UNLIKELY((state & 63u) == 0))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return (state & 63u) == 6 ? p : nullptr;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the end of the longest well-formed UTF-8 prefix of a run of non-ASCII bytes
|
||||
|
||||
Validates [p, e) up to the first byte below 0x80 (see utf8_dfa_table()) and
|
||||
returns the end of the longest prefix that consists of complete well-formed
|
||||
UTF-8 sequences. The whole run was valid if the result is @a e or points to a
|
||||
byte below 0x80; otherwise it points to the lead byte of the first ill-formed or
|
||||
incomplete sequence. Like utf8_dfa_run(), it stops at the first ill-formed byte.
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE inline const unsigned char* utf8_dfa_prefix(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint64_t* const table = utf8_dfa_table();
|
||||
std::uint64_t state = 6;
|
||||
const unsigned char* last = p; // the end of the last complete sequence
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
// a shift uses only the low 6 bits of its count on x86-64 and ARM64, so
|
||||
// the mask is free there
|
||||
state = table[*p] >> (state & 63u);
|
||||
++p;
|
||||
const std::uint64_t current = state & 63u;
|
||||
if (JSON_HEDLEY_UNLIKELY(current == 0))
|
||||
{
|
||||
break;
|
||||
}
|
||||
last = (current == 6) ? p : last;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
// Return the length of the longest prefix of [data, data+n) that consists of
|
||||
// ASCII characters and complete well-formed UTF-8 sequences; n if all of it is
|
||||
// valid UTF-8. Unlike scalar_string_bulk_run(), quotes, escapes, and control
|
||||
@@ -250,12 +401,12 @@ inline std::size_t valid_utf8_prefix(const unsigned char* data, std::size_t n) n
|
||||
continue;
|
||||
}
|
||||
|
||||
const std::size_t seq = validate_one_utf8(data + pos, n - pos);
|
||||
if (seq == 0)
|
||||
const unsigned char* const last = utf8_dfa_prefix(data + pos, data + n);
|
||||
pos = static_cast<std::size_t>(last - data);
|
||||
if (pos != n && *last >= 0x80u)
|
||||
{
|
||||
break; // ill-formed or truncated
|
||||
}
|
||||
pos += seq;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
@@ -440,18 +591,14 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t scalar_string_bulk_run(const unsigned char
|
||||
}
|
||||
#endif
|
||||
#if !JSON_SCAN_VECTOR_UTF8 || JSON_SCAN_SSSE3_DISPATCH
|
||||
// a run of multi-byte sequences (e.g. CJK text) is validated sequence
|
||||
// by sequence without searching for the next special byte in between
|
||||
do
|
||||
// a run of multi-byte sequences (e.g. CJK text) is validated as a whole
|
||||
const unsigned char* const run_end = utf8_dfa_run(p, e);
|
||||
if (JSON_HEDLEY_UNLIKELY(run_end == nullptr))
|
||||
{
|
||||
const std::size_t seq = validate_one_utf8(p, static_cast<std::size_t>(e - p));
|
||||
if (seq == 0)
|
||||
{
|
||||
return static_cast<std::size_t>(p - data); // ill-formed or truncated: let the byte path diagnose it
|
||||
}
|
||||
p += seq;
|
||||
// ill-formed or truncated: let the byte path diagnose it
|
||||
return static_cast<std::size_t>(utf8_dfa_prefix(p, e) - data);
|
||||
}
|
||||
while (p != e && *p >= 0x80u);
|
||||
p = run_end;
|
||||
#endif
|
||||
}
|
||||
return static_cast<std::size_t>(p - data);
|
||||
@@ -556,7 +703,7 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t lexer_string_bulk_run(const unsigned char*
|
||||
// unaffected by this fast path.
|
||||
inline int hex_codepoint(const unsigned char* data) noexcept
|
||||
{
|
||||
static const std::array<std::uint8_t, 256> hex_digit_table = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static const std::array<std::uint8_t, 256> hex_digit_table =
|
||||
{
|
||||
{
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 00..0F
|
||||
|
||||
@@ -206,9 +206,10 @@ places, which differ in speed, diagnostics, and how they read the input:
|
||||
the bytes as is and leave the check to dump() and the binary writers.
|
||||
- the per-lead-byte switch in lexer::scan_string(): JSON text, with a
|
||||
diagnostic for each kind of error.
|
||||
- validate_one_utf8() and valid_utf8_prefix() in string_scan.hpp: the lexer's
|
||||
bulk string scan, the bulk path of the BON8 reader, and the BON8 writer.
|
||||
They must accept exactly what the lexer's switch accepts.
|
||||
- validate_one_utf8(), utf8_dfa_prefix() and valid_utf8_prefix() in
|
||||
string_scan.hpp: the lexer's bulk string scan, the bulk path of the BON8
|
||||
reader, and the BON8 writer. They must accept exactly what the lexer's
|
||||
switch accepts.
|
||||
- the byte path of binary_reader::get_bon8_string(): BON8 input without bulk
|
||||
access, and the bytes the bulk path leaves to it. It keeps its own ranges:
|
||||
taking them from utf8_shape() made BON8 decoding 2-3% slower (#5891).
|
||||
|
||||
@@ -628,11 +628,7 @@ run_found:
|
||||
// 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}));
|
||||
}
|
||||
const std::size_t size = (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_doc.arena.resize((std::max)(m_doc.arena.size() * 2, wanted));
|
||||
m_arena_write = m_doc.arena.data() + used;
|
||||
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,
|
||||
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)
|
||||
{
|
||||
// accept() of plain JSON: nothing is stored. The other option sets
|
||||
|
||||
@@ -8,9 +8,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
#include <array> // array
|
||||
#include <cstddef> // size_t
|
||||
#include <cstdint> // uint8_t, uint32_t
|
||||
@@ -182,52 +179,6 @@ struct document_data
|
||||
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
|
||||
/// std::bad_alloc for a count that does not fit the address space,
|
||||
/// instead of wrapping around in n * sizeof(node))
|
||||
|
||||
Generated
+165
-17
@@ -7388,9 +7388,10 @@ places, which differ in speed, diagnostics, and how they read the input:
|
||||
the bytes as is and leave the check to dump() and the binary writers.
|
||||
- the per-lead-byte switch in lexer::scan_string(): JSON text, with a
|
||||
diagnostic for each kind of error.
|
||||
- validate_one_utf8() and valid_utf8_prefix() in string_scan.hpp: the lexer's
|
||||
bulk string scan, the bulk path of the BON8 reader, and the BON8 writer.
|
||||
They must accept exactly what the lexer's switch accepts.
|
||||
- validate_one_utf8(), utf8_dfa_prefix() and valid_utf8_prefix() in
|
||||
string_scan.hpp: the lexer's bulk string scan, the bulk path of the BON8
|
||||
reader, and the BON8 writer. They must accept exactly what the lexer's
|
||||
switch accepts.
|
||||
- the byte path of binary_reader::get_bon8_string(): BON8 input without bulk
|
||||
access, and the bytes the bulk path leaves to it. It keeps its own ranges:
|
||||
taking them from utf8_shape() made BON8 decoding 2-3% slower (#5891).
|
||||
@@ -13171,6 +13172,157 @@ inline std::size_t validate_one_utf8(const unsigned char* data, std::size_t avai
|
||||
return 0; // invalid, incomplete, or must be diagnosed by the byte path
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the transition table of the UTF-8 DFA of utf8_dfa_run() and utf8_dfa_prefix()
|
||||
|
||||
They accept exactly what validate_one_utf8() accepts. This is a shift-based
|
||||
DFA (after Per Vognsen; see also https://orlp.net/blog/utf8-dfa/): a state is
|
||||
6 times its number, and table[b] holds, for every state s, the next state on
|
||||
byte b at bit offset s. One step is a load and a shift without a branch on the
|
||||
byte. On CJK text, it made the lexer and the serializer without SSE2/NEON 30%
|
||||
faster and the BON8 reader 15% faster than a loop over validate_one_utf8()
|
||||
(Apple M, Apple Clang).
|
||||
|
||||
The states are 0 (error, absorbing), 6 (between sequences), 12/18/24 (1/2/3
|
||||
continuation bytes of 0x80-0xBF missing), and the states after the lead bytes
|
||||
that restrict the second byte: 30 (0xE0: 0xA0-0xBF), 36 (0xED: 0x80-0x9F), 42
|
||||
(0xF0: 0x90-0xBF), and 48 (0xF4: 0x80-0x8F). The test "UTF-8 DFA" in
|
||||
unit-class_lexer.cpp checks every entry of the table.
|
||||
*/
|
||||
inline const std::uint64_t* utf8_dfa_table() noexcept
|
||||
{
|
||||
static const std::array<std::uint64_t, 256> table =
|
||||
{
|
||||
{
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000780ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000900ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000A80ull, 0x0000000000000600ull, 0x0000000000000600ull, 0x0000000000000600ull,
|
||||
0x0000000000000C00ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
}
|
||||
};
|
||||
return table.data();
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief validate a run of non-ASCII bytes as UTF-8
|
||||
|
||||
Validates [p, e) up to the first byte below 0x80 (see utf8_dfa_table()).
|
||||
@return the end of the run (@a e or a byte below 0x80) if it consists of
|
||||
complete well-formed UTF-8 sequences, otherwise nullptr
|
||||
|
||||
It stops at the first ill-formed byte: the serializer's error handlers scan
|
||||
again after every ill-formed byte, and scanning on to the end of the run would
|
||||
make them quadratic. Without the position of the error, it is faster than
|
||||
utf8_dfa_prefix(), which made the lexer 15% slower on CJK text.
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE inline const unsigned char* utf8_dfa_run(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint64_t* const table = utf8_dfa_table();
|
||||
std::uint64_t state = 6;
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
// a shift uses only the low 6 bits of its count on x86-64 and ARM64, so
|
||||
// the mask is free there
|
||||
state = table[*p] >> (state & 63u);
|
||||
++p;
|
||||
if (JSON_HEDLEY_UNLIKELY((state & 63u) == 0))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return (state & 63u) == 6 ? p : nullptr;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the end of the longest well-formed UTF-8 prefix of a run of non-ASCII bytes
|
||||
|
||||
Validates [p, e) up to the first byte below 0x80 (see utf8_dfa_table()) and
|
||||
returns the end of the longest prefix that consists of complete well-formed
|
||||
UTF-8 sequences. The whole run was valid if the result is @a e or points to a
|
||||
byte below 0x80; otherwise it points to the lead byte of the first ill-formed or
|
||||
incomplete sequence. Like utf8_dfa_run(), it stops at the first ill-formed byte.
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE inline const unsigned char* utf8_dfa_prefix(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint64_t* const table = utf8_dfa_table();
|
||||
std::uint64_t state = 6;
|
||||
const unsigned char* last = p; // the end of the last complete sequence
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
// a shift uses only the low 6 bits of its count on x86-64 and ARM64, so
|
||||
// the mask is free there
|
||||
state = table[*p] >> (state & 63u);
|
||||
++p;
|
||||
const std::uint64_t current = state & 63u;
|
||||
if (JSON_HEDLEY_UNLIKELY(current == 0))
|
||||
{
|
||||
break;
|
||||
}
|
||||
last = (current == 6) ? p : last;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
// Return the length of the longest prefix of [data, data+n) that consists of
|
||||
// ASCII characters and complete well-formed UTF-8 sequences; n if all of it is
|
||||
// valid UTF-8. Unlike scalar_string_bulk_run(), quotes, escapes, and control
|
||||
@@ -13198,12 +13350,12 @@ inline std::size_t valid_utf8_prefix(const unsigned char* data, std::size_t n) n
|
||||
continue;
|
||||
}
|
||||
|
||||
const std::size_t seq = validate_one_utf8(data + pos, n - pos);
|
||||
if (seq == 0)
|
||||
const unsigned char* const last = utf8_dfa_prefix(data + pos, data + n);
|
||||
pos = static_cast<std::size_t>(last - data);
|
||||
if (pos != n && *last >= 0x80u)
|
||||
{
|
||||
break; // ill-formed or truncated
|
||||
}
|
||||
pos += seq;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
@@ -13388,18 +13540,14 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t scalar_string_bulk_run(const unsigned char
|
||||
}
|
||||
#endif
|
||||
#if !JSON_SCAN_VECTOR_UTF8 || JSON_SCAN_SSSE3_DISPATCH
|
||||
// a run of multi-byte sequences (e.g. CJK text) is validated sequence
|
||||
// by sequence without searching for the next special byte in between
|
||||
do
|
||||
// a run of multi-byte sequences (e.g. CJK text) is validated as a whole
|
||||
const unsigned char* const run_end = utf8_dfa_run(p, e);
|
||||
if (JSON_HEDLEY_UNLIKELY(run_end == nullptr))
|
||||
{
|
||||
const std::size_t seq = validate_one_utf8(p, static_cast<std::size_t>(e - p));
|
||||
if (seq == 0)
|
||||
{
|
||||
return static_cast<std::size_t>(p - data); // ill-formed or truncated: let the byte path diagnose it
|
||||
}
|
||||
p += seq;
|
||||
// ill-formed or truncated: let the byte path diagnose it
|
||||
return static_cast<std::size_t>(utf8_dfa_prefix(p, e) - data);
|
||||
}
|
||||
while (p != e && *p >= 0x80u);
|
||||
p = run_end;
|
||||
#endif
|
||||
}
|
||||
return static_cast<std::size_t>(p - data);
|
||||
@@ -13504,7 +13652,7 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t lexer_string_bulk_run(const unsigned char*
|
||||
// unaffected by this fast path.
|
||||
inline int hex_codepoint(const unsigned char* data) noexcept
|
||||
{
|
||||
static const std::array<std::uint8_t, 256> hex_digit_table = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static const std::array<std::uint8_t, 256> hex_digit_table =
|
||||
{
|
||||
{
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 00..0F
|
||||
|
||||
Generated
+1
-65
@@ -102,9 +102,6 @@
|
||||
|
||||
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
#include <array> // array
|
||||
#include <cstddef> // size_t
|
||||
#include <cstdint> // uint8_t, uint32_t
|
||||
@@ -526,52 +523,6 @@ struct document_data
|
||||
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
|
||||
/// std::bad_alloc for a count that does not fit the address space,
|
||||
/// instead of wrapping around in n * sizeof(node))
|
||||
@@ -2013,11 +1964,7 @@ run_found:
|
||||
// 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}));
|
||||
}
|
||||
const std::size_t size = (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_doc.arena.resize((std::max)(m_doc.arena.size() * 2, wanted));
|
||||
m_arena_write = m_doc.arena.data() + used;
|
||||
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,
|
||||
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)
|
||||
{
|
||||
// accept() of plain JSON: nothing is stored. The other option sets
|
||||
|
||||
@@ -2138,6 +2138,14 @@ TEST_CASE("UTF-8 validators accept the same sequences")
|
||||
CAPTURE(t)
|
||||
CHECK((dfa == expected && bulk == (expected ? length : 0) && bulk_short == 0));
|
||||
|
||||
// the DFA of the bulk paths: the whole sequence or nothing
|
||||
if (length >= 2)
|
||||
{
|
||||
const unsigned char* const run_end = nlohmann::detail::utf8_dfa_prefix(bytes.data(), bytes.data() + length);
|
||||
const unsigned char* const short_end = nlohmann::detail::utf8_dfa_prefix(bytes.data(), bytes.data() + length - 1);
|
||||
CHECK((run_end == bytes.data() + (expected ? length : 0) && short_end == bytes.data()));
|
||||
}
|
||||
|
||||
if (t < 2 && length >= 2)
|
||||
{
|
||||
// the lexer's byte path
|
||||
@@ -2165,6 +2173,70 @@ TEST_CASE("UTF-8 validators accept the same sequences")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("UTF-8 DFA")
|
||||
{
|
||||
// utf8_dfa_prefix() and utf8_dfa_run() against a loop over
|
||||
// validate_one_utf8(): every byte
|
||||
// after a prefix that leads to each state of the DFA checks every entry of
|
||||
// its table, and the bytes after it whether it stops at the right place
|
||||
const auto reference = [](const unsigned char* p, const unsigned char* e)
|
||||
{
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
const std::size_t length = nlohmann::detail::validate_one_utf8(p, static_cast<std::size_t>(e - p));
|
||||
if (length == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
p += length;
|
||||
}
|
||||
return p;
|
||||
};
|
||||
|
||||
// between sequences, after a valid sequence, 1/2/3 continuation bytes
|
||||
// missing, and after the lead bytes that restrict the second byte
|
||||
const std::vector<std::vector<unsigned char>> prefixes =
|
||||
{
|
||||
{}, {0xC3, 0xA4}, {0xC3}, {0xE1}, {0xF1}, {0xE1, 0x80}, {0xF1, 0x80}, {0xF1, 0x80, 0x80},
|
||||
{0xE0}, {0xED}, {0xF0}, {0xF4}
|
||||
};
|
||||
const std::vector<std::vector<unsigned char>> suffixes =
|
||||
{
|
||||
{}, {0x80}, {0xBF}, {0x41}, {0xC3, 0xA4}, {0xE4, 0xB8, 0xAD}, {0xF0, 0x9F, 0x98, 0x80}, {0xFF}
|
||||
};
|
||||
for (const auto& prefix : prefixes)
|
||||
{
|
||||
for (unsigned byte = 0; byte <= 0xFF; ++byte)
|
||||
{
|
||||
for (const auto& suffix : suffixes)
|
||||
{
|
||||
std::vector<unsigned char> input(prefix);
|
||||
input.push_back(static_cast<unsigned char>(byte));
|
||||
input.insert(input.end(), suffix.begin(), suffix.end());
|
||||
const unsigned char* const begin = input.data();
|
||||
const unsigned char* const end = begin + input.size();
|
||||
CAPTURE(prefix.size())
|
||||
CAPTURE(byte)
|
||||
CAPTURE(suffix.size())
|
||||
const unsigned char* const expected = reference(begin, end);
|
||||
CHECK(nlohmann::detail::utf8_dfa_prefix(begin, end) - begin == expected - begin);
|
||||
|
||||
// utf8_dfa_run() only tells whether the whole run is valid
|
||||
const bool whole_run = expected == end || *expected < 0x80u;
|
||||
CHECK(nlohmann::detail::utf8_dfa_run(begin, end) == (whole_run ? expected : nullptr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// an ill-formed byte stops the scan at once, whatever follows
|
||||
std::vector<unsigned char> invalid(1000, 0xFF);
|
||||
CHECK(nlohmann::detail::utf8_dfa_prefix(invalid.data(), invalid.data() + invalid.size()) == invalid.data());
|
||||
CHECK(nlohmann::detail::utf8_dfa_run(invalid.data(), invalid.data() + invalid.size()) == nullptr);
|
||||
invalid[0] = 0xC3;
|
||||
invalid[1] = 0xA4;
|
||||
CHECK(nlohmann::detail::utf8_dfa_prefix(invalid.data(), invalid.data() + invalid.size()) == invalid.data() + 2);
|
||||
}
|
||||
|
||||
TEST_CASE("float_bigint")
|
||||
{
|
||||
// the big integer of the exact comparison of a float token with the
|
||||
|
||||
@@ -598,54 +598,3 @@ TEST_CASE("json_view large objects")
|
||||
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