Repository navigation
Conversation
Since jbeder#1221, node_data::insert_map_pair compares each new key with every key in the map already. Load inserts every key like this, so a map with n keys costs n^2/2 string comparisons. Track the scalar keys of each open map in NodeBuilder instead: a list for the first 16, then a hash set. NodeBuilder throws the same NonUniqueMapKey, with the same mark, just before the insert, and node_data::insert, whose only caller is NodeBuilder, skips its scan. The index stays out of node_data because node_data is exported from a public header, and a new member would change its layout. convert_sequence_to_map also skips the scan, since its keys are the sequence indexes.
ptweezy
marked this pull request as ready for review
November 9, 2026 03:50
Collaborator
|
Nice analysis! What is the reason for having a custom case for <16 elements? |
Author
Yeah, after some testing I think we can just use hash-set. I was trying to avoid the cost of a hash-set and keep small maps as cheap to load as they were before because most YAML maps don't have that many keys. But testing shows simplifying to just using a hash-set is pretty close to the same performance. |
This branch has not been deployed
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.
#1221 added a check to
node_data::insert_map_pairthat compares each new key with every key in the map already.YAML::Loadgoes through it for every key, so loading a map is quadratic. For example, a 64,000-key map takes about 17s. The check is not in a release yet, so the slowdown would ship in the next one.This moves the check into
NodeBuilder, which keeps the scalar keys of each open map: a list for the first 16, then astd::unordered_set<std::string>. Maps with up to 16 keys never use the hash set. The 4- and 20-key rows below, under and just over that limit, show no slowdown. It throwsNonUniqueMapKeybefore the insert, andnode_data::insert(only caller:NodeBuilder) passesforce = true.The index is not in
node_databecause that class is exported from a public header and a new member would change its layout.YAML::Loadtiming, MSVC 14.51, Release, median of 5:convert_sequence_to_mapalso passesforce = true, since its keys are the sequence indexes. It had the same quadratic cost.