From f1e25ec7aeeda8e2f5d1de98b461fb95766f8cf2 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 20 Nov 2024 18:59:23 +0000 Subject: [PATCH] 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. --- lib/irb/completion.rb | 18 ++++++++++++------ lib/irb/context.rb | 7 +++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/irb/completion.rb b/lib/irb/completion.rb index 36a8b084f..3e9704706 100644 --- a/lib/irb/completion.rb +++ b/lib/irb/completion.rb @@ -137,27 +137,33 @@ def doc_namespace(preposing, matched, _postposing, bind:) end class RegexpCompletor < BaseCompletor # :nodoc: + KERNEL_METHODS = ::Kernel.instance_method(:methods) + KERNEL_PRIVATE_METHODS = ::Kernel.instance_method(:private_methods) + KERNEL_INSTANCE_VARIABLES = ::Kernel.instance_method(:instance_variables) + OBJECT_CLASS_INSTANCE_METHOD = ::Object.instance_method(:class) + MODULE_CONSTANTS_INSTANCE_METHOD = ::Module.instance_method(:constants) + using Module.new { refine ::Binding do def eval_methods - ::Kernel.instance_method(:methods).bind(eval("self")).call + KERNEL_METHODS.bind_call(receiver) end def eval_private_methods - ::Kernel.instance_method(:private_methods).bind(eval("self")).call + KERNEL_PRIVATE_METHODS.bind_call(receiver) end def eval_instance_variables - ::Kernel.instance_method(:instance_variables).bind(eval("self")).call + KERNEL_INSTANCE_VARIABLES.bind_call(receiver) end def eval_global_variables - ::Kernel.instance_method(:global_variables).bind(eval("self")).call + ::Kernel.global_variables end def eval_class_constants - klass = ::Object.instance_method(:class).bind_call(receiver) - ::Module.instance_method(:constants).bind_call(klass) + klass = OBJECT_CLASS_INSTANCE_METHOD.bind_call(receiver) + MODULE_CONSTANTS_INSTANCE_METHOD.bind_call(klass) end end } diff --git a/lib/irb/context.rb b/lib/irb/context.rb index bcbbb8b82..c65628192 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -13,6 +13,9 @@ module IRB # A class that wraps the current state of the irb session, including the # configuration of IRB.conf. class Context + KERNEL_PUBLIC_METHOD = ::Kernel.instance_method(:public_method) + KERNEL_METHOD = ::Kernel.instance_method(:method) + ASSIGN_OPERATORS_REGEXP = Regexp.union(%w[= += -= *= /= %= **= &= |= &&= ||= ^= <<= >>=]) # Creates a new IRB context. # @@ -648,8 +651,8 @@ def parse_command(code) return if local_variables.include?(command) # Check visibility - public_method = !!Kernel.instance_method(:public_method).bind_call(main, command) rescue false - private_method = !public_method && !!Kernel.instance_method(:method).bind_call(main, command) rescue false + public_method = !!KERNEL_PUBLIC_METHOD.bind_call(main, command) rescue false + private_method = !public_method && !!KERNEL_METHOD.bind_call(main, command) rescue false if Command.execute_as_command?(command, public_method: public_method, private_method: private_method) [command, arg] end