Skip to content

Commit 590e855

Browse files
authored
Merge pull request #753 from splitrb/improve-redis-loading
Prefetch trial state in a single Redis roundtrip
2 parents 876a2ee + 7c632c1 commit 590e855

2 files changed

Lines changed: 36 additions & 14 deletions

File tree

lib/split/experiment.rb

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,29 @@ def start
176176
end
177177

178178
def start_time
179-
@start_time ||= Split.cache(:experiment_start_times, @name) do
180-
t = redis.hget(:experiment_start_times, @name)
181-
if t
182-
# Check if stored time is an integer
183-
if t =~ /^[-+]?[0-9]+$/
184-
Time.at(t.to_i)
185-
else
186-
Time.parse(t)
187-
end
188-
end
179+
return @start_time if defined?(@start_time)
180+
181+
@start_time = Split.cache(:experiment_start_times, @name) do
182+
parse_start_time(redis.hget(:experiment_start_times, @name))
189183
end
190184
end
191185

186+
def prefetch_trial_state!
187+
winner_name, raw_start_time, raw_version, raw_cohorting =
188+
redis.pipelined do |pipe|
189+
pipe.hget(:experiment_winner, name)
190+
pipe.hget(:experiment_start_times, @name)
191+
pipe.get("#{name}:version")
192+
pipe.hget(experiment_config_key, :cohorting)
193+
end
194+
195+
@has_winner = !winner_name.nil?
196+
@start_time = parse_start_time(raw_start_time)
197+
@version = raw_version.to_i
198+
@cohorting_disabled = raw_cohorting.nil? ? false : raw_cohorting.downcase == "true"
199+
self
200+
end
201+
192202
def next_alternative
193203
winner || random_alternative
194204
end
@@ -402,10 +412,9 @@ def jstring(goal = nil)
402412
end
403413

404414
def cohorting_disabled?
405-
@cohorting_disabled ||= begin
406-
value = redis.hget(experiment_config_key, :cohorting)
407-
value.nil? ? false : value.downcase == "true"
408-
end
415+
return @cohorting_disabled if defined?(@cohorting_disabled)
416+
value = redis.hget(experiment_config_key, :cohorting)
417+
@cohorting_disabled = value.nil? ? false : value.downcase == "true"
409418
end
410419

411420
def disable_cohorting
@@ -424,6 +433,17 @@ def experiment_config_key
424433
end
425434

426435
private
436+
def parse_start_time(t)
437+
return if t.nil?
438+
439+
# Check if stored time is an integer
440+
if t =~ /^[-+]?[0-9]+$/
441+
Time.at(t.to_i)
442+
else
443+
Time.parse(t)
444+
end
445+
end
446+
427447
def redis
428448
Split.redis
429449
end

lib/split/trial.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def choose!(context = nil)
5656
# Only run the process once
5757
return alternative if @alternative_chosen
5858

59+
@experiment.prefetch_trial_state!
60+
5961
new_participant = @user[@experiment.key].nil?
6062
if override_is_alternative?
6163
self.alternative = @options[:override]

0 commit comments

Comments
 (0)