c0fec0de / anytree

Python tree data library
Apache License 2.0
947 stars 133 forks source link

Use __slots__ on NodeMixin to reduce memory usage #196

Closed lluissalord closed 11 months ago

lluissalord commented 2 years ago

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 commented 2 years ago

The slots implementation did cause a lot of trouble. See https://github.com/c0fec0de/anytree/issues/77. Therefore i will not implement __slots__. Will think about a duplicate of NodeMixin with __slots__ named NodeMixinSlotted

c0fec0de commented 11 months ago

It is more complicated.

coveralls commented 11 months ago

Coverage Status

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

c0fec0de commented 11 months ago

Thanks for your contribution and sorry for the delay.

lluissalord commented 11 months ago

Thanks for your contribution and sorry for the delay.

Thanks to you for implementing it 😄