Skip to content

Commit

Permalink
Use Set for users and groups, for better lookup performance
Browse files Browse the repository at this point in the history
* Set provides constant over Array's linear lookup time
  • Loading branch information
Dapeng Li authored and reneklacan committed Feb 13, 2016
1 parent c0e6fe4 commit 5b2615e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
11 changes: 6 additions & 5 deletions lib/rollout.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "rollout/version"
require "rollout/legacy"
require "zlib"
require 'set'

class Rollout
class Feature
Expand All @@ -14,15 +15,15 @@ def initialize(name, string = nil, opts = {})
if string
raw_percentage,raw_users,raw_groups = string.split("|")
@percentage = raw_percentage.to_i
@users = (raw_users || "").split(",").map(&:to_s)
@groups = (raw_groups || "").split(",").map(&:to_sym)
@users = (raw_users || "").split(",").map(&:to_s).to_set
@groups = (raw_groups || "").split(",").map(&:to_sym).to_set
else
clear
end
end

def serialize
"#{@percentage}|#{@users.join(",")}|#{@groups.join(",")}"
"#{@percentage}|#{@users.to_a.join(",")}|#{@groups.to_a.join(",")}"
end

def add_user(user)
Expand All @@ -43,8 +44,8 @@ def remove_group(group)
end

def clear
@groups = []
@users = []
@groups = Set.new
@users = Set.new
@percentage = 0
end

Expand Down
30 changes: 15 additions & 15 deletions spec/rollout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
end

it "leaves the other groups active" do
expect(@rollout.get(:chat).groups).to eq([:fivesonly])
expect(@rollout.get(:chat).groups).to eq [:fivesonly].to_set
end
end

Expand Down Expand Up @@ -169,7 +169,7 @@
end

it "remains active for other active users" do
expect(@rollout.get(:chat).users).to eq(%w(24))
expect(@rollout.get(:chat).users).to eq %w(24).to_set
end
end

Expand Down Expand Up @@ -377,14 +377,14 @@

it "returns the feature object" do
feature = @rollout.get(:chat)
expect(feature.groups).to eq([:caretakers, :greeters])
expect(feature.percentage).to eq(10)
expect(feature.users).to eq(%w(42))
expect(feature.to_hash).to eq({
:groups => [:caretakers, :greeters],
expect(feature.groups).to eq [:caretakers, :greeters].to_set
expect(feature.percentage).to eq 10
expect(feature.users).to eq %w(42).to_set
expect(feature.to_hash).to eq(
:groups => [:caretakers, :greeters].to_set,
:percentage => 10,
:users => %w(42)
})
:users => %w(42).to_set
)

feature = @rollout.get(:signup)
expect(feature.groups).to be_empty
Expand All @@ -404,11 +404,11 @@

it "each feature is cleared" do
features.each do |feature|
expect(@rollout.get(feature).to_hash).to eq({
expect(@rollout.get(feature).to_hash).to eq(
:percentage => 0,
:users => [],
:groups => []
})
:users => Set.new,
:groups => Set.new
)
end
end

Expand Down Expand Up @@ -436,8 +436,8 @@
@legacy.deactivate_all(:chat)
expect(@rollout.get(:chat).to_hash).to eq({
:percentage => 12,
:users => %w(24 42),
:groups => [:dope_people]
:users => %w(24 42).to_set,
:groups => [:dope_people].to_set
})
expect(@redis.get("feature:chat")).not_to be_nil
end
Expand Down

0 comments on commit 5b2615e

Please sign in to comment.