mirror of
https://github.com/nlohmann/json.git
synced 2026-10-14 18:27:13 +00:00
Like CBOR before #5824, MessagePack integer arrays were no faster than in 3.12.0: the type switch (number_integer vs. number_unsigned), the sign test, and the fixint/8/16/32/64-bit ladder mispredict on mixed data. They now take the CBOR approach: - number_integer and number_unsigned share one case of write_msgpack. - The format comes from the size class of x, where x is the value itself if it is not negative and 2 * (-1 - n) + 1 for a negative n: n fits int 8/16/32 exactly if that fits uint 8/16/32, so one table lookup serves both signs. The fixint bound (127, or -32) is selected through a mask, and the marker is 0xCB + class for uint and 0xCF + class for int. - Arrays are written by an out-of-line write_msgpack_array() with runs of integers encoded straight into space reserved from the sink. The CBOR integer code is factored into helpers both formats share (integer_bits, integer_size_class, encode_first_byte_and_tail, write_array_elements). The new path picks the format from the low 64 bits, so number types wider than that keep the previous range checks. The output is unchanged; the integer-array test now also checks MessagePack against an independent encoder. Signed-off-by: Niels Lohmann <mail@nlohmann.me>