Browse files

for redis lookups, first query the first bit, if not set then missing,

otherwise pipeline remaining bits (optimize for negative case)
  • Loading branch information...
1 parent 2a608f4 commit 721a12256688b6381040004e7e6f9ddaa3c64f55 @igrigorik committed Mar 31, 2011
Showing with 41 additions and 2 deletions.
  1. +34 −0 benchmark/redis-bm.rb
  2. +6 −1 lib/bloomfilter/redis.rb
  3. +1 −1 lib/bloomfilter/version.rb
View
34 benchmark/redis-bm.rb
@@ -0,0 +1,34 @@
+$:<< 'lib'
+
+require 'benchmark'
+require 'bloomfilter-rb'
+
+n = 10000
+
+Benchmark.bm do |x|
+ r = BloomFilter::Redis.new
+
+ x.report("insert") do
+ n.times do
+ r.insert("a")
+ end
+ end
+
+ x.report("lookup present") do
+ n.times do
+ r.include?("a")
+ end
+ end
+
+ x.report("lookup missing") do
+ n.times do
+ r.include?("b")
+ end
+ end
+
+end
+
+# user system total real
+# insert 1.000000 0.380000 1.380000 ( 1.942181)
+# lookup present 1.030000 0.470000 1.500000 ( 2.577577)
+# lookup missing 0.370000 0.160000 0.530000 ( 1.060429)
View
7 lib/bloomfilter/redis.rb
@@ -26,8 +26,13 @@ def insert(key, ttl=nil)
def include?(*keys)
keys.each do |key|
+ indexes = []
+ indexes_for(key) { |idx| indexes << idx }
+
+ return false if @db.getbit(@opts[:namespace], indexes.shift) == 0
+
result = @db.pipelined do
- indexes_for(key) do |idx|
+ indexes.each do |idx|
@db.getbit(@opts[:namespace], idx)
end
end
View
2 lib/bloomfilter/version.rb
@@ -1,3 +1,3 @@
module BloomFilter
- VERSION = "2.1.0"
+ VERSION = "2.1.1"
end

0 comments on commit 721a122

Please sign in to comment.