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

Add new Performance/StringIdentifierArgument cop #276

Conversation

koic
Copy link
Member

@koic koic commented Dec 7, 2021

This PR adds new Performance/StringIdentifierArgument cop.

This cop identifies places where string identifier argument can be replaced by symbol identifier argument. It prevents the redundancy of the internal string-to-symbol conversion.

This cop targets methods that take identifier (e.g. method name) argument and the following examples are parts of it.

# bad
send('do_something')
attr_accessor 'do_something'
instance_variable_get('@ivar')

# good
send(:do_something)
attr_accessor :do_something
instance_variable_get(:@ivar)

The send method is a representative and other similar methods are similar benchmarks. The following is an example benchmark.

% cat symbol.rb
puts `ruby -v`

require 'benchmark/ips'

def foo
end

Benchmark.ips do |x|
  x.report('symbol arg') { send(:foo) }
  x.report('string arg') { send('foo') }

  x.compare!
end
% ruby symbol.rb
ruby 3.1.0dev (2021-12-05T10:23:42Z master 19f037e452) [x86_64-darwin19]
Warming up --------------------------------------
          symbol arg     1.025M i/100ms
          string arg   590.038k i/100ms
Calculating -------------------------------------
          symbol arg     10.665M (± 1.5%) i/s -     53.318M in 5.000551s

          string arg      5.895M (± 1.0%) i/s -     29.502M in 5.004999s

Comparison:
          symbol arg: 10665035.8 i/s
          string arg:  5895132.3 i/s - 1.81x  (± 0.00) slower

I learned about this performance difference from the book "Polished Ruby Programming".


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

This PR adds new `Performance/StringIdentifierArgument` cop.

This cop identifies places where string identifier argument can be replaced
by symbol identifier argument.
It prevents the redundancy of the internal string-to-symbol conversion.

This cop targets methods that take identifier (e.g. method name) argument
and the following examples are parts of it.

```ruby
# bad
send('do_something')
attr_accessor 'do_something'
instance_variable_get('@ivar')

# good
send(:do_something)
attr_accessor :do_something
instance_variable_get(:@ivar)
```

The `send` method is a representative and other similar methods are
similar benchmarks. The following is an example benchmark.

```ruby
% cat symbol.rb
puts `ruby -v`

require 'benchmark/ips'

def foo
end

Benchmark.ips do |x|
  x.report('symbol arg') { send(:foo) }
  x.report('string arg') { send('foo') }

  x.compare!
end
```

```console
% ruby symbol.rb
ruby 3.1.0dev (2021-12-05T10:23:42Z master 19f037e452) [x86_64-darwin19]
Warming up --------------------------------------
          symbol arg     1.025M i/100ms
          string arg   590.038k i/100ms
Calculating -------------------------------------
          symbol arg     10.665M (± 1.5%) i/s -     53.318M in 5.000551s

          string arg      5.895M (± 1.0%) i/s -     29.502M in 5.004999s

Comparison:
          symbol arg: 10665035.8 i/s
          string arg:  5895132.3 i/s - 1.81x  (± 0.00) slower
```

I learned about this performance difference from the book "Polished Ruby Programming".
@koic koic force-pushed the new_add_new_performance_string_identifier_argument_cop branch from c922d5d to 9a52662 Compare December 7, 2021 13:28
@koic koic merged commit f2dfcba into rubocop:master Dec 16, 2021
@koic koic deleted the new_add_new_performance_string_identifier_argument_cop branch December 16, 2021 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant