summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorStan Lo <[email protected]>2023-06-30 18:41:56 +0100
committergit <[email protected]>2023-06-30 17:42:00 +0000
commit136fcd5118b844bb8399d69dc44414cb3bd2028a (patch)
treedd5fb9e08bf8d97423ad981e71f6ae8fc527f65a /lib
parent94788a6d13a136f28daca2a51ad7945c52b2311d (diff)
[ruby/irb] Reduce internal operations' exposure to benchmarking
(https://github.com/ruby/irb/pull/618) * Test last value is assigned with measure enabled * Remove unnecessary `result` variable `Context#evaluate` always assigns the result of the evaluation to `_` so we don't need to do it in `Irb#eval_input`. * Move benchmarking logic into `Context#evaluate` Current location of the benchmarking logic is too high up and includes operations like command loading and argument transformation, which should be excluded. So this commit moves it into `Context#evaluate` to reduce the noise. We don't move it further down to `Workspace#evaluate` because `Context` is an argument of the measure block, which is not available in `Workspace`.
Diffstat (limited to 'lib')
-rw-r--r--lib/irb.rb20
-rw-r--r--lib/irb/context.rb24
2 files changed, 25 insertions, 19 deletions
diff --git a/lib/irb.rb b/lib/irb.rb
index ed79160b24..64c716fd4c 100644
--- a/lib/irb.rb
+++ b/lib/irb.rb
@@ -558,26 +558,10 @@ module IRB
@scanner.each_top_level_statement do |line, line_no|
signal_status(:IN_EVAL) do
begin
- if IRB.conf[:MEASURE] && IRB.conf[:MEASURE_CALLBACKS].empty?
- IRB.set_measure_callback
- end
# Assignment expression check should be done before evaluate_line to handle code like `a /2#/ if false; a = 1`
is_assignment = assignment_expression?(line)
- if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty?
- result = nil
- last_proc = proc{ result = evaluate_line(line, line_no) }
- IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) { |chain, item|
- _name, callback, arg = item
- proc {
- callback.(@context, line, line_no, arg) do
- chain.call
- end
- }
- }.call
- @context.set_last_value(result)
- else
- evaluate_line(line, line_no)
- end
+ evaluate_line(line, line_no)
+
if @context.echo?
if is_assignment
if @context.echo_on_assignment?
diff --git a/lib/irb/context.rb b/lib/irb/context.rb
index 58964aa832..6f209b596a 100644
--- a/lib/irb/context.rb
+++ b/lib/irb/context.rb
@@ -475,7 +475,29 @@ module IRB
def evaluate(line, line_no) # :nodoc:
@line_no = line_no
- set_last_value(@workspace.evaluate(line, irb_path, line_no))
+ result = nil
+
+ if IRB.conf[:MEASURE] && IRB.conf[:MEASURE_CALLBACKS].empty?
+ IRB.set_measure_callback
+ end
+
+ if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty?
+ last_proc = proc do
+ result = @workspace.evaluate(line, irb_path, line_no)
+ end
+ IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) do |chain, item|
+ _name, callback, arg = item
+ proc do
+ callback.(self, line, line_no, arg) do
+ chain.call
+ end
+ end
+ end.call
+ else
+ result = @workspace.evaluate(line, irb_path, line_no)
+ end
+
+ set_last_value(result)
end
def inspect_last_value # :nodoc: