mirror of
https://github.com/nlohmann/json.git
synced 2026-11-02 10:55:35 +00:00
CBOR reader: read runs of scalar array elements in one loop (#5975)
* CBOR reader: read runs of scalar array elements in one loop When a definite-length CBOR array opens and the input is contiguous, read the integers, floats, booleans, and nulls that follow in a tight loop instead of going through parse_cbor_internal's container logic and parse_cbor_value's dispatch for every element. The loop stops at any other kind of value and when fewer than 9 bytes are left; those values are read by the general path exactly as before. Non-contiguous inputs are not affected (tag dispatch on the bulk-scan capability). Also pass a member empty string as the number source to number_float instead of constructing a std::string for every float. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * CBOR reader: address review of the scalar-run reader - parse_cbor_scalar_run: set the size explicitly in every case that reads a value, instead of relying on a default that every other case overwrites. - Remove cbor_scalar_follows(): parse_cbor_scalar_run already returns on the same conditions, and without the pre-check SAX canada needs another 8.5 % fewer instructions with GCC 13 (0.915 against the previous version), with no row more than 0.5 % worse (Mac and GCC). - Restore the comment on why parse_cbor_internal copies the container frame. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * CBOR reader: enter the scalar run only when a scalar follows An array of containers or strings called the out-of-line run start once per array only to leave it at once; on nested data (arrays of objects) that call made reading slower than before the run reader. enter_cbor_array now tests the first byte and the remaining length inline and calls the run only for a scalar, so the run's loop condition moves to the end (do-while). Signed-off-by: Niels Lohmann <mail@nlohmann.me> * CBOR reader: mark the initial accepted in the scalar run as a false-positive dead store ci_infer reports DEAD_STORE for 'bool accepted = true;' in parse_cbor_scalar_run because every case assigns accepted before it is read. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * CBOR reader: drop the scalar pre-check before the run enter_cbor_array calls the run directly again, and parse_cbor_scalar_run tests the remaining count and length at the top of a while loop; its own switch already leaves on any value that is not a scalar. The MessagePack reader is built the same way. Instructions per repetition, new/old: clang SAX canada 0.983, read canada 0.993; GCC 13 read canada 0.997, the other rows at most 1.004. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * CBOR reader: say why the initial accepted in the scalar run stays Infer's DEAD_STORE report is right, not a false positive: every case that reaches the read assigns accepted. The initializer is kept for clang-tidy's cppcoreguidelines-init-variables. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * CBOR reader: give the scalar run the bulk tag instead of a wrapper parse_cbor_scalar_run takes the bulk-scan tag itself and is the function that is kept out of line, so the one-line parse_cbor_array_start wrapper goes, as the UBJSON typed run already does. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * CBOR reader: start the scalar run's accepted at false Infer ignores dead stores of 0 (--liveness-ignored-constant defaults to "0"), and false is 0 to it, so the suppression and its comment go. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * CBOR reader: let the scalar run find its array itself parse_cbor_scalar_run reads container_stack.back() in its bulk version, so the caller no longer evaluates it for input that is not contiguous. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
1 parent
5337d841d4
commit
6c3ab966d3
3 files changed
+414
-28
No files matched your search
Generated
+170
-14
@@ -18549,11 +18549,13 @@ class binary_reader
|
||||
bool get_cbor_negative_integer()
|
||||
{
|
||||
NumberType number{};
|
||||
if (JSON_HEDLEY_UNLIKELY(!get_number(number)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return get_number(number) && emit_cbor_negative_integer(number);
|
||||
}
|
||||
|
||||
/// pass the CBOR negative integer -1 - @a number to the SAX parser
|
||||
template<typename NumberType>
|
||||
bool emit_cbor_negative_integer(const NumberType number)
|
||||
{
|
||||
// the value is -1 - number, which fits into number_integer_t
|
||||
// whenever number does; the outer cast undoes the integral promotion
|
||||
// for number_integer_t types narrower than int
|
||||
@@ -18778,7 +18780,7 @@ class binary_reader
|
||||
case 0x95:
|
||||
case 0x96:
|
||||
case 0x97:
|
||||
return enter_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
|
||||
return enter_cbor_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
|
||||
|
||||
case 0x98: // array (one-byte uint8_t for n follows)
|
||||
case 0x99: // array (two-byte uint16_t for n follow)
|
||||
@@ -18787,7 +18789,7 @@ class binary_reader
|
||||
{
|
||||
std::uint64_t len{};
|
||||
std::size_t size{};
|
||||
return get_cbor_argument(len) && get_cbor_container_size(len, size, "array") && enter_array(size);
|
||||
return get_cbor_argument(len) && get_cbor_container_size(len, size, "array") && enter_cbor_array(size);
|
||||
}
|
||||
|
||||
case 0x9F: // array (indefinite length)
|
||||
@@ -19484,6 +19486,155 @@ class binary_reader
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief read a big-endian number of type @a NumberType at @a data
|
||||
*/
|
||||
template<typename NumberType>
|
||||
NumberType load_big_endian(const unsigned char* data) const noexcept
|
||||
{
|
||||
NumberType result{};
|
||||
std::memcpy(&result, data, sizeof(NumberType));
|
||||
if (is_little_endian)
|
||||
{
|
||||
byte_swap(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief read the integers, floats, booleans, and nulls that follow in a
|
||||
definite-length array, without the per-element bookkeeping of
|
||||
@ref parse_cbor_internal
|
||||
|
||||
Stops at the end of the array, at any other kind of value, and when fewer
|
||||
than 9 bytes (the longest of these values) are left in the input, leaving
|
||||
those to @ref parse_cbor_value, which reads them the same way.
|
||||
|
||||
Out of line, so that @ref parse_cbor_value stays small enough to be inlined.
|
||||
|
||||
The array is the one on top of @ref container_stack; its count of
|
||||
elements not yet read is updated.
|
||||
|
||||
@return whether the SAX parser accepted the values
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE
|
||||
bool parse_cbor_scalar_run(std::true_type /*bulk*/)
|
||||
{
|
||||
// nothing pushes onto the stack while the run reads scalars
|
||||
std::size_t& remaining = container_stack.back().remaining;
|
||||
while (remaining != 0 && ia.bulk_remaining() >= 9)
|
||||
{
|
||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||
const unsigned char head = data[0];
|
||||
std::size_t size = 0;
|
||||
bool accepted = false;
|
||||
switch (head)
|
||||
{
|
||||
case 0x18:
|
||||
size = 2;
|
||||
accepted = emit_unsigned(data[1]);
|
||||
break;
|
||||
case 0x19:
|
||||
size = 3;
|
||||
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
||||
break;
|
||||
case 0x1A:
|
||||
size = 5;
|
||||
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
||||
break;
|
||||
case 0x1B:
|
||||
size = 9;
|
||||
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
||||
break;
|
||||
case 0x38:
|
||||
size = 2;
|
||||
accepted = emit_cbor_negative_integer(data[1]);
|
||||
break;
|
||||
case 0x39:
|
||||
size = 3;
|
||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint16_t>(data + 1));
|
||||
break;
|
||||
case 0x3A:
|
||||
size = 5;
|
||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint32_t>(data + 1));
|
||||
break;
|
||||
case 0x3B:
|
||||
size = 9;
|
||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint64_t>(data + 1));
|
||||
break;
|
||||
case 0xF4:
|
||||
size = 1;
|
||||
accepted = sax->boolean(false);
|
||||
break;
|
||||
case 0xF5:
|
||||
size = 1;
|
||||
accepted = sax->boolean(true);
|
||||
break;
|
||||
case 0xF6:
|
||||
size = 1;
|
||||
accepted = sax->null();
|
||||
break;
|
||||
case 0xFA:
|
||||
size = 5;
|
||||
accepted = emit_float(load_big_endian<float>(data + 1));
|
||||
break;
|
||||
case 0xFB:
|
||||
size = 9;
|
||||
accepted = emit_float(load_big_endian<double>(data + 1));
|
||||
break;
|
||||
default:
|
||||
if (head <= 0x17)
|
||||
{
|
||||
size = 1;
|
||||
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
||||
}
|
||||
else if (head >= 0x20 && head <= 0x37)
|
||||
{
|
||||
size = 1;
|
||||
accepted = sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - head));
|
||||
}
|
||||
else
|
||||
{
|
||||
// any other value is read by parse_cbor_value
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// as if read by get() and get_number(): current is the head byte
|
||||
ia.bulk_skip(size);
|
||||
chars_read += size;
|
||||
current = head;
|
||||
--remaining;
|
||||
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief open a definite-length array and read the scalars it begins with
|
||||
(see @ref parse_cbor_scalar_run)
|
||||
|
||||
Only the start of an array is tried, which covers arrays of numbers
|
||||
(e.g. coordinates) but costs arrays of other values only one call, not one
|
||||
per element.
|
||||
|
||||
@param[in] len the number of elements
|
||||
@return whether the SAX parser accepted the array and the values read
|
||||
*/
|
||||
bool enter_cbor_array(const std::size_t len)
|
||||
{
|
||||
return enter_array(len) && (len == 0 || parse_cbor_scalar_run(std::integral_constant<bool, bulk_scan> {}));
|
||||
}
|
||||
|
||||
/// input that is not contiguous: every value is read by parse_cbor_value
|
||||
bool parse_cbor_scalar_run(std::false_type /*bulk*/) const noexcept
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief read a CBOR value and everything nested inside it
|
||||
|
||||
@@ -19513,6 +19664,7 @@ class binary_reader
|
||||
// pop_back() below, which destroys the container_stack element
|
||||
// it would otherwise alias
|
||||
const container_frame top = container_stack.back();
|
||||
const bool is_object = top.is_object;
|
||||
bool at_end = false;
|
||||
|
||||
if (top.remaining != npos)
|
||||
@@ -19524,7 +19676,7 @@ class binary_reader
|
||||
{
|
||||
// claim the element about to be read
|
||||
--container_stack.back().remaining;
|
||||
if (top.is_object)
|
||||
if (is_object)
|
||||
{
|
||||
get();
|
||||
}
|
||||
@@ -19537,7 +19689,7 @@ class binary_reader
|
||||
// Testing for it consumes a byte, which is the first byte
|
||||
// of the next element when it is not one.
|
||||
at_end = (get() == 0xFF);
|
||||
fetch = top.is_object;
|
||||
fetch = is_object;
|
||||
}
|
||||
|
||||
if (at_end)
|
||||
@@ -19554,7 +19706,7 @@ class binary_reader
|
||||
continue;
|
||||
}
|
||||
|
||||
if (top.is_object)
|
||||
if (is_object)
|
||||
{
|
||||
if (JSON_HEDLEY_UNLIKELY(!skip_cbor_key_tags(tag_handler)))
|
||||
{
|
||||
@@ -21499,13 +21651,13 @@ class binary_reader
|
||||
return sax->null();
|
||||
|
||||
case 0xFB:
|
||||
return sax->number_float(static_cast<number_float_t>(-1.0), "");
|
||||
return sax->number_float(static_cast<number_float_t>(-1.0), no_source);
|
||||
|
||||
case 0xFC:
|
||||
return sax->number_float(static_cast<number_float_t>(0.0), "");
|
||||
return sax->number_float(static_cast<number_float_t>(0.0), no_source);
|
||||
|
||||
case 0xFD:
|
||||
return sax->number_float(static_cast<number_float_t>(1.0), "");
|
||||
return sax->number_float(static_cast<number_float_t>(1.0), no_source);
|
||||
|
||||
case 0xFF: // empty string
|
||||
{
|
||||
@@ -22009,7 +22161,7 @@ class binary_reader
|
||||
{
|
||||
return out_of_range_error(exception_id::number_overflow, "number overflow", "value");
|
||||
}
|
||||
return sax->number_float(result, "");
|
||||
return sax->number_float(result, no_source);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -22069,7 +22221,7 @@ class binary_reader
|
||||
}();
|
||||
return sax->number_float((half & 0x8000u) != 0
|
||||
? static_cast<number_float_t>(-val)
|
||||
: static_cast<number_float_t>(val), "");
|
||||
: static_cast<number_float_t>(val), no_source);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -22394,6 +22546,10 @@ class binary_reader
|
||||
/// the SAX parser
|
||||
json_sax_t* sax = nullptr;
|
||||
|
||||
/// the (empty) source text of every number_float event: binary formats
|
||||
/// have none, and a string_t built for each float would cost more
|
||||
const string_t no_source{};
|
||||
|
||||
/// the containers that have been opened and not closed yet; see @ref container_frame
|
||||
std::vector<container_frame> container_stack{};
|
||||
|
||||
|
||||
Reference in new issue
Block a user