Repository navigation
Check duplicate keys when a non String/Symbol key follows a String/Symbol key - #1126
Merged
byroot merged 1 commit intoNov 5, 2026
Conversation
stephanmoneybird
force-pushed
the
generator-duplicate-key-check-key-order
branch
from
November 5, 2026 13:09
2918b3c to
91a2fbc
Compare
byroot
reviewed
Nov 5, 2026
byroot
force-pushed
the
generator-duplicate-key-check-key-order
branch
2 times, most recently
from
November 5, 2026 17:29
5d9ac7e to
0153951
Compare
…mbol key Fix: #1125 Co-Authored-By: Jean Boussier <byroot@ruby-lang.org>
byroot
force-pushed
the
generator-duplicate-key-check-key-order
branch
from
November 5, 2026 17:37
0153951 to
2e9d3e9
Compare
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.
Fix: #1125
The
defaultbranch ofjson_object_iconverted the key withto_swithout the mixed keys check theT_STRINGandT_SYMBOLbranches have, so{ "1" => 1, 1 => 2 }generated{"1":1,"1":2}while{ 1 => 1, "1" => 2 }raised. It now callsjson_inspect_hash_with_mixed_keyswhen the first key was a String or Symbol. The TruffleRuby and Java generators already catch this case.Hashes whose keys all have the same type pay one extra comparison per non String/Symbol key. A hash that starts with a String or Symbol key and then has another key type now runs
JSON.on_mixed_keys_hashonce, which is the cost the reverse order already paid:The
integer keyscases are(1..1000).to_h { |i| [i, i] }andArray.new(1000) { |i| { i => i, i + 1 => i } }; the mixed cases put a"a"key before or after the Integer keys. Ruby 4.0.9, arm64-darwin.So the mixed String-then-Integer order gets 3 to 4 times slower, the same as Integer-then-String already is. If that is too much, the cost is in
JSON.on_mixed_keys_hash, which allocates a String per key and a Hash per inspected hash, and moving that check to C would speed up both orders.A more general condition is possible as well: a single
key_type != arg->first_key_typecheck before theswitch, instead of the three per-branch checks, would also catch{ 1 => :a, obj => :b }withobj.to_s == "1", which the C extension still generates while the Java and TruffleRuby generators raise. It would also inspect hashes mixingIntegerandFloat, or fixnums and bignums, which can never contain a duplicate. I kept the narrower check discussed in #1125, but I'm happy to switch.