Skip to content
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

Improve performance of show_source for large class #249

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/irb/cmd/show_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,18 @@ def find_source(str)
def find_end(file, first_line)
return first_line unless File.exist?(file)
lex = RubyLex.new
code = +""
File.read(file).lines[(first_line - 1)..-1].each_with_index do |line, i|
_ltype, _indent, continue, code_block_open = lex.check_state(code << line)
lines = File.read(file).lines[(first_line - 1)..-1]
tokens = RubyLex.ripper_lex_without_warning(lines.join)
prev_tokens = []

# chunk with line number
tokens.chunk { |tok| tok[0][0] }.each do |lnum, chunk|
code = lines[0..lnum].join
Comment on lines +64 to +68
Copy link
Member

@k0kubun k0kubun Jun 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prev_tokens = []
# chunk with line number
tokens.chunk { |tok| tok[0][0] }.each do |lnum, chunk|
code = lines[0..lnum].join
code = +""
prev_tokens = []
# chunk with line number
tokens.chunk { |tok| tok[0][0] }.each do |lnum, chunk|
code << lines[lnum]

You changed the way code is constructed, but this seems like a new bottleneck in your code. The original code seems optimal.

before

ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
Rehearsal --------------------------------------------------------
                       2.401019   0.011546   2.412565 (  2.414266)
----------------------------------------------- total: 2.412565sec

                           user     system      total        real
                       2.420137   0.005364   2.425501 (  2.426264)

after

ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
Rehearsal --------------------------------------------------------
                       1.028879   0.009408   1.038287 (  1.039615)
----------------------------------------------- total: 1.038287sec

                           user     system      total        real
                       1.000221   0.007454   1.007675 (  1.008295)

Copy link
Member

@k0kubun k0kubun Jun 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(The code I suggested might skip lines that don't have any token, but I assume such lines don't impact compilation error checks. Such lines seem to have an on_ignored_nl token.)

prev_tokens.concat chunk
continue = lex.process_continue(prev_tokens)
code_block_open = lex.check_code_block(code, prev_tokens)
if !continue && !code_block_open
return first_line + i
return first_line + lnum
end
end
first_line
Expand Down