Compare commits

..
Author SHA1 Message Date
Niels Lohmann 3f14362959 Keep GCC from expanding put_string's memcpy into rep movsq
put_string() tests the run length against the size of the write buffer
before copying it there, so GCC knows the memcpy length is below 1024. With
-march=x86-64-v3 (the default of Ubuntu 26.04's GCC on CPUs that support it)
and -mtune=generic, it then expands the memcpy inline into "rep movsq"
instead of calling memcpy. The start-up cost of rep movsq is far above that
of the call for the runs of a few dozen bytes the serializer writes; it hit
dump() with ensure_ascii, where the copy sits in dump_escaped_impl<true>.

An empty asm statement now hides the bound from GCC's optimizer, so the
copy stays a call to memcpy. Clang is not affected and does not see the
statement.

dump() with ensure_ascii, GCC 15.2 -O3 -march=x86-64-v3, Sapphire Rapids
(c7i.2xlarge), median of 5 interleaved rounds: github_events -40.7 %,
semanticscholar -12.2 %, gsoc-2018 -10.8 %, update-center -7.7 %, twitter
-4.8 %. Aligned builds (-falign-loops=64 -falign-functions=64) agree. All
other rows (ensure_ascii=false, baseline x86-64, Clang) are within noise;
outside dump_escaped_impl<true> the generated code differs only in register
allocation.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-11-26 01:20:16 +01:00
4 changed files with 32 additions and 10 deletions

No files matched your search

+2 -3
View File
@@ -1322,9 +1322,8 @@ class json_sax_dom_parser : public json_sax_dom_error_state
} }
/// the number of members an object collects on the stack before it /// the number of members an object collects on the stack before it
/// switches to storing them directly; this is the size from which the /// switches to storing them directly
/// stored object looks up its keys in a hash table (key_index_threshold) static constexpr std::size_t object_buffer_limit = 32;
static constexpr std::size_t object_buffer_limit = 64;
void reserve_members() void reserve_members()
{ {
+13 -1
View File
@@ -1510,7 +1510,19 @@ class serializer
{ {
flush(); flush();
} }
std::memcpy(write_buffer.data() + write_buffer_pos, s, length); #if defined(__GNUC__) && !defined(__clang__)
std::size_t copy_length = length;
// GCC knows from the test above that the length is below the size of
// the write buffer, and with that bound it may expand the memcpy into
// "rep movsq" (seen with -march=x86-64-v3 and -mtune=generic) instead
// of calling memcpy. For the short runs written here its start-up cost
// is far above that of the call (dump() with ensure_ascii 80 % slower
// on Sapphire Rapids), so hide the bound from the optimizer.
__asm__("" : "+r"(copy_length));
#else
const std::size_t copy_length = length;
#endif
std::memcpy(write_buffer.data() + write_buffer_pos, s, copy_length);
write_buffer_pos += length; write_buffer_pos += length;
} }
+15 -4
View File
@@ -17540,9 +17540,8 @@ class json_sax_dom_parser : public json_sax_dom_error_state
} }
/// the number of members an object collects on the stack before it /// the number of members an object collects on the stack before it
/// switches to storing them directly; this is the size from which the /// switches to storing them directly
/// stored object looks up its keys in a hash table (key_index_threshold) static constexpr std::size_t object_buffer_limit = 32;
static constexpr std::size_t object_buffer_limit = 64;
void reserve_members() void reserve_members()
{ {
@@ -39102,7 +39101,19 @@ class serializer
{ {
flush(); flush();
} }
std::memcpy(write_buffer.data() + write_buffer_pos, s, length); #if defined(__GNUC__) && !defined(__clang__)
std::size_t copy_length = length;
// GCC knows from the test above that the length is below the size of
// the write buffer, and with that bound it may expand the memcpy into
// "rep movsq" (seen with -march=x86-64-v3 and -mtune=generic) instead
// of calling memcpy. For the short runs written here its start-up cost
// is far above that of the call (dump() with ensure_ascii 80 % slower
// on Sapphire Rapids), so hide the bound from the optimizer.
__asm__("" : "+r"(copy_length));
#else
const std::size_t copy_length = length;
#endif
std::memcpy(write_buffer.data() + write_buffer_pos, s, copy_length);
write_buffer_pos += length; write_buffer_pos += length;
} }
+2 -2
View File
@@ -247,8 +247,8 @@ TEST_CASE("regression test for issue #5673 - ordered_json::emplace with a non-rv
namespace namespace
{ {
// the sizes around which the DOM parser changes how it stores the elements of // the sizes around which the DOM parser changes how it stores the elements of
// an array (16) and the members of an object in a vector (64, which is also // an array (16) and the members of an object in a vector (32, and 64 for the
// where the key index starts) // key index)
const std::array<std::size_t, 17> container_sizes = {{0, 1, 2, 3, 15, 16, 17, 18, 31, 32, 33, 34, 63, 64, 65, 66, 200}}; const std::array<std::size_t, 17> container_sizes = {{0, 1, 2, 3, 15, 16, 17, 18, 31, 32, 33, 34, 63, 64, 65, 66, 200}};
template<typename Json> template<typename Json>