Skip to content

Commit

Permalink
Improve performance with RESTRICT_ON_SEND
Browse files Browse the repository at this point in the history
https://github.com/rubocop/rubocop/blob/master/docs/modules/ROOT/pages/
development.adoc#implementation mentions:

> The `on_send` callback is the most used and can be optimized
> by restricting the acceptable method names with a constant
> `RESTRICT_ON_SEND`.
  • Loading branch information
bquorning committed May 18, 2022
1 parent e8f99fe commit 1a29b97
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 35 deletions.
15 changes: 2 additions & 13 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
# frozen_string_literal: true

appraise 'rubocop-0.53' do
gem 'rubocop', '~> 0.53.0'
end

appraise 'rubocop-0.81' do
gem 'rubocop', '~> 0.81.0'
end

if Gem::Requirement.new('>= 2.4.0')
.satisfied_by?(Gem::Version.new(RUBY_VERSION))
appraise 'rubocop-0.86' do
gem 'rubocop', '~> 0.86.0'
end
appraise 'rubocop-0.90' do
gem 'rubocop', '~> 0.90.0'
end

if Gem::Requirement.new('>= 2.5.0')
Expand Down
7 changes: 0 additions & 7 deletions gemfiles/rubocop_0.81.gemfile

This file was deleted.

7 changes: 0 additions & 7 deletions gemfiles/rubocop_0.86.gemfile

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

source "https://rubygems.org"

gem "rubocop", "~> 0.53.0"
gem "rubocop", "~> 0.90.0"

gemspec path: "../"
6 changes: 6 additions & 0 deletions lib/rubocop/cop/thread_safety/class_and_module_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ module ThreadSafety
# end
class ClassAndModuleAttributes < Cop
MSG = 'Avoid mutating class and module attributes.'
RESTRICT_ON_SEND = %i[
mattr_writer mattr_accessor cattr_writer cattr_accessor
class_attribute
attr attr_accessor attr_writer
attr_internal attr_internal_accessor attr_internal_writer
].freeze

def_node_matcher :mattr?, <<~MATCHER
(send nil?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ module ThreadSafety
# end
class InstanceVariableInClassMethod < Cop
MSG = 'Avoid instance variables in class methods.'
RESTRICT_ON_SEND = %i[
instance_variable_set
instance_variable_get
].freeze

def_node_matcher :instance_variable_set_call?, <<~MATCHER
(send nil? :instance_variable_set (...) (...))
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/thread_safety/new_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module ThreadSafety
# Thread.new { do_work }
class NewThread < Cop
MSG = 'Avoid starting new threads.'
RESTRICT_ON_SEND = %i[new].freeze

def_node_matcher :new_thread?, <<~MATCHER
(send (const {nil? cbase} :Thread) :new)
Expand Down
4 changes: 2 additions & 2 deletions rubocop-thread_safety.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.required_ruby_version = '>= 2.3.0'
spec.required_ruby_version = '>= 2.4.0'

spec.add_runtime_dependency 'rubocop', '>= 0.53.0'
spec.add_runtime_dependency 'rubocop', '>= 0.90.0'

spec.add_development_dependency 'appraisal'
spec.add_development_dependency 'bundler', '>= 1.10', '< 3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
:config do
subject(:cop) { described_class.new(config) }
let(:msg) { 'Freeze mutable objects assigned to class instance variables.' }
if Gem::Requirement.new('< 0.69')
.satisfied_by?(Gem::Version.new(RuboCop::Version::STRING))
let(:ruby_version) { 2.3 }
end

let(:prefix) { nil }
let(:suffix) { nil }
let(:indent) { '' }
Expand Down

0 comments on commit 1a29b97

Please sign in to comment.