Repository navigation
Eliminate temporary basic_json allocations for object keys in CBOR and MessagePack (#5318) - #5763
Closed
SaurabhCodes-16 wants to merge 1 commit into
Closed
SaurabhCodes-16 wants to merge 1 commit into
SaurabhCodes-16 wants to merge 1 commit into
Conversation
…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>
Owner
|
This is a duplicate of #5328. |
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.
What & Why
In
write_cbor()andwrite_msgpack(), serializing object keys previously calledwrite_cbor(el.first)andwrite_msgpack(el.first). Becauseel.firstis astring_tand those functions takeconst BasicJsonType&, each object key was implicitly converted to a temporarybasic_json, incurring heap allocations for every single key in every object being serialized.This PR introduces dedicated
write_cbor_string()andwrite_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 temporarybasic_jsonheap allocations during object key serialization in CBOR and MessagePack.Benchmark
Serialization benchmark for a 100,000-key JSON object:
Closes #5318
OSS-Fuzz: <id>.make amalgamate.