Does "__slots__" make sense in micropython
Does "__slots__" make sense in micropython
Hi
Is there a difference in using __slots__ vs __dict__ in MicroPython. I am not familiar with the inner workings.
Regards
Is there a difference in using __slots__ vs __dict__ in MicroPython. I am not familiar with the inner workings.
Regards
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Does "__slots__" make sense in micropython
You might like to see this discussion.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Does "__slots__" make sense in micropython
Interesting,
Shouldn't it be mentioned in the docs (maybe in the Maximising Micropython Speed) ?
As I have I understand it could help save RAM. It not exactly speed optimisation, but it could help as I have frequently hit the RAM limit...
Am I wrong ?
Shouldn't it be mentioned in the docs (maybe in the Maximising Micropython Speed) ?
As I have I understand it could help save RAM. It not exactly speed optimisation, but it could help as I have frequently hit the RAM limit...
Am I wrong ?
nodemcu V2 (amica)
micropython firmware Daily build 05/31/2016
micropython firmware Daily build 05/31/2016
Re: Does "__slots__" make sense in micropython
nodemcu V2 (amica)
micropython firmware Daily build 05/31/2016
micropython firmware Daily build 05/31/2016
Re: Does "__slots__" make sense in micropython
I have made some tests, and confirm it save some RAM :
Code: Select all
import gc
import micropython
class MyClass(object):
#__slots__ = ['name', 'identifier']
def __init__(self, name, identifier):
self.name = name
self.identifier = identifier
def MyPrint(self):
print('{}:{}'.format(self.name,self.identifier))
gc.collect()
micropython.mem_info()
toto=[]
for n in range(20):
toto.append(MyClass('name','identifier'))
print("---------------------")
micropython.mem_info()
# ===========================================================
# Without __slot__
#
# stack: 2096 out of 8192
# GC: total: 37952, used: 7792, free: 30160
# No. of 1-blocks: 23, 2-blocks: 8, max blk sz: 264, max free sz: 1600
# ---------------------
# stack: 2096 out of 8192
# GC: total: 37952, used: 8592, free: 29360
# No. of 1-blocks: 64, 2-blocks: 7, max blk sz: 264, max free sz: 1600
#
# Used diff: 800
#
# ===================================================================
#
# With __slot__
#
# stack: 2096 out of 8192
# GC: total: 37952, used: 7840, free: 30112
# No. of 1-blocks: 25, 2-blocks: 7, max blk sz: 264, max free sz: 1627
# ---------------------
# stack: 2096 out of 8192
# GC: total: 37952, used: 8640, free: 29312
# No. of 1-blocks: 66, 2-blocks: 6, max blk sz: 264, max free sz: 1627
#
# Used diff: 352
nodemcu V2 (amica)
micropython firmware Daily build 05/31/2016
micropython firmware Daily build 05/31/2016
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Does "__slots__" make sense in micropython
There is interesting discussion of __slots__ here.
A difference between MicroPython and CPython is that in CPython defining __slots__ prevents the creation of any other bound variables. Consider:
Under CPython the last line throws an AttributeError whereas MicroPython accepts it. In CPython foo has no __dict__ attribute while MicroPython does.
A difference between MicroPython and CPython is that in CPython defining __slots__ prevents the creation of any other bound variables. Consider:
Code: Select all
class Foo:
__slots__ = ()
foo = Foo()
foo.a = 42
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Does "__slots__" make sense in micropython
MicroPython doesn't support __slots__. The referenced PR (https://github.com/micropython/micropython/pull/3392) was never merged.
Your tests show the difference as 800 bytes in both cases.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Does "__slots__" make sense in micropython
Do you happen to know why? It seems like a useful optimisation in terms of RAM usage and/or attribute access time. The CPython docs claim an access time improvement.jimmo wrote: ↑Sun Dec 01, 2019 11:15 amMicroPython doesn't support __slots__. The referenced PR (https://github.com/micropython/micropython/pull/3392) was never merged.
...
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Does "__slots__" make sense in micropython
Good question. It's not a huge amount of code. Might be worth revisiting now that MicroPython has the performance test suite?pythoncoder wrote: ↑Sun Dec 01, 2019 11:22 amDo you happen to know why? It seems like a useful optimisation in terms of RAM usage and/or attribute access time. The CPython docs claim an access time improvement.
The benefit for MicroPython compared to CPython is likely to be a lot smaller due to the way dicts are represented in MicroPython, but there is still an obvious RAM benefit. Performance... I don't have a good guess, but my intuition matches the comment from that PR that there wouldn't be much expected difference in MicroPython.
It's an interesting one -- as the PR also points out, the benefit also only kicks in asymptotically... I wonder how common is it to have enough instances of a given type that this actually starts to make a meaningful difference (but consider that the allocations are rounded up to a 16 byte GC block, which may work for or against this feature resulting in a significant difference...). I'm curious though, so will try and get some actual numbers sometime.