anytree icon indicating copy to clipboard operation
anytree copied to clipboard

Use __slots__ on NodeMixin to reduce memory usage

Open lluissalord opened this issue 3 years ago • 1 comments

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.

lluissalord avatar May 10 '22 11:05 lluissalord

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 avatar Sep 20 '22 20:09 c0fec0de

It is more complicated.

c0fec0de avatar Oct 11 '23 19:10 c0fec0de

Coverage Status

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

coveralls avatar Oct 26 '23 19:10 coveralls

Thanks for your contribution and sorry for the delay.

c0fec0de avatar Oct 26 '23 20:10 c0fec0de

Thanks for your contribution and sorry for the delay.

Thanks to you for implementing it 😄

lluissalord avatar Oct 26 '23 21:10 lluissalord