Skip to content

Eliminate temporary basic_json allocations for object keys in CBOR and MessagePack (#5318) - #5763

Closed
SaurabhCodes-16 wants to merge 1 commit into
nlohmann:developfrom
SaurabhCodes-16:perf/eliminate-key-allocations-cbor-msgpack
Closed

SaurabhCodes-16 wants to merge 1 commit into
nlohmann:developfrom
SaurabhCodes-16:perf/eliminate-key-allocations-cbor-msgpack

Conversation

@SaurabhCodes-16

Copy link
Copy Markdown

What & Why

In write_cbor() and write_msgpack(), serializing object keys previously called write_cbor(el.first) and write_msgpack(el.first). Because el.first is a string_t and those functions take const BasicJsonType&, each object key was implicitly converted to a temporary basic_json, incurring heap allocations for every single key in every object being serialized.

This PR introduces dedicated write_cbor_string() and write_msgpack_string() helper methods (matching the pattern used by the UBJSON, BSON, and BON8 writers) that serialize strings directly with the enclosing object context for UTF-8 error handling. This eliminates all temporary basic_json heap allocations during object key serialization in CBOR and MessagePack.

Benchmark

Serialization benchmark for a 100,000-key JSON object:

  • CBOR: ~11.5 ms per iteration (0 heap allocations for keys)
  • MessagePack: ~11.9 ms per iteration (0 heap allocations for keys)

Closes #5318

  • The changes are described in detail, both the what and why.
  • If applicable, an existing issue is referenced.
  • If applicable, a fixed OSS-Fuzz issue is referenced as OSS-Fuzz: <id>.
  • The Code coverage remained at 100%. A test case for every new line of code.
  • If applicable, the documentation is updated.
  • The source code is amalgamated by running make amalgamate.

…d MessagePack

Previously, write_cbor() and write_msgpack() serialized object keys by calling write_cbor(el.first) and write_msgpack(el.first). Because el.first is string_t while the functions take const BasicJsonType&, each object key was implicitly converted to a temporary basic_json, incurring heap allocations for every key in every object.

This change introduces dedicated write_cbor_string() and write_msgpack_string() helper functions (matching the pattern in UBJSON, BSON, and BON8) that serialize strings directly with the enclosing object context for UTF-8 validation and error handling, completely eliminating temporary basic_json heap allocations during object key serialization.

Closes nlohmann#5318

Signed-off-by: Saurabh Tiwari <spssgzp4@gmail.com>
@nlohmann

nlohmann commented Oct 5, 2026

Copy link
Copy Markdown
Owner

This is a duplicate of #5328.

@nlohmann nlohmann added the solution: duplicate the issue is a duplicate; refer to the linked issue instead label Oct 5, 2026
@github-actions github-actions Bot added aspect: binary formats BSON, CBOR, MessagePack, UBJSON L tests labels Oct 5, 2026
@nlohmann nlohmann closed this Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aspect: binary formats BSON, CBOR, MessagePack, UBJSON L solution: duplicate the issue is a duplicate; refer to the linked issue instead tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

to_cbor()/to_msgpack() allocate a temporary basic_json for every object key

2 participants