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

Commits on Oct 30, 2021

  1. 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.
    koic committed Oct 30, 2021
    Configuration menu
    Copy the full SHA
    69d541a View commit details
    Browse the repository at this point in the history