summaryrefslogtreecommitdiff
path: root/lib/irb/context.rb
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/irb/context.rb
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/irb/context.rb')
-rw-r--r--lib/irb/context.rb24
1 files changed, 23 insertions, 1 deletions
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: