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>