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/ConcurrentMonotonicTime cop #267

Merged

Conversation

koic
Copy link
Member

@koic koic commented Oct 30, 2021

Follow up to rails/rails#43502.

This PR adds new Performance/ConcurrentMonotonicTime cop.

This cop identifies places where Concurrent.monotonic_time can be replaced by Process.clock_gettime(Process::CLOCK_MONOTONIC).

# bad
Concurrent.monotonic_time

# good
Process.clock_gettime(Process::CLOCK_MONOTONIC)

Below is a benchmark.

% cat monotonic_time.rb
#!/usr/local/bin/ruby

p `ruby -v`

require 'concurrent'
require 'benchmark/ips'

Benchmark.ips do |x|
  x.report('Concurrent.monotonic_time') { Concurrent.monotonic_time }
  x.report('Process.clock_gettime')     { Process.clock_gettime(Process::CLOCK_MONOTONIC) }

  x.compare!
end
% ruby monotonic_time.rb
"ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-darwin19]\n"
Warming up --------------------------------------
Concurrent.monotonic_time
                       607.064k i/100ms
Process.clock_gettime
                       730.862k i/100ms
Calculating -------------------------------------
Concurrent.monotonic_time
                          5.695M (± 4.4%) i/s -     28.532M in
                          5.019798s
Process.clock_gettime
                          7.398M (± 2.5%) i/s -     37.274M in
                          5.041776s

Comparison:
Process.clock_gettime:  7397700.9 i/s
Concurrent.monotonic_time:  5695385.0 i/s - 1.30x  (± 0.00) slower

And this cop is compatible with ruby-concurrency/concurrent-ruby#915.


Before submitting the PR make sure the following are checked:

  • 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.
  • 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.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

Follow up to rails/rails#43502.

This PR adds new `Performance/ConcurrentMonotonicTime` cop.

This cop identifies places where `Concurrent.monotonic_time`
can be replaced by `Process.clock_gettime(Process::CLOCK_MONOTONIC)`.

```ruby
# bad
Concurrent.monotonic_time

# good
Process.clock_gettime(Process::CLOCK_MONOTONIC)
```

Below is a benchmark.

```ruby
% cat monotonic_time.rb
#!/usr/local/bin/ruby

p `ruby -v`

require 'concurrent'
require 'benchmark/ips'

Benchmark.ips do |x|
  x.report('Concurrent.monotonic_time') { Concurrent.monotonic_time }
  x.report('Process.clock_gettime')     {
  Process.clock_gettime(Process::CLOCK_MONOTONIC) }

  x.compare!
end
```

```console
% ruby monotonic_time.rb
"ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-darwin19]\n"
Warming up --------------------------------------
Concurrent.monotonic_time
                       607.064k i/100ms
Process.clock_gettime
                       730.862k i/100ms
Calculating -------------------------------------
Concurrent.monotonic_time
                          5.695M (± 4.4%) i/s -     28.532M in
                          5.019798s
Process.clock_gettime
                          7.398M (± 2.5%) i/s -     37.274M in
                          5.041776s

Comparison:
Process.clock_gettime:  7397700.9 i/s
Concurrent.monotonic_time:  5695385.0 i/s - 1.30x  (± 0.00) slower
```

And this cop is compatible with ruby-concurrency/concurrent-ruby#915.
@koic koic force-pushed the add_new_performance_concurrent_monotonic_time_cop branch from b0cae18 to 69d541a Compare October 30, 2021 22:42
@koic koic merged commit a3f489b into rubocop:master Oct 30, 2021
@koic koic deleted the add_new_performance_concurrent_monotonic_time_cop branch October 30, 2021 23:16
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