ADR Suggestion Using __slots__ for performance improvement.
#165
Replies: 3 comments 1 reply
-
|
What's the point of adding it to the baseclass if we don't know which attributes to mention to have any optimization? On the other hand this is really cool and seems easy to implement for any class in which we will see an optimization benefit. |
Beta Was this translation helpful? Give feedback.
-
|
If we have many, many base objects (parameters, descriptors), and query them in quick succession (loop? list compr), using I think Additionally, they give you an "interface-like" behaviour where you see immediately what is being exposed by the class. I might be wrong, but linters and type checkers might use The BAD side is that the transient attributes like
|
Beta Was this translation helpful? Give feedback.
-
|
At our daily stand-up we decided that we would wait with implementing |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
General
The python
__slots__dunder method, when defined, creates "slots" for each attributes of the class defined in its__slots__method.This is an alternative to the normal, implicit
__dict__method which stores all instance attributes as a dictionary on the instance.Using
__slots__speeds up attribute access by app. 30% and reduces memory consumption of the class instance.Caveats
While a 30% speed up and a lower memory footprint sounds very good, it comes with certain caveats:
No dynamic attributes
If
__slots__is defined, but__dict__isn't, the class will not create the internal dictionary, thereby saving even more memory.This comes at the cost of not being able to dynamically add attributes to a class instance, if the attribute is not defined in
__slots__, it cannot be set. I.e. ALL attributes that can ever be set on the class instance, needs to be in__slots__.This downside can be circumvented by adding
__dict__to___slots__:Which then enables:
Which would otherwise crash. Adding
__dict__to___slots__will however mean losing out on some of the potential memory gain by using__slots__.No weak references
If
__slots__is defined, you cannot create a weak reference to instances of the class, UNLESS we add__weakref__to__slots__.Considering that we rely on weak references in our
global_objectto storeunique_namereferences, this is a must.No multiple class inheritances
If we use
__slots__, then we can't have multiple inheritances in our classes.The exception is if one of the classes don't have
__slots__defined, or it is defined to be empty, then it can be used for multiple inheritance.But since we're here considering adding
__slots__to our base class, this would mean we can't have multiple inheritances from any class inheriting from our base classes.More information can be found here:
https://stackoverflow.com/questions/472000/usage-of-slots
Current Implementation
The current new base classes does not have
__slots__defined and thus use__dict__by default.Proposed Implementation
Using
__slots__but also adding__weakref__and__dict__to it, to allow for dynamic attribute creation and weak references (weakrefs are non-negotiable with our currentunique_nameimplementation).Attributes which are properties of the classes should then be in
__slot__, for faster access times, whereas transient attributes (like__serialization_id) can be dynamically added to the class instance.Beta Was this translation helpful? Give feedback.
All reactions