Avoid allocating temporary basic_json for cbor and msgpack object keys - #5328
Open
alexprabhat99 wants to merge 4 commits into
Open
Avoid allocating temporary basic_json for cbor and msgpack object keys#5328alexprabhat99 wants to merge 4 commits into
alexprabhat99 wants to merge 4 commits into
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
Signed-off-by: alexprabhat99 <alexpbara@gmail.com>
Signed-off-by: alexprabhat99 <alexpbara@gmail.com>
Signed-off-by: alexprabhat99 <alexpbara@gmail.com>
Signed-off-by: alexprabhat99 <alexpbara@gmail.com>
alexprabhat99
force-pushed
the
5318-cbor-msgpack-object-key-allocations
branch
from
July 28, 2026 16:34
38a5640 to
d10514d
Compare
Contributor
Author
|
Hi @nlohmann |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR optimizes CBOR and MessagePack serialization by avoiding the construction of a temporary basic_json object for every object key during serialization.
Previously, object keys were passed through write_cbor()/write_msgpack(), which expected a BasicJsonType and therefore implicitly constructed a temporary basic_json for each key. This introduced unnecessary heap allocations during serialization.
This change factors out the string serialization logic into dedicated helper functions and serializes object keys directly. The implementation preserves compatibility with custom object key types while eliminating the extra basic_json allocations for the common case where object_t::key_type is string_t.
A regression test has been added to verify compatibility with custom object key types.
Fixes #5318