10000 Combine load_attr and store_attr type methods by dpgeorge · Pull Request #1174 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

Combine load_attr and store_attr type methods #1174

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

Closed
wants to merge 1 commit into from

Conversation

dpgeorge
Copy link
Member
@dpgeorge dpgeorge commented Apr 1, 2015

This simplifies the API for objects and reduces code size by combining the .load_attr and .store_attr methods in mp_obj_type_t.

Code size decreases:

  • 2816 on unix x86
  • 1588 on minimal
  • 252 on bare-arm
  • 444 on stmhal
  • 384 on cc3200
  • 400 on teensy

In also saves RAM: each user-defined class is 1 word smaller. Furthermore, any further additions of new static objects will be 1 word smaller than previously.

But it does also hamper performance, although only very slightly. I measured about 1% decrease in pystone score on x86 (down from around 84k to around 83k).

My feeling is that this is a good patch to merge because it reduces code size by a non-trivial amount, and reduces RAM usage, making micropython more micro. If people want performance there's CPython and PyPy for that (and Pyston and Cython etc). If a port has bytes to spend on optimisations, then likely there are better things to spend those bytes on than separating load/store. Note that subscr load/store is already combined.

This simplifies the API for objects and reduces code size (by around 400
bytes on Thumb2, and around 2k on x86).  It reduced performance a
little, measured as 1% decrease in Pystone score.
@danicampora
Copy link
Member

I think this is a very good patch, specially because of the RAM savings with the user defined objects.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 90.88% when pulling 6a27edc on combine-load-store-attr into 2686f9b on master.

@pfalcon
Copy link
Contributor
pfalcon commented Apr 1, 2015

Furthermore, any further additions of new static objects will be 1 word smaller than previously.

Not sure I understand this.

Otherwise, the arguments above are very reasonable, though it's still a bit said that we'll use that 1% (and it won't be just 1% then) even for super-optimized viper code. I hope you'll recover opportunity to have 1 vs 2 methods in that meta-MicroPython you dream of ;-).

@dpgeorge
Copy link
Member Author
dpgeorge commented Apr 1, 2015

Furthermore, any further additions of new static objects will be 1 word smaller than previously.

Not sure I understand this.

I just meant that if you add a new object (eg pyb.I2S) then it'll cost 1 word less in ROM than it would have without this patch, because it has 1 less entry in mp_obj_type_t.

Otherwise, the arguments above are very reasonable, though it's still a bit said that we'll use that 1% (and it won't be just 1% then) even for super-optimized viper code.

Yes, I'm not 100% happy to lose performance, hence why I made this a PR to get feedback. But I think we ought to do proper performance analysis to see where bytes are worth spending, rather than guessing.

I hope you'll recover opportunity to have 1 vs 2 methods in that meta-MicroPython you dream of ;-).

That would certainly be the ideal situtation! It would even be possible now to make it configurable, but would be pretty ugly.

@pfalcon
Copy link
Contributor
pfalcon commented Apr 1, 2015

I just meant that if you add a new object (eg pyb.I2S)

I see, (kinda) singleton object you mean, because savings indeed are for types.

It would even be possible now to make it configurable, but would be pretty ugly.

Yes. There're more #ifdef's coming all the time, and hopefully for more important things than trade-off between 1% performance and 0.5% of code space, so just go one way or another here.

@blmorris
Copy link
Contributor
blmorris commented Apr 1, 2015

Will this patch have any impact if I'm writing I2S to make dynamic objects instead of static?
I don't see any changes to existing object classes, so I'll assume that either way I won't need to adapt my code.

@dpgeorge
Copy link
Member Author
dpgeorge commented Apr 1, 2015

No, it shouldn't have any impact either way.

@stinos
Copy link
Contributor
stinos commented Apr 2, 2015

If people want performance there's CPython and PyPy for that (and Pyston and Cython etc)

Except those do not run on most embedded platforms let alone pyboard?

@dpgeorge
Copy link
Member Author
dpgeorge commented Apr 2, 2015

Except those do not run on most embedded platforms let alone pyboard?

Yes, you have a point. uPy is a tradeoff amongst Python compatibility, code size, RAM usage and efficiency. It's a difficult balancing act.

To confuse matters more, this patch actually improves pystone score on pyboard by 2%! Increases from 1717 to 1755. Because I don't understand this improvement I'm uncertain as to what this patch does under the hood.

@danicampora
Copy link
Member

So, it decreases performance on x86 but improves it on thumb2? Interesting...

@dpgeorge
Copy link
Member Author

It looks like @pfalcon and I are +/-0 with this, @danicampora is +1, @stinos is -1. Any more good arguments either way?

@dpgeorge
Copy link
Member Author

Just rebased this PR and measured pystone performance on x64 and I'm getting no noticeable change with or without this patch. Same on pyboard.

@pfalcon
Copy link
Contributor
pfalcon commented Apr 11, 2015

Well, I'm +0.75 on this, let's have this merged!

@danicampora
Copy link
Member

Yes, +1 on merging it :-)

@dpgeorge
Copy link
Member Author

Merged in b1bbe96.

@stinos you shouldn't notice any slow down :)

@dpgeorge dpgeorge closed this Apr 11, 2015
@dpgeorge dpgeorge deleted the combine-load-store-attr branch April 11, 2015 15:57
@stinos
Copy link
Contributor
stinos commented Apr 11, 2015

No problem; for the record I wasn't really -1, more like 0: I didn't measure performance in an actual application myself and I'm not sure how well pystone score resembles what happens in an actual application? Just wanted to point out that while CPython/PyPy are nice, none of them nor any other I tried came even close to building on all kinds of 'minimal' platforms.

@dpgeorge
Copy link
Member Author

I didn't measure performance in an actual application myself and I'm not sure how well pystone score resembles what happens in an actual application?

Neither am I. But it seems to test quite a broad range of operations so is somewhat indicative of overall performance. Really we need a proper benchmark suite to make decisions about efficiency gains/loses.

Just wanted to point out that while CPython/PyPy are nice, none of them nor any other I tried came even close to building on all kinds of 'minimal' platforms.

uPy aims to be minimal and not inefficient. Low RAM usage is top priority, then low code size, then performance, although sometimes performance takes over low code size. If we didn't care about performance at all then it would be a different project. Running on a microcontroller or embedded environment also means you need to be efficient in CPU cycles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0