-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Performance/ConcurrentMonotonicTime
cop
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.
- Loading branch information
Showing
7 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changelog/new_add_new_performance_concurrent_monotonic_time_cop.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#267](https://github.com/rubocop/rubocop-performance/pull/267): Add new `Performance/ConcurrentMonotonicTime` cop. ([@koic][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# This cop identifies places where `Concurrent.monotonic_time` | ||
# can be replaced by `Process.clock_gettime(Process::CLOCK_MONOTONIC)`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# Concurrent.monotonic_time | ||
# | ||
# # good | ||
# Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
# | ||
class ConcurrentMonotonicTime < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `%<prefer>s` instead of `%<current>s`.' | ||
RESTRICT_ON_SEND = %i[monotonic_time].freeze | ||
|
||
def_node_matcher :concurrent_monotonic_time?, <<~PATTERN | ||
(send | ||
(const {nil? cbase} :Concurrent) :monotonic_time ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless concurrent_monotonic_time?(node) | ||
|
||
optional_unit_parameter = ", #{node.first_argument.source}" if node.first_argument | ||
prefer = "Process.clock_gettime(Process::CLOCK_MONOTONIC#{optional_unit_parameter})" | ||
message = format(MSG, prefer: prefer, current: node.source) | ||
|
||
add_offense(node, message: message) do |corrector| | ||
corrector.replace(node, prefer) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
spec/rubocop/cop/performance/concurrent_monotonic_time_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::ConcurrentMonotonicTime, :config do | ||
it 'registers an offense when using `Concurrent.monotonic_time`' do | ||
expect_offense(<<~RUBY) | ||
Concurrent.monotonic_time | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Process.clock_gettime(Process::CLOCK_MONOTONIC)` instead of `Concurrent.monotonic_time`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `::Concurrent.monotonic_time`' do | ||
expect_offense(<<~RUBY) | ||
::Concurrent.monotonic_time | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Process.clock_gettime(Process::CLOCK_MONOTONIC)` instead of `::Concurrent.monotonic_time`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `Concurrent.monotonic_time` with an optional unit parameter' do | ||
expect_offense(<<~RUBY) | ||
Concurrent.monotonic_time(:float_second) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second)` instead of `Concurrent.monotonic_time(:float_second)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `Process.clock_gettime(Process::CLOCK_MONOTONIC)`' do | ||
expect_no_offenses(<<~RUBY) | ||
Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
RUBY | ||
end | ||
end |