-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Optimize JSON parsing a bit #14366
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
Merged
Merged
Optimize JSON parsing a bit #14366
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8c3bd22
JSON: lex per byte, not per char (string case)
fe846a8
JSON: lex per byte, not per char (IO case)
31df1ee
JSON (IO): try to use peek buffer
c083651
Continue parsing after escape slash
b69dbcf
No need to store and parse string value for "small" integers
48d7b48
Use peek bufffer before skipping
69c8a20
Less times incrementing column number
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 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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| # :nodoc: | ||
| class JSON::Lexer::StringBased < JSON::Lexer | ||
| def initialize(string) | ||
| def initialize(string : String) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. polish: This could be |
||
| super() | ||
| @reader = Char::Reader.new(string) | ||
| @string = string | ||
| @pos = 0 | ||
| @number_start = 0 | ||
| end | ||
|
|
||
|
|
@@ -33,7 +34,7 @@ class JSON::Lexer::StringBased < JSON::Lexer | |
| if @expects_object_key | ||
| start_pos += 1 | ||
| end_pos = current_pos - 1 | ||
| @token.string_value = @string_pool.get(@reader.string.to_unsafe + start_pos, end_pos - start_pos) | ||
| @token.string_value = @string_pool.get(@string.to_unsafe + start_pos, end_pos - start_pos) | ||
| else | ||
| @token.string_value = string_range(start_pos + 1, current_pos - 1) | ||
| end | ||
|
|
@@ -47,27 +48,30 @@ class JSON::Lexer::StringBased < JSON::Lexer | |
| end | ||
|
|
||
| private def current_pos | ||
| @reader.pos | ||
| @pos | ||
| end | ||
|
|
||
| def string_range(start_pos, end_pos) : String | ||
| @reader.string.byte_slice(start_pos, end_pos - start_pos) | ||
| @string.byte_slice(start_pos, end_pos - start_pos) | ||
| end | ||
|
|
||
| def slice_range(start_pos, end_pos) : Bytes | ||
| @reader.string.to_slice[start_pos, end_pos - start_pos] | ||
| @string.to_slice[start_pos, end_pos - start_pos] | ||
| end | ||
|
|
||
| private def next_char_no_column_increment | ||
| char = @reader.next_char | ||
| if char == '\0' && @reader.pos != @reader.string.bytesize | ||
| @pos += 1 | ||
|
|
||
| char = current_char | ||
| if char == '\0' && @pos != @string.bytesize | ||
| unexpected_char | ||
| end | ||
|
|
||
| char | ||
| end | ||
|
|
||
| private def current_char | ||
| @reader.current_char | ||
| @string.to_unsafe[@pos].chr | ||
| end | ||
|
|
||
| private def number_start | ||
|
|
||
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
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.
thought: I'm wondering if the strategy here could be based on byte search (
peek.index('"')) instead? The implementation for that can be more efficient than iterating over each byte.Slice#indexon a byte buffer is backed bymemchr, so it depends on how much the libc implementation is optimized.We still need to sanity check for escape sequences and unallowed characters in the potential string, though. So that would reduce effectiveness. I think it could overall be better that way, but I'm not sure. Just wanted to leave this thought here. It should be good to take the current implementation for now.
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.
Might be true. When I needed to find the end of a JSON string in a
Bytes, I came up with something like this:I did not check if it makes sense to use
strpbrk() to find the next "interesting" byte, such as quote, backslash, ...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.
That's the other optimization I was thinking. It makes string parsing much faster. I didn't want to include it in this PR though!