Skip to content

Commit 52f0357

Browse files
committed
Avoid deepcopy of immutables to improve performance by 2-6x
1 parent 4e38c99 commit 52f0357

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

redis/_cache.py

Lines changed: 8 additions & 1 deletion
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,12 @@ 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 (
266+
response
267+
if isinstance(response, IMMUTABLES)
268+
else copy.deepcopy(response)
269+
)
263270

264271
def delete_command(self, command: str):
265272
"""

0 commit comments

Comments
 (0)