Re:Modern Python Performance Considerations
by SuaveSteve on Thursday May 05, 2022 @09:52AM
(#31273339)
Why not switch to making __slots__ in classes the default and then making attribute changes to an object during runtime an opt-in? It will require a long grace period but wouldn't it help optimisation efforts immensely?
[ Parent
| on HN ]
Re:Modern Python Performance Considerations
by gjulianm on Thursday May 05, 2022 @10:11AM
(#31273577)
That's going to require quite a lot of changes, it's a giant breaking change. All classes would need someone to go around finding all the attributes that are created and adding an __slots__ dictionary, to avoid regular attribute initialization in __init__ failing. It's a massive task, and it would completely break backwards compatibility for performance gains that not everybody will need.
Re:Modern Python Performance Considerations
by yedpodtrzitko on Thursday May 05, 2022 @09:59AM
(#31273417)
That would mean all installed dependencies need to comply with this change as well, which is unlikely to happen in any realistic timeframe.
default __slots__ breaks a lot of monkey patching.
An "easier" change would be to add a class attribute "no__dict__", which says that the __dict__ attribute can't be used, which lets the implementation do whatever it wants. That can be incrementally added to classes.
Another option is a "no__getattr__" attribute, which disables gettattr and friends.
Re:Modern Python Performance Considerations
by BurningFrog on Thursday May 05, 2022 @11:08AM
(#31274332)
Where can I read about what kind of performance improvements `__slots__` brings?
Re:Modern Python Performance Considerations
by WillDaSilva on Thursday May 05, 2022 @12:00PM
(#31275047)
The Python docs themselves is a good place to start: https://docs.python.org/3/reference/datamodel.html#slots
The Python wiki also has some good info about it: https://wiki.python.org/moin/UsingSlots
Re:Modern Python Performance Considerations
by disconnect3d on Friday May 06, 2022 @06:08AM
(#31283406)
Apart from the official docs, this video also explains the low level data layout (in CPython) that slots introduce: https://www.youtube.com/watch?v=Iwf17zsDAnY