Skip to content

Commit f1e25ec

Browse files
authored
Store method objects in constants (#1033)
It probably won't speed up things significantly, but these are hot paths and we can save a few method calls per completion/input call.
1 parent 9750fa2 commit f1e25ec

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

lib/irb/completion.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,27 +137,33 @@ def doc_namespace(preposing, matched, _postposing, bind:)
137137
end
138138

139139
class RegexpCompletor < BaseCompletor # :nodoc:
140+
KERNEL_METHODS = ::Kernel.instance_method(:methods)
141+
KERNEL_PRIVATE_METHODS = ::Kernel.instance_method(:private_methods)
142+
KERNEL_INSTANCE_VARIABLES = ::Kernel.instance_method(:instance_variables)
143+
OBJECT_CLASS_INSTANCE_METHOD = ::Object.instance_method(:class)
144+
MODULE_CONSTANTS_INSTANCE_METHOD = ::Module.instance_method(:constants)
145+
140146
using Module.new {
141147
refine ::Binding do
142148
def eval_methods
143-
::Kernel.instance_method(:methods).bind(eval("self")).call
149+
KERNEL_METHODS.bind_call(receiver)
144150
end
145151

146152
def eval_private_methods
147-
::Kernel.instance_method(:private_methods).bind(eval("self")).call
153+
KERNEL_PRIVATE_METHODS.bind_call(receiver)
148154
end
149155

150156
def eval_instance_variables
151-
::Kernel.instance_method(:instance_variables).bind(eval("self")).call
157+
KERNEL_INSTANCE_VARIABLES.bind_call(receiver)
152158
end
153159

154160
def eval_global_variables
155-
::Kernel.instance_method(:global_variables).bind(eval("self")).call
161+
::Kernel.global_variables
156162
end
157163

158164
def eval_class_constants
159-
klass = ::Object.instance_method(:class).bind_call(receiver)
160-
::Module.instance_method(:constants).bind_call(klass)
165+
klass = OBJECT_CLASS_INSTANCE_METHOD.bind_call(receiver)
166+
MODULE_CONSTANTS_INSTANCE_METHOD.bind_call(klass)
161167
end
162168
end
163169
}

lib/irb/context.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ module IRB
1313
# A class that wraps the current state of the irb session, including the
1414
# configuration of IRB.conf.
1515
class Context
16+
KERNEL_PUBLIC_METHOD = ::Kernel.instance_method(:public_method)
17+
KERNEL_METHOD = ::Kernel.instance_method(:method)
18+
1619
ASSIGN_OPERATORS_REGEXP = Regexp.union(%w[= += -= *= /= %= **= &= |= &&= ||= ^= <<= >>=])
1720
# Creates a new IRB context.
1821
#
@@ -648,8 +651,8 @@ def parse_command(code)
648651
return if local_variables.include?(command)
649652

650653
# Check visibility
651-
public_method = !!Kernel.instance_method(:public_method).bind_call(main, command) rescue false
652-
private_method = !public_method && !!Kernel.instance_method(:method).bind_call(main, command) rescue false
654+
public_method = !!KERNEL_PUBLIC_METHOD.bind_call(main, command) rescue false
655+
private_method = !public_method && !!KERNEL_METHOD.bind_call(main, command) rescue false
653656
if Command.execute_as_command?(command, public_method: public_method, private_method: private_method)
654657
[command, arg]
655658
end

0 commit comments

Comments
 (0)