From 291aec7f04775dbe8aaff0c6a2d7328aaca0427b Mon Sep 17 00:00:00 2001 From: fatkodima Date: Mon, 26 Oct 2020 19:37:03 +0200 Subject: [PATCH] Optimize memory usage --- lib/rubocop/cop/corrector.rb | 4 +++- lib/rubocop/cop/mixin/line_length_help.rb | 2 +- lib/rubocop/cop/naming/predicate_name.rb | 3 ++- lib/rubocop/cop/style/semicolon.rb | 3 +++ lib/rubocop/cop/team.rb | 7 ++++++- lib/rubocop/cop/util.rb | 2 +- lib/rubocop/magic_comment.rb | 4 ++-- 7 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/rubocop/cop/corrector.rb b/lib/rubocop/cop/corrector.rb index b2d1c4c0f3c7..848dd61d5f52 100644 --- a/lib/rubocop/cop/corrector.rb +++ b/lib/rubocop/cop/corrector.rb @@ -9,6 +9,8 @@ module Cop # The nodes modified by the corrections should be part of the # AST of the source_buffer. class Corrector < ::Parser::Source::TreeRewriter + NOOP_CONSUMER = ->(diagnostic) {} # noop + # @param source [Parser::Source::Buffer, or anything # leading to one via `(processed_source.)buffer`] # @@ -23,7 +25,7 @@ def initialize(source) ) # Don't print warnings to stderr if corrections conflict with each other - diagnostics.consumer = ->(diagnostic) {} # noop + diagnostics.consumer = NOOP_CONSUMER end alias rewrite process # Legacy diff --git a/lib/rubocop/cop/mixin/line_length_help.rb b/lib/rubocop/cop/mixin/line_length_help.rb index 30ad7890d947..db25d317da1f 100644 --- a/lib/rubocop/cop/mixin/line_length_help.rb +++ b/lib/rubocop/cop/mixin/line_length_help.rb @@ -57,7 +57,7 @@ def match_uris(string) def indentation_difference(line) return 0 unless tab_indentation_width - line.match(/^\t*/)[0].size * (tab_indentation_width - 1) + (line.index(/[^\t]/) || 0) * (tab_indentation_width - 1) end def tab_indentation_width diff --git a/lib/rubocop/cop/naming/predicate_name.rb b/lib/rubocop/cop/naming/predicate_name.rb index 52c665ee3f54..086f18d97d34 100644 --- a/lib/rubocop/cop/naming/predicate_name.rb +++ b/lib/rubocop/cop/naming/predicate_name.rb @@ -67,7 +67,8 @@ def on_def(node) private def allowed_method_name?(method_name, prefix) - !method_name.match?(/^#{prefix}[^0-9]/) || + !(method_name.start_with?(prefix) && # cheap check to avoid allocating Regexp + method_name.match?(/^#{prefix}[^0-9]/)) || method_name == expected_name(method_name, prefix) || method_name.end_with?('=') || allowed_method?(method_name) diff --git a/lib/rubocop/cop/style/semicolon.rb b/lib/rubocop/cop/style/semicolon.rb index e09fde2e6572..99a4f8820f3e 100644 --- a/lib/rubocop/cop/style/semicolon.rb +++ b/lib/rubocop/cop/style/semicolon.rb @@ -70,6 +70,9 @@ def on_begin(node) # rubocop:todo Metrics/CyclomaticComplexity private def check_for_line_terminator_or_opener + # Make the obvious check first + return unless @processed_source.raw_source.include?(';') + each_semicolon { |line, column| convention_on(line, column, true) } end diff --git a/lib/rubocop/cop/team.rb b/lib/rubocop/cop/team.rb index 3439d5cdd3b2..3bbf29a36bbc 100644 --- a/lib/rubocop/cop/team.rb +++ b/lib/rubocop/cop/team.rb @@ -99,7 +99,12 @@ def forces def self.forces_for(cops) needed = Hash.new { |h, k| h[k] = [] } cops.each do |cop| - Array(cop.class.joining_forces).each { |force| needed[force] << cop } + forces = cop.class.joining_forces + if forces.is_a?(Array) + forces.each { |force| needed[force] << cop } + elsif forces + needed[forces] << cop + end end needed.map do |force_class, joining_cops| diff --git a/lib/rubocop/cop/util.rb b/lib/rubocop/cop/util.rb index 9cf0fb0b6e91..3d8f1c239122 100644 --- a/lib/rubocop/cop/util.rb +++ b/lib/rubocop/cop/util.rb @@ -62,7 +62,7 @@ def on_node(syms, sexp, excludes = [], &block) end def begins_its_line?(range) - (range.source_line =~ /\S/) == range.column + range.source_line.index(/\S/) == range.column end # Returns, for example, a bare `if` node if the given node is an `if` diff --git a/lib/rubocop/magic_comment.rb b/lib/rubocop/magic_comment.rb index 0b2373844122..f84db8e049a4 100644 --- a/lib/rubocop/magic_comment.rb +++ b/lib/rubocop/magic_comment.rb @@ -194,7 +194,7 @@ def frozen_string_literal; end class SimpleComment < MagicComment # Match `encoding` or `coding` def encoding - extract(/\A\s*\#.*\b(?:en)?coding: (#{TOKEN})/i) + extract(/\A\s*\#.*\b(?:en)?coding: (#{TOKEN})/io) end private @@ -207,7 +207,7 @@ def encoding # Case-insensitive and dashes/underscores are acceptable. # @see https://git.io/vM7Mg def extract_frozen_string_literal - extract(/\A\s*#\s*frozen[_-]string[_-]literal:\s*(#{TOKEN})\s*\z/i) + extract(/\A\s*#\s*frozen[_-]string[_-]literal:\s*(#{TOKEN})\s*\z/io) end end end