Pre-size valdi_core bridge dictionaries and maps#114
Open
ultramiraculous wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
🎉 Thanks for your first contribution to Valdi!
A maintainer will review your PR soon. Here are a few things to check while you wait:
- ✅ All tests pass (
bazel test //...) - ✅ Your changes follow our coding standards
- ✅ You've added tests for your changes (if applicable)
- ✅ You've updated documentation (if needed)
Join our Discord community if you have questions!
clholgat
approved these changes
Jul 10, 2026
clholgat
left a comment
Collaborator
There was a problem hiding this comment.
This looks good, thanks for PR!
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.
[iOS] Pre-size valdi_core bridge dictionaries and maps
Description
We ran into a workload that pushes a large number of small dictionaries (4-8 keys each) through the validi bridge. The bridge builds its dictionaries/maps with no capacity hint, so each one starts at the minimum size and grows as it fills — at 4-8 keys that's multiple resize boundaries crossed per item, each one a rehash/realloc plus a free of the smaller backing buffer. Multiply by the number of items and we're churning a lotttt of transient memory for dictionaries whose final size we already know up front.
The array bridging cases elsewhere in this file already do pre-allocation, so it makes sense to mirror that, I think.
NSObjectFromValue): build theMapandTypedObjectNSMutableDictionarywithdictionaryWithCapacity:(map size / property count), matching theArraycase right below it.ValueFromNSDictionary):reserve(dict.count)on theValueMapbefore the insert loop, matching the typed and array paths.There's also obviously some bigger optimization options but this one stood out as a good "starter bug" candidate.
Type of Change
Testing
bazel test //...)Testing Details
Covered by the existing test suite (
valdi/test/ios/SCValdiMarshallerTests.m) —testCanMarshallMap/testCanUnmarshallMapround-trip an untypedNSDictionarythrough both changed functions (ValueFromNSDictionaryandNSObjectFromValue's map case). No new tests since only the initial capacity changes, not the contents or ordering.Ran
bazel test //...locally: 63/72 targets pass, including everything this change compiles into (//valdi:valdi_ios_objc_test,valdi_ios_swift_test,valdi_macos_objc_test). The 7 that fail (e.g.//valdi:test_java,//libs/dummy:test) fail identically on a cleanmaincheckout, so that's pre-existing local breakage rather than anything from this change.Checklist
Related Issues
Additional Context