|
|
|
|
|
by gshulegaard
618 days ago
|
|
Nope, __slots__ exist explicitly as an alternative to __dict__: https://wiki.python.org/moin/UsingSlots Whether or not the performance matters...well that's somewhat subjective since Python has a fairly high performance floor which makes performance concerns a bit of a, "Why are you doing it in Python?" question rather than a, "How do I do this faster in Python?" most of the time. That said it _is_ more memory efficient and faster on attribute lookup. https://medium.com/@stephenjayakar/a-quick-dive-into-pythons... Anecdotally, I have used Slotted Objects to buy performance headroom before to delay/postpone a component rewrite. |
|
And yes __slots__ improve perf, but it’s about avoiding the __dict__ access, which hits really generic hashing code and then memory probing more than it is about L1 cache
Where __slots__ are most useful (and IIRC what they were designed for) is when you have a lot of tiny objects and memory usage can shrink significantly as a result. That could be the difference between having to spill to disk or keeping the workload in memory. E.g., Openpyxl does with a spreadsheet model, where there could be tons of cell references floating around