Skip to content

Commit ff1a971

Browse files
committed
Avoid deepcopy of immutables to improve performance by 2-6x
1 parent 2c94569 commit ff1a971

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
@@ -159,6 +159,8 @@ class EvictionPolicy(Enum):
159159
_CTIME = "ctime"
160160
_ACCESS_COUNT = "access_count"
161161

162+
IMMUTABLES = (bytes, str, int, float, bool, type(None), tuple)
163+
162164

163165
class AbstractCache(ABC):
164166
"""
@@ -267,7 +269,12 @@ def get(self, command: Union[str, Sequence[str]]) -> ResponseT:
267269
self.delete_command(command)
268270
return
269271
self._update_access(command)
270-
return copy.deepcopy(self.cache[command]["response"])
272+
response = self.cache[command]["response"]
273+
return (
274+
response
275+
if isinstance(response, IMMUTABLES)
276+
else copy.deepcopy(response)
277+
)
271278

272279
def delete_command(self, command: Union[str, Sequence[str]]):
273280
"""

0 commit comments

Comments
 (0)