Skip to content

Commit

Permalink
Memoize irb_path existence to reduce file existence check calculating…
Browse files Browse the repository at this point in the history
… eval_path
  • Loading branch information
tompng committed Feb 12, 2024
1 parent 2cd06f8 commit 125693c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,6 @@ def evaluate(line, line_no) # :nodoc:
IRB.set_measure_callback
end

# We need to use differente path to distinguish source_location of method defined in the actual file and method defined in irb session.
eval_path = File.exist?(irb_path) ? "#{irb_path}(#{IRB.conf[:IRB_NAME]})" : irb_path

if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty?
last_proc = proc do
result = @workspace.evaluate(line, eval_path, line_no)
Expand All @@ -577,6 +574,14 @@ def evaluate(line, line_no) # :nodoc:
set_last_value(result)
end

private def eval_path
# We need to use differente path to distinguish source_location of method defined in the actual file and method defined in irb session.
if !defined?(@irb_path_existence) || @irb_path_existence[0] != irb_path
@irb_path_existence = [irb_path, File.exist?(irb_path)]
end
@irb_path_existence[1] ? "#{irb_path}(#{IRB.conf[:IRB_NAME]})" : irb_path
end

def inspect_last_value # :nodoc:
@inspect_method.inspect_value(@last_value)
end
Expand Down
9 changes: 9 additions & 0 deletions test/irb/test_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,15 @@ def test_lineno
], out)
end

def test_eval_path
@context.irb_path = __FILE__
assert_equal("#{__FILE__}(irb)", @context.send(:eval_path))
@context.irb_path = 'file/does/not/exist'
assert_equal('file/does/not/exist', @context.send(:eval_path))
@context.irb_path = "#{__FILE__}(irb)"
assert_equal("#{__FILE__}(irb)", @context.send(:eval_path))
end

def test_build_completor
verbose, $VERBOSE = $VERBOSE, nil
original_completor = IRB.conf[:COMPLETOR]
Expand Down

0 comments on commit 125693c

Please sign in to comment.