Skip to content

Commit 98d36b4

Browse files
authored
Fix ordered set prepend bug (#115)
The scoring for prepended elements was not being calculated correctly. It needs to decrement the score by 0.000001 for each element. Previously it was increasing the score, but because the base score was decreasing the bug was not exposed. Using Ruby 3.2.2, performance seems to be much improved, exposing this bug.
1 parent be13d7a commit 98d36b4

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/kredis/types/ordered_set.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ def insert(elements, prepending: false)
3434
return if elements.empty?
3535

3636
elements_with_scores = types_to_strings(elements, typed).map.with_index do |element, index|
37-
score = generate_base_score(negative: prepending) + (index / 100000)
37+
incremental_score = index * 0.000001
38+
39+
score = if prepending
40+
-base_score - incremental_score
41+
else
42+
base_score + incremental_score
43+
end
3844

3945
[ score , element ]
4046
end
@@ -45,10 +51,8 @@ def insert(elements, prepending: false)
4551
end
4652
end
4753

48-
def generate_base_score(negative:)
49-
current_time = process_start_time + process_uptime
50-
51-
negative ? -current_time : current_time
54+
def base_score
55+
process_start_time + process_uptime
5256
end
5357

5458
def process_start_time

0 commit comments

Comments
 (0)