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

Use custom descriptor instead of properties for Struct attributes #165

Merged
merged 11 commits into from
Jul 16, 2020

Conversation

ilevkivskyi
Copy link
Contributor

A custom descriptor (essentially de-inlining the current logic) allows to save around 30% of memory at a cost of minor performance penalty.

# type: (typing.Any, typing.Any) -> typing.Any
if instance is None:
return self
value = getattr(instance, "_{}_value".format(self.name))
Copy link

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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):
Copy link

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.

Copy link
Contributor Author

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.

@ilevkivskyi
Copy link
Contributor Author

I pushed some changes but this PR isn't updated, it looks like GitHub is broken again...

@ilevkivskyi ilevkivskyi reopened this Jul 15, 2020
@ilevkivskyi
Copy link
Contributor Author

It looks like closing and re-opening helped.

@ilevkivskyi ilevkivskyi reopened this Jul 15, 2020
@ilevkivskyi ilevkivskyi merged commit 1a08624 into dropbox:master Jul 16, 2020
@ilevkivskyi ilevkivskyi deleted the use-custom-descr branch July 16, 2020 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants