Skip to content

Commit

Permalink
Avoid deepcopy of immutables to improve performance by 2-6x
Browse files Browse the repository at this point in the history
  • Loading branch information
frost-nzcr4 committed Apr 17, 2024
1 parent 7f1fba2 commit 17faee0
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion redis/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@
_CTIME = "ctime"
_ACCESS_COUNT = "access_count"

IMMUTABLES = (bytes, str, int, float, bool, type(None), tuple)


class EvictionPolicy(Enum):
LRU = "lru"
Expand Down Expand Up @@ -259,7 +261,8 @@ def get(self, command: str) -> ResponseT:
self.delete_command(command)
return
self._update_access(command)
return copy.deepcopy(self.cache[command]["response"])
response = self.cache[command]["response"]
return response if isinstance(response, IMMUTABLES) else copy.deepcopy(response)

def delete_command(self, command: str):
"""
Expand Down

0 comments on commit 17faee0

Please sign in to comment.