Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace forwardable with explicit delegation for performance #312

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/rubocop/ast.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require 'parser'
require 'forwardable'
require 'set'

require_relative 'ast/ext/range'
Expand Down
10 changes: 7 additions & 3 deletions lib/rubocop/ast/node/mixin/collection_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ module RuboCop
module AST
# A mixin that helps give collection nodes array polymorphism.
module CollectionNode
extend Forwardable

ARRAY_METHODS =
(Array.instance_methods - Object.instance_methods - [:to_a]).freeze
private_constant :ARRAY_METHODS

def_delegators :to_a, *ARRAY_METHODS
ARRAY_METHODS.each do |method|
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{method}(...) # def key?(...)
to_a.#{method}(...) # to_a.key?(...)
end # end
RUBY
end
end
end
end
15 changes: 12 additions & 3 deletions lib/rubocop/ast/node_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def def_node_search(method_name, pattern_str, **keyword_defaults)
end
end

extend Forwardable
include MethodDefiner
Invalid = Class.new(StandardError)

Expand All @@ -72,8 +71,6 @@ def self.descend(element, &block)

attr_reader :pattern, :ast, :match_code

def_delegators :@compiler, :captures, :named_parameters, :positional_parameters

def initialize(str, compiler: Compiler.new)
@pattern = str
@ast = compiler.parser.parse(str)
Expand All @@ -82,6 +79,18 @@ def initialize(str, compiler: Compiler.new)
@cache = {}
end

def captures
@compiler.captures
end

def named_parameters
@compiler.named_parameters
end

def positional_parameters
@compiler.positional_parameters
end

def match(*args, **rest, &block)
@cache[:lambda] ||= as_lambda
@cache[:lambda].call(*args, block: block, **rest)
Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/ast/node_pattern/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class NodePattern
# Doc on how this fits in the compiling process:
# /docs/modules/ROOT/pages/node_pattern.adoc
class Compiler
extend Forwardable
attr_reader :captures, :named_parameters, :positional_parameters, :binding

def initialize
Expand All @@ -21,7 +20,9 @@ def initialize
@atom_subcompiler = self.class::AtomSubcompiler.new(self)
end

def_delegators :binding, :bind
def bind(...)
@binding.bind(...)
end

def positional_parameter(number)
@positional_parameters = number if number > @positional_parameters
Expand Down
8 changes: 7 additions & 1 deletion lib/rubocop/ast/node_pattern/compiler/debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ def parser
@parser ||= Parser::WithMeta.new
end

def_delegators :parser, :comments, :tokens
def comments
parser.comments
end

def tokens
parser.tokens
end

# @api private
module InstrumentationSubcompiler
Expand Down
9 changes: 7 additions & 2 deletions lib/rubocop/ast/node_pattern/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module AST
class NodePattern
# Base class for AST Nodes of a `NodePattern`
class Node < ::Parser::AST::Node
extend Forwardable
include ::RuboCop::AST::Descendence

MATCHES_WITHIN_SET = %i[symbol number string].to_set.freeze
Expand Down Expand Up @@ -95,7 +94,13 @@ def in_sequence_head
# Node class for `$something`
class Capture < Node
# Delegate most introspection methods to it's only child
def_delegators :child, :arity, :rest?
def arity
child.arity
end

def rest?
child.rest?
end

def capture?
true
Expand Down
16 changes: 11 additions & 5 deletions lib/rubocop/ast/node_pattern/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class NodePattern
# Doc on how this fits in the compiling process:
# /docs/modules/ROOT/pages/node_pattern.adoc
class Parser < Racc::Parser
extend Forwardable

Builder = NodePattern::Builder
Lexer = NodePattern::Lexer

Expand Down Expand Up @@ -43,9 +41,17 @@ def inspect

private

def_delegators :@builder, :emit_list, :emit_unary_op, :emit_atom, :emit_capture,
:emit_call, :emit_union
def_delegators :@lexer, :next_token
%i[emit_list emit_unary_op emit_atom emit_capture emit_call emit_union].each do |method|
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{method}(...) # def emit_list(...)
@builder.#{method}(...) # @builder.emit_list(...)
end # end
RUBY
end

def next_token
@lexer.next_token
end

def enforce_unary(node)
return node if node.arity == 1
Expand Down