-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parser: Optimize JSON-attribute parsing #11369
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think I understand this term:
}+(?=})
. Among other things, I don't understand why+
should be applied to that first}
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the history is in #11355
I guess we could go in and add some explanatory comments here but we're creating two new matching groups here based on a "fast parse" pathway
we know that specifically we want to consume
((?!}\s+-->).)
, but the problem with that is that for every position in the input we're allocating some memory to hold the match group and then we also start backtracking once the match fails. this is unlike the autogenerated PEG parser because PEGs don't backtrack.therefore, I looked at what was causing things to be slow and realized that we know more about our inputs than the RegExp does, that is, the majority of our characters won't even be
}
. taking this knowledge lets us encode that fast-path and fallback to the slow pathwe can compare these two methods with a simplified test.
we'll use the simple test data
< {"a": "b"} >
.make sure to click on the debugger in these to see what's
happening inside the RegExp engine.
(?:<\s+)({(?:(?!}\s+>).)*})(?:\s+>)
- 1 match, 51 steps - https://regex101.com/r/UmdrvA/1(?:<\s+)({(?:(?:[^}]+)|(?:(?!}\s+>).))*})(?:\s+>)
- 1 match, 26 steps - https://regex101.com/r/cXcMFw/1if we make the input bigger we can watch in the debugger how the
[}]+
can consume a long string of text in one step and skip all the backtracking.that leaves us a bad case where we end up with deeply-nested JSON though and in the content we could imagine
}}}}}}}}} -->
. the}+(?=})
bit then is a second-order optimization to consume a string of closing brackets up to the last one in the string (?=
is the positive lookahead to say "match the closer but don't consume it).by introducing a new alternation group we've made a new possible branch in the execution but on the other hand it's fast to consume a string of closers. the goal is mainly to remove the backtracking which opens a denial-of-service attack and can dramatically bloat the execution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the thorough explanation <3