mirror of
https://github.com/nlohmann/json.git
synced 2026-10-14 18:27:13 +00:00
The vector-returning to_cbor(), to_msgpack(), ... wrote through output_vector_sink, which appended each write with push_back() or insert(). A number's 1 to 8 payload bytes went through vector::insert(), which libc++ and libstdc++ both run as an out-of-line insert-anywhere routine; on integer arrays that call cost more than encoding the number, so the devirtualization of #5286 brought nothing there, and with GCC the writer was slower than in 3.12.0. output_vector_sink now keeps its own write position: it grows the vector geometrically ahead of the position (resize(), using any capacity the vector already has), copies each write there, and shrinks the vector to the written length in its destructor, also when a write throws. Writes of 256 characters or more that do not fit are appended with insert(), so a large string or binary value is copied once instead of zero-filled and copied. The sink is move-only, as it owns that trim. output_vector_adapter (the to_*(j, vector) overloads) appends with push_back()/insert() again, so a caller holding the adapter never sees the space kept ahead. Signed-off-by: Niels Lohmann <mail@nlohmann.me>