Skip to content

Commit

Permalink
Improve the performance of MultipleMemoizedHelpers
Browse files Browse the repository at this point in the history
Fetching memoized helpers for each of the ancestors is not optimal, is
not optimal and has non-linear complexity.
  • Loading branch information
pirj committed Aug 9, 2020
1 parent edb87e8 commit 986537f
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/rubocop/cop/rspec/multiple_memoized_helpers.rb
Expand Up @@ -100,8 +100,14 @@ def on_block(node)
add_offense(node, message: format(MSG, count: count, max: max))
end

def on_new_investigation
@example_group_memoized_helpers = {}
end

private

attr_reader :example_group_memoized_helpers

def all_helpers(node)
[
*helpers(node),
Expand All @@ -110,13 +116,14 @@ def all_helpers(node)
end

def helpers(node)
variable_nodes(node).map do |variable_node|
if variable_node.block_type?
variable_definition?(variable_node.send_node)
else # block-pass (`let(:foo, &bar)`)
variable_definition?(variable_node)
@example_group_memoized_helpers[node] ||=
variable_nodes(node).map do |variable_node|
if variable_node.block_type?
variable_definition?(variable_node.send_node)
else # block-pass (`let(:foo, &bar)`)
variable_definition?(variable_node)
end
end
end
end

def variable_nodes(node)
Expand Down

0 comments on commit 986537f

Please sign in to comment.