Check and sanitize strings out of line

The string writers kept the string_t that holds a sanitized copy for the other
error handlers in the function that also has the fast path for the default
one, so that every string paid for its construction check, a stack frame with
a stack protector under GCC, and the registers saved for the slow path.

The slow path is now a function of its own that the fast path does not call.

Instructions per call (Linux, GCC 13, thousands), to_ubjson: twitter 3867 -> 3274, 20000 small objects 20981 -> 17901; to_bjdata: twitter 3873 -> 3281, 20000 small objects 21001 -> 17921; to_msgpack: 20000 small objects 18342 -> 15922.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-22 09:29:01 +02:00
1 parent a517b8ea8b
commit dd3ed1ef86
2 files changed
+50

No files matched your search

@@ -2894,6 +2894,15 @@ class binary_writer
}
}
write_cbor_checked_string(value, context);
}
/// @brief write a CBOR text string checked or sanitized by the error handler;
/// out of line, as the string_t for the sanitized copy would make
/// every call of @ref write_cbor_string set up a frame for it
JSON_HEDLEY_NEVER_INLINE
void write_cbor_checked_string(const string_t& value, const BasicJsonType& context)
{
string_t storage;
const string_t& sanitized = sanitize_utf8_for_write(value, context, storage);
@@ -2946,6 +2955,14 @@ class binary_writer
}
}
write_msgpack_checked_string(value, context);
}
/// @brief write a MessagePack str checked or sanitized by the error handler;
/// out of line, like @ref write_cbor_checked_string
JSON_HEDLEY_NEVER_INLINE
void write_msgpack_checked_string(const string_t& value, const BasicJsonType& context)
{
string_t storage;
const string_t& sanitized = sanitize_utf8_for_write(value, context, storage);
@@ -3167,6 +3184,14 @@ class binary_writer
return;
}
write_ubjson_checked_text(value, context, add_marker, use_bjdata);
}
/// @brief write a UBJSON/BJData string checked or sanitized by the error
/// handler; out of line, like @ref write_cbor_checked_string
JSON_HEDLEY_NEVER_INLINE
void write_ubjson_checked_text(const string_t& value, const BasicJsonType& context, const bool add_marker, const bool use_bjdata)
{
string_t storage;
const string_t& sanitized = sanitize_utf8_for_write(value, context, storage);
if (add_marker)