Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize cython cached_property implementation #1122

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Changes from 1 commit
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
Next Next commit
Optimize cython cached_property implementation
bdraco committed Sep 7, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 0b9eb7fdf100d62640dd2308eb904a2b2dbea9df
21 changes: 10 additions & 11 deletions yarl/_helpers_c.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# cython: language_level=3

cdef _sentinel = object()

cdef class cached_property:
"""Use as a class method decorator. It operates almost exactly like
the Python `@property` decorator, but it puts the result of the
@@ -21,17 +23,14 @@ cdef class cached_property:
return self.wrapped.__doc__

def __get__(self, inst, owner):
try:
try:
return inst._cache[self.name]
except KeyError:
val = self.wrapped(inst)
inst._cache[self.name] = val
return val
except AttributeError:
if inst is None:
return self
raise
if inst is None:
return self
cdef dict cache = inst._cache
val = cache.get(self.name, _sentinel)
if val is _sentinel:
val = self.wrapped(inst)
cache[self.name] = val
return val

def __set__(self, inst, value):
raise AttributeError("cached property is read-only")