Skip to content

Avoid quadratic duplicate-key check in YAML::Load - #1522

Open
ptweezy wants to merge 2 commits into
jbeder:masterfrom
ptweezy:faster-unique-keys
Open

ptweezy wants to merge 2 commits into
jbeder:masterfrom
ptweezy:faster-unique-keys

Conversation

@ptweezy

@ptweezy ptweezy commented Nov 9, 2026 •

Copy link
Copy Markdown

#1221 added a check to node_data::insert_map_pair that compares each new key with every key in the map already. YAML::Load goes 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 a std::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 throws NonUniqueMapKey before the insert, and node_data::insert (only caller: NodeBuilder) passes force = true.

The index is not in node_data because that class is exported from a public header and a new member would change its layout.

YAML::Load timing, MSVC 14.51, Release, median of 5:

Input Before After
1 map, 1,000 keys 4.0 ms 2.5 ms
1 map, 4,000 keys 38 ms 10 ms
1 map, 16,000 keys 1.04 s 45 ms
1 map, 64,000 keys 16.8 s 179 ms
100,000 maps × 4 keys 1.11 s 1.09 s
20,000 maps × 20 keys 1.11 s 1.10 s
2,000 maps × 200 keys 1.16 s 1.05 s

convert_sequence_to_map also passes force = true, since its keys are the sequence indexes. It had the same quadratic cost.

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
ptweezy marked this pull request as ready for review November 9, 2026 03:50
@SGSSGene

SGSSGene commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Nice analysis!

What is the reason for having a custom case for <16 elements?
wouldnt it be easier to just use the hash-set?

@ptweezy

ptweezy commented Nov 9, 2026 •

Copy link
Copy Markdown
Author

Nice analysis!

What is the reason for having a custom case for <16 elements? wouldnt it be easier to just use the hash-set?

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants