Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/1361.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Made `istr` slotted in the Python implementation to prevent excessive creations of instance dictionaries -- by :user:`jonathandung4`.
12 changes: 9 additions & 3 deletions multidict/_multidict_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@
class istr(str):
"""Case insensitive str."""

__slots__ = ("__istr_identity__",)

__is_istr__ = True
__istr_identity__: str | None = None
Comment thread
jonathandung marked this conversation as resolved.
__istr_identity__: str | None

def __new__(cls, s: Any, /):
self = super().__new__(cls, s)
self.__istr_identity__ = None
return self


_V = TypeVar("_V")
Expand Down Expand Up @@ -441,8 +448,7 @@ def _identity(self, key: str) -> str:
if isinstance(key, istr):
ret = key.__istr_identity__
if ret is None:
ret = key.lower()
key.__istr_identity__ = ret
key.__istr_identity__ = ret = key.lower()
return ret
if isinstance(key, str):
return key.lower()
Expand Down
Loading