-
-
Notifications
You must be signed in to change notification settings - Fork 930
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
Improve performance of State #2463
base: master
Are you sure you want to change the base?
Conversation
@Kludex I tested a few approaches to improve the performance of accessing State properties.
|
Context: I am just a random person - not a maintainer. I personally think the SimpleNamespace would work best but it can be simplified like that: class NameSpaceState2(SimpleNamespace):
"""
An object that can be used to store arbitrary state.
Used for `request.state` and `app.state`.
"""
def __init__(self, state: dict[str, typing.Any] | None = None):
if state is not None:
super().__init__(**state)
@property
def _state(self):
return self.__dict__ There is no need to keep 2 separate dicts. It has better performance characteristic for setting attrs and does not bug out when accessing _state directly. I confirm that this class is generally 10x faster compared to existing solution. |
Summary
#2389
I modified setattr to preserve the behavior of the existing State. Although assignment performance has slowed down compared to before, the rest of the performance significantly improves.
Checklist