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 __slots__ on NodeMixin to reduce memory usage #196

Closed
wants to merge 2 commits into from

Conversation

lluissalord
Copy link
Contributor

The usage of __slots__ is highly recommended in order to reduce memory usage on classes, even more for the ones which are used a lot of time as it could be NodeMixin. The benefit here is that __dict__ is not generated then and a lot of memory is saved with this. Besides, with this change any class which inherit NodeMixin can now benefit of not creating __dict__ and reduce the memory usage, otherwise child classes cannot reduce memory with this trick if the parent class does not implement it too.

However, the rest of the classes of this package which inherit from NodeMixin should not use __slots__ because they benefit from the flexibility of __dict__. Hence, this benefit is only for implementations which use directly NodeMixin, as mine.

In order to set some statistics, in my implementation, the use of this change help us reduce the memory usage of the custom node classes by 49,91% and by 71,85%. Hence, as these classes are highly used on my implementation this can represent a lot of MBs reduction on memory usage.

The usage of `__slots__` is highly recommended in order to reduce memory usage on classes, even more for the ones which are used a lot of time as it could be `NodeMixin`. The benefit here is that `__dict__` is not generated then and a lot of memory is saved with this. Besides, with this change any class which inherit `NodeMixin` can now benefit of not creating `__dict__` and reduce the memory usage, otherwise child classes cannot reduce memory with this trick if the parent class does not implement it too.

However, the rest of the classes of this package which inherit from `NodeMixin` should not use `__slots__` because they benefit from the flexibility of `__dict__`. Hence, this benefit is only for implementations which use directly `NodeMixin`, as mine.

In order to set some statistics, in my implementation, the use of this change help us reduce the memory usage of the custom node classes by 49,91% and by 71,85%. Hence, as these classes are highly used on my implementation this can represent a lot of MBs reduction on memory usage.
@c0fec0de
Copy link
Owner

The slots implementation did cause a lot of trouble. See #77. Therefore i will not implement __slots__. Will think about a duplicate of NodeMixin with __slots__ named NodeMixinSlotted

@c0fec0de c0fec0de self-assigned this Oct 10, 2023
@c0fec0de c0fec0de modified the milestone: 2.10.0 Oct 10, 2023
@c0fec0de
Copy link
Owner

It is more complicated.

@coveralls
Copy link

Coverage Status

coverage: 99.799% (-0.1%) from 99.899% when pulling 8ce0836 on lluissalord:patch-1 into 2960936 on c0fec0de:master.

@c0fec0de c0fec0de closed this in 4015a45 Oct 26, 2023
@c0fec0de
Copy link
Owner

Thanks for your contribution and sorry for the delay.

@lluissalord
Copy link
Contributor Author

Thanks for your contribution and sorry for the delay.

Thanks to you for implementing it 😄

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.

3 participants