Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-938

Queue/dequeue is ~14% slower than it needs to be

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.0.5
    • Affects Version/s: None
    • Component/s: Connections
    • None
    • Fully Compatible

      Queue uses Array#delete_at(0) and Array#push to manage the connection pool. Array#pop and Array#unshift are substantially faster while maintaining FIFO semantics.

      require 'benchmark/ips'
      
      arr = (1..100).to_a
      Benchmark.ips do |x|
        x.report("Stack") { arr.push arr.pop }
        x.report("Tail -> Head Queue") { arr.unshift arr.pop }
        x.report("Head -> Tail Queue (shift)") { arr.push arr.shift }
        x.report("Head -> Tail Queue (delete_at)") { arr.push arr.delete_at(0) }
      end
      
                     Stack           4.994M (± 0.2%) i/s -     24.985M
        Tail -> Head Queue           5.092M (± 0.1%) i/s -     25.521M
      Head -> Tail Queue (shift)     4.898M (± 0.1%) i/s -     24.511M
      Head -> Tail Queue (delete_at) 4.453M (± 0.1%) i/s -     22.289M
      

      My fix is here:

      https://github.com/cheald/mongo-ruby-driver/commit/a8cafb7f5a30ec22334a23b166efba9f01618580

            Assignee:
            durran.jordan@mongodb.com Durran Jordan
            Reporter:
            cheald Chris Heald
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: