Write the one-byte head of an array or object with a single sink write
The head of an array or object of up to 23 elements (15 for MessagePack) is a single byte, but it was written through write_cbor_head()/write_number(), which GCC keeps out of line, and a std::array. The CBOR array and object writers now write that byte directly, and MessagePack does the same in its array and map prefixes. Instructions per call (Linux, GCC 13, thousands), to_cbor: citm_catalog 5588 -> 5020, canada 15840 -> 14499, 20000 small objects 13162 -> 12582; to_msgpack: citm_catalog 5920 -> 5676, canada 16344 -> 15676. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
2 files changed
+32
-8
No files matched your search
Generated
+16
-4
@@ -25743,7 +25743,7 @@ class binary_writer
|
||||
if (n <= 15)
|
||||
{
|
||||
// fixarray
|
||||
write_number(static_cast<std::uint8_t>(0x90 | n));
|
||||
oa.write_character(to_char_type(static_cast<std::uint8_t>(0x90 | n)));
|
||||
}
|
||||
else if (n <= (std::numeric_limits<std::uint16_t>::max)())
|
||||
{
|
||||
@@ -25765,7 +25765,7 @@ class binary_writer
|
||||
if (n <= 15)
|
||||
{
|
||||
// fixmap
|
||||
write_number(static_cast<std::uint8_t>(0x80 | (n & 0xF)));
|
||||
oa.write_character(to_char_type(static_cast<std::uint8_t>(0x80 | (n & 0xF))));
|
||||
}
|
||||
else if (n <= (std::numeric_limits<std::uint16_t>::max)())
|
||||
{
|
||||
@@ -27419,6 +27419,18 @@ class binary_writer
|
||||
return encode_first_byte_and_tail(out, first_byte, size_class, argument);
|
||||
}
|
||||
|
||||
/// write the head of an array or object: one byte for a size of up to 23, else as
|
||||
/// @ref write_cbor_head
|
||||
void write_cbor_container_head(const std::uint8_t major_type, const std::uint64_t size)
|
||||
{
|
||||
if (JSON_HEDLEY_LIKELY(size <= 0x17))
|
||||
{
|
||||
oa.write_character(to_char_type(static_cast<std::uint8_t>(major_type + size)));
|
||||
return;
|
||||
}
|
||||
write_cbor_head(major_type, size);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief write an object value (head and members) as CBOR
|
||||
|
||||
@@ -27429,7 +27441,7 @@ class binary_writer
|
||||
void write_cbor_object(const BasicJsonType& j, const std::size_t depth)
|
||||
{
|
||||
// step 1: write control byte and the object size
|
||||
write_cbor_head(0xA0, j.m_data.m_value.object->size());
|
||||
write_cbor_container_head(0xA0, j.m_data.m_value.object->size());
|
||||
|
||||
for (const auto& el : *j.m_data.m_value.object)
|
||||
{
|
||||
@@ -27480,7 +27492,7 @@ class binary_writer
|
||||
JSON_HEDLEY_NEVER_INLINE
|
||||
void write_cbor_array(const BasicJsonType& j, const std::size_t depth)
|
||||
{
|
||||
write_cbor_head(0x80, j.m_data.m_value.array->size());
|
||||
write_cbor_container_head(0x80, j.m_data.m_value.array->size());
|
||||
if (!j.m_data.m_value.array->empty())
|
||||
{
|
||||
write_cbor_array_elements(j, depth, std::integral_constant<bool, integers_fit_64_bits()>());
|
||||
|
||||
Reference in new issue
Block a user