Skip to content

Commit

Permalink
Optimize _scan().
Browse files Browse the repository at this point in the history
In case that a regular expression is specified, it is not needed to copy all the keys if some of them do not match the regular expression.
If a regular expression is not provided, there is no need to iterate over the slice. Instead, we simply copy the entire slice.
This is useful in cases where we're dealing with a large number of keys.
  • Loading branch information
thedrow authored Aug 27, 2018
1 parent 8098106 commit 72ff3c2
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import types
import re
import functools
from itertools import count
from itertools import count, islice

import redis
from redis.exceptions import ResponseError
Expand Down Expand Up @@ -2046,14 +2046,15 @@ def _scan(self, keys, cursor, match, count):
data = sorted(keys)
result_cursor = cursor + count
result_data = []
# subset =

if match is not None:
regex = _compile_pattern(match)
for val in islice(data, cursor, result_cursor):
if regex.match(to_bytes(val)):
result_data.append(val)
else:
regex = None
for val in data[cursor:result_cursor]:
if not regex or regex.match(to_bytes(val)):
result_data.append(val)
result_data = data[cursor:result_cursor]

if result_cursor >= len(data):
result_cursor = 0
return result_cursor, result_data
Expand Down

0 comments on commit 72ff3c2

Please sign in to comment.