Make KeyValidator faster by sorting keys and using binary search algorithm (Array#bsearch) - attempt #2 #461
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.
This is revised attempt of making
KeyValidator
class faster when checking for existence of given key. Previous one was broken.The problem with previous version was that it tried to search for all 3 possibilities (
key
,key + "[]"
andkey + "."
) at once. Consider this schema:Now we want to validate it with a hash:
Previous version would start with comparing
foo
key withfooA
key (because it is in the middle of sorted paths["a", "fooA", "foo[].bar"]
). It does not match, so what we should do? Should we go to the left or right? It depends on schema. Whenfoo
is a scalar thenfoo
key would be beforefooA
in the sorted paths, so we should go left. Forfoo
being an array we should go right (foo[]
is afterfooA
).Simply saying: it wasn't possible to do that in a single pass.
New algorithm searches for
foo
(exact match),foo.
(prefix match) andfoo[]
(prefix match) separately. Maximum 3 searches, each O(N*logN) complexity which is still O(N*logN) in total.I added tests which does not pass with previous approach and does pass with new one.
Hope this time I didn't miss anything. And sorry for the delay :).