-
Notifications
You must be signed in to change notification settings - Fork 77
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
Use custom descriptor instead of properties for Struct attributes #165
Conversation
# type: (typing.Any, typing.Any) -> typing.Any | ||
if instance is None: | ||
return self | ||
value = getattr(instance, "_{}_value".format(self.name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it's worth precalculating _{}_value
and storing it in an attribute? This would increase memory use but speed things up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I wasn't sure about this. I decided to start with more memory saving option, we can always go back to pre-computing if performance will ever be an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I don't think we need the original name, so I will just replace it with the formatted one.
class Attribute(object): | ||
__slots__ = ("name", "default", "nullable", "user_defined") | ||
|
||
def __init__(self, name, nullable=False, user_defined=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Brainstorming: What about a separate descriptor class for nullable attributes? This would make the __get__
and __set__
methods slightly faster.
Another idea would be to provide the validator as an attribute of Attribute
, instead of putting it in the class dictionary. This way we could benefit from __slots__
, and we wouldn't need to use getattr
to access it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about a separate descriptor class for nullable attributes? This would make the
__get__
and__set__
methods slightly faster.
I was also thinking about this, but it probably not worth it. Also again, we can always add it later if we will see any performance issues.
Another idea would be to provide the validator as an attribute of
Attribute
This is a good idea, it will probably not save huge memory but looks cleaner anyway.
I pushed some changes but this PR isn't updated, it looks like GitHub is broken again... |
It looks like closing and re-opening helped. |
A custom descriptor (essentially de-inlining the current logic) allows to save around 30% of memory at a cost of minor performance penalty.