Skip to content

Commit

Permalink
Reduce allocated Sets
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima authored and bbatsov committed Jul 11, 2020
1 parent 5cb29ef commit 94430a8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
16 changes: 12 additions & 4 deletions lib/rubocop/cop/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def add_global_offense(message = nil, severity: nil)
# If message is not specified, the method `message` will be called.
def add_offense(node_or_range, message: nil, severity: nil, &block)
range = range_from_node_or_range(node_or_range)
return unless @current_offense_locations.add?(range)
return unless current_offense_locations.add?(range)

range_to_pass = callback_argument(range)

Expand Down Expand Up @@ -271,11 +271,19 @@ def correction_strategy

### Reserved for Commissioner:

def current_offense_locations
@current_offense_locations ||= Set.new
end

def currently_disabled_lines
@currently_disabled_lines ||= Set.new
end

# Called before any investigation
def begin_investigation(processed_source)
@current_offenses = []
@current_offense_locations = Set[]
@currently_disabled_lines = Set[]
@current_offense_locations = nil
@currently_disabled_lines = nil
@processed_source = processed_source
@current_corrector = Corrector.new(@processed_source) if @processed_source.valid_syntax?
end
Expand Down Expand Up @@ -327,7 +335,7 @@ def attempt_correction(range, corrector)

def disable_uncorrectable(range)
line = range.line
return unless @currently_disabled_lines.add?(line)
return unless currently_disabled_lines.add?(line)

disable_offense(range)
end
Expand Down
9 changes: 5 additions & 4 deletions lib/rubocop/cop/style/bisected_attr_accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class BisectedAttrAccessor < Cop
def on_class(class_node)
VISIBILITY_SCOPES.each do |visibility|
reader_names, writer_names = accessor_names(class_node, visibility)
next unless reader_names && writer_names

accessor_macroses(class_node, visibility).each do |macro|
check(macro, reader_names, writer_names)
Expand All @@ -48,17 +49,17 @@ def autocorrect(node)
private

def accessor_names(class_node, visibility)
reader_names = Set.new
writer_names = Set.new
reader_names = nil
writer_names = nil

accessor_macroses(class_node, visibility).each do |macro|
names = macro.arguments.map(&:source)

names.each do |name|
if attr_reader?(macro)
reader_names.add(name)
(reader_names ||= Set.new).add(name)
else
writer_names.add(name)
(writer_names ||= Set.new).add(name)
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions lib/rubocop/cop/variable_force/variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def referenced?
def reference!(node)
reference = Reference.new(node, @scope)
@references << reference
consumed_branches = Set.new
consumed_branches = nil

@assignments.reverse_each do |assignment|
next if consumed_branches.include?(assignment.branch)
next if consumed_branches&.include?(assignment.branch)

assignment.reference!(node) unless assignment.run_exclusively_with?(reference)

Expand All @@ -58,7 +58,9 @@ def reference!(node)

break if !assignment.branch || assignment.branch == reference.branch

consumed_branches << assignment.branch unless assignment.branch.may_run_incompletely?
unless assignment.branch.may_run_incompletely?
(consumed_branches ||= Set.new) << assignment.branch
end
end
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
Expand Down

0 comments on commit 94430a8

Please sign in to comment.