Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Implement user classes with __slots__ #3392

Closed
wants to merge 0 commits into from

Conversation

@pfalcon
Copy link
Contributor

commented Oct 28, 2017

This implements support for use classes with __slots__ defined (optimized memory usage).

I initial though it would require changes to the compiler, but it doesn't.

Note also the that initially I had mp_obj_slotted_make_new(), but that required splitting existing mp_obj_instance_make_new() for reuse and passing more args to (additional stack usage), plus some other minor issue. So, I reworked it to keep one mp_obj_instance_make_new(), the drawback of that is that it needs to lookup __slots__ in class' locals_dict on every object allocation.

@pfalcon pfalcon force-pushed the pfalcon:obj-slots branch from 4748aba to 500ec15 Nov 3, 2017

@pfalcon

This comment has been minimized.

Copy link
Contributor Author

commented Nov 3, 2017

Added MICROPY_CLASS_SLOTS config define.

@peterhinch

This comment has been minimized.

Copy link
Contributor

commented Nov 4, 2017

A useful optimisation.

@robert-hh

This comment has been minimized.

Copy link
Contributor

commented Nov 4, 2017

I had to read about slots first, and it looks like a useful enhancement, especially for RAM constrained devices. It's requirement of "declaring" symbols can also be helpful. Pythons convenience about creating symbols on-the-fly has a dark side too.

@stinos

This comment has been minimized.

Copy link
Contributor

commented Nov 4, 2017

Looks interesting. Do you have measurements of space savings/performance gains (which is significant in CPython)? Can you lay out the dfferences with CPython (e.g. with te latter if you assign __slots__ then you don't have __dict__ unless you add __dict__ to __slots__, is this the case here?)

@pfalcon

This comment has been minimized.

Copy link
Contributor Author

commented Nov 4, 2017

Yeah, this was for a long time in #1331, and IMHO an important part to make the core "complete" (given our focus of low-memory systems).

Do you have measurements of space savings/performance gains (which is significant in CPython)?

No, I don't. This is just an initial, proof of concept patch. But the expected gains are known: it asymptotically saves 50% of RAM on objects (because normal __dict__ stores keys/values, and __slots__ store only values), and no noticeable performance differences are expected (because we use O(n) lookup algos for both, as you hacked on namedtuple, that wouldn't be a surprise for you).

Can you lay out the dfferences with CPython (e.g. with te latter if you assign slots then you don't have dict unless you add dict to slots, is this the case here?)

This just implements __slots__ as a mutable version of namedtuple (wrapped in a class definition syntax, so more methods can be added). Tricks like above aren't handled (there's just no __dict__, that's all), and I'm not sure if we need them at all, definitely not for the first version.

@pfalcon pfalcon force-pushed the pfalcon:obj-slots branch from 500ec15 to 85b30a7 Nov 4, 2017

@prusnak

This comment has been minimized.

Copy link
Contributor

commented Jan 30, 2018

Which of the following common slot use cases are possible?

a) __slots__ = 'foo'
b) __slots__ = ['foo', 'bar']
c) __slots__ = {'foo': 1, 'bar': 2}
d) __slots__ = {'foo': (1, int), 'bar': (2, str)}
e) __slots__ = any iterable

@pfalcon

This comment has been minimized.

Copy link
Contributor Author

commented Jan 30, 2018

This one:

slots = ['foo', 'bar']

And I wouldn't call the rest "common" ;-).

Note that this patch no longer applies on the master.

@ph4r05

This comment has been minimized.

Copy link

commented Jul 18, 2018

To save some memory I would go for:

__slots__ = ('foo', 'bar')

IMO it makes sense to have slots immutable. Moreover, if tuples are implemented effectively it could be all frozen.

@dpgeorge dpgeorge closed this Oct 13, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.