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
* Optimize SourceBuffer
line and column handling
#755
Conversation
Prelude to another PR to improve behavior of |
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.
Looks good overall, thanks!
…mance) `#line_begins`: - store `line_begin` instead of `[line_begin, index]` - simplify code by storing an additional "virtual" line_begin at the end caches for `position` => line / column: Use same cache instead of two. `#decompose_position`: use cache there too I added tests for behavior of some methods with out-of-range arguments. Behavior arguably less bad.
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.
👍
SourceBuffer
line and column handling (memory and performance)SourceBuffer
line and column handling
I read this code for a long time and decided that the memoization of Anyway, it seems like it's not an issue since it's ultimately calculated from That said, nothing verifies that it was set the first time. So the API does have ways it can be used, which are incorrect. eg: # Versions
$ ruby -r parser -ve 'puts Parser::VERSION'
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [arm64-darwin20]
3.0.1.1
# Obvious way of using the lib explodes
$ ruby -r parser -e '
p Parser::Source::Buffer.new("some_file.rb").line_for_position(1)
'
Traceback (most recent call last):
3: from -e:2:in `<main>'
2: from /Users/josh/.gem/ruby/2.7.2/gems/parser-3.0.1.1/lib/parser/source/buffer.rb:223:in `line_for_position'
1: from /Users/josh/.gem/ruby/2.7.2/gems/parser-3.0.1.1/lib/parser/source/buffer.rb:332:in `line_index_for_position'
/Users/josh/.gem/ruby/2.7.2/gems/parser-3.0.1.1/lib/parser/source/buffer.rb:320:in `line_begins': undefined method `index' for nil:NilClass (NoMethodError)
# This is how the code seems intended to be used:
$ ruby -r parser -e '
p Parser::Source::Buffer.new("some_file.rb").tap { |b| b.source = "" }.line_for_position(1)
'
1 I assume we don't want to break the API in backwards incompatible ways, which is totally understandable. Or perhaps there's a use-case that I'm ignorant to. But, if the option is available and reasonable, then I'd advocate making eg: $ ruby -r parser -e '
p Parser::Source::Buffer.new("some_file.rb", source: "").line_for_position(1)
'
1 Cheers 🍻 |
@JoshCheek Note that you can now pass |
Aye 👍 I think this was released on a major version bump (I found this PR by looking through the changelog), but as that's past now, we probably can't make that change I mentioned. |
#line_begins
:line_begin
instead of[line_begin, index]
caches for
position
=> line / column: Use same cache instead of two.#decompose_position
: use cache there tooI added tests for behavior of some methods with out-of-range arguments.
Behavior arguably less bad after this PR.