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

Improve performance of set_expiration #154

Merged
merged 1 commit into from
Dec 31, 2018

Conversation

saverio-kantox
Copy link
Contributor

@saverio-kantox saverio-kantox commented Dec 31, 2018

Fixes #118 and #121

Array.bsearch is in ruby 2.0.

The difference with #121 is that here we manually build the index search using plain bsearch item on the array of (item, index) pairs, and then extract the index from it.

Before:

> bundle exec ruby performance.rb
setex
21.502102999947965
set
0.09983500000089407

After:

> bundle exec ruby performance.rb
setex
7.995712999952957
set
0.08754800003953278

Test file:

require 'mock_redis'
require 'benchmark'

VALS = Array.new(10_000) do |i|
  ["some_key:#{i}", 3000 + rand(10)]
end

mr = MockRedis.new

puts 'setex', (
  Benchmark.realtime do
    VALS.each do |key, exp|
      mr.setex(key, exp, 'some value')
    end
  end
)

puts 'set', (
  Benchmark.realtime do
    VALS.each do |key, exp|
      mr.set(key, 'some value')
    end
  end
)

@sds sds merged commit cd37333 into sds:master Dec 31, 2018
@sds
Copy link
Owner

sds commented Dec 31, 2018

Thanks for the fix, @saverio-kantox.

Now that Ruby 2.2 is EOL, we'd be open to accepting a fix that uses bsearch_index if it faster. However, if the difference is negligible this will totally suffice!

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.

2 participants