Skip to content

Commit 17faee0

Browse files
committed
Avoid deepcopy of immutables to improve performance by 2-6x
1 parent 7f1fba2 commit 17faee0

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

redis/_cache.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154
_CTIME = "ctime"
155155
_ACCESS_COUNT = "access_count"
156156

157+
IMMUTABLES = (bytes, str, int, float, bool, type(None), tuple)
158+
157159

158160
class EvictionPolicy(Enum):
159161
LRU = "lru"
@@ -259,7 +261,8 @@ def get(self, command: str) -> ResponseT:
259261
self.delete_command(command)
260262
return
261263
self._update_access(command)
262-
return copy.deepcopy(self.cache[command]["response"])
264+
response = self.cache[command]["response"]
265+
return response if isinstance(response, IMMUTABLES) else copy.deepcopy(response)
263266

264267
def delete_command(self, command: str):
265268
"""

0 commit comments

Comments
 (0)