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 May 9, 2024
1 parent 2c94569 commit ff1a971
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion redis/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ class EvictionPolicy(Enum):
_CTIME = "ctime"
_ACCESS_COUNT = "access_count"

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


class AbstractCache(ABC):
"""
Expand Down Expand Up @@ -267,7 +269,12 @@ def get(self, command: Union[str, Sequence[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: Union[str, Sequence[str]]):
"""
Expand Down

0 comments on commit ff1a971

Please sign in to comment.