Skip to content

Commit d8b00b8

Browse files
mlovicandrew
authored andcommitted
Improve performance of RedisInterface#make_list_length (#509)
This implementation will only execute 1 to Redis command, instead of 2n (n being the total number of elements to trim from the list). The operation will still take O(n) time, but the constants will be much better, since only 1 network round trip is required.
1 parent 77f87b1 commit d8b00b8

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

lib/split/redis_interface.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ def remove_last_item_from_list(list_name)
3535
end
3636

3737
def make_list_length(list_name, new_length)
38-
while list_length(list_name) > new_length
39-
remove_last_item_from_list(list_name)
40-
end
38+
redis.ltrim(list_name, 0, new_length - 1)
4139
end
4240

4341
def add_to_set(set_name, value)

0 commit comments

Comments
 (0)