8000 py/compile: Remove "return if" optimisation, it's rarely used. by dpgeorge · Pull Request #1505 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

py/compile: Remove "return if" optimisation, it's rarely used. #1505

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

Conversation

dpgeorge
Copy link
Member

Motivation for this PR is purely to reduce complexity of compiler.

The code that is being removed would optimise "return x if y else z" patterns, to save 2 bytes of RAM in the generated bytecode and eliminate a jump. It costs 56 bytes of code. It was there originally to agree with CPython generated bytecode.

This coding pattern is not common and there are better places to spend 56 bytes of code space.

I'm putting this up as a PR to see what other people's opinions are: is it worth optimising for a rarely used coding pattern? Arguably there are much more common patterns than this that could be optimised instead.

This code would optimise "return x if y else z" patterns, to save 2 bytes
of RAM in the generated bytecode and eliminate a jump.  It costs 56 bytes
of code.  It was there originally to agree with CPython generated
bytecode.

This coding pattern is not common and there are better places to spend
56 bytes of code space.
@pfalcon
Copy link
Contributor
pfalcon commented Oct 12, 2015

Well, my idea would be that compiler grows in complexity, not reduces ;-).

there are better places to spend 56 bytes of code space.

Probably yes, but 56 bytes is not that much.

Arguably there are much more common patterns than this that could be optimised instead.

Yes, replacing this with some other optimization would be more convincing ;-). Overall, if you're working on compiler and plan to add more optimizations then removing this may be grounded, but if it reandomly caught your eye, then why removing something on which previously effort was spent?

@dpgeorge
Copy link
Member Author

Overall, if you're working on compiler and plan to add more optimizations then removing this may be grounded,

Yes and no. Working on compiler/parser to implement various new ideas lead to finding this (and lead to 64f2b21). I wanted to drastically reduce parse tree size, implement persistent bytecode (#222) and do proper constant tables (#722). But this current PR is neither here nor there for those things.

Anyway, reason for PR is to get other's opinions :)

@pfalcon
Copy link
Contributor
pfalcon commented Oct 12, 2015

Anyway, reason for PR is to get other's opinions :)

Well, then persistent bytecode is the most important of these things - if you have that, you can drop entire compiler ;-).

@dpgeorge
Copy link
Member Author

Not yet... :)

@dpgeorge
Copy link
Member Author

No real motivation for this so let's leave it for now.

@dpgeorge dpgeorge closed this Oct 19, 2015
@dpgeorge dpgeorge deleted the remove-rarely-used-optimisation branch October 19, 2015 22:32
@dpgeorge
Copy link
Member Author
dpgeorge commented Jun 9, 2017

For the record, this optimisation was made configurable in ae54fbf

@pfalcon
Copy link
Contributor
pfalcon commented Jun 9, 2017

For the record, this optimisation was made configurable in ae54fbf

Is it enabled for mpy-cross?

@dpgeorge
Copy link
Member Author

Is it enabled for mpy-cross?

Yes, all compiler optimisations are enabled for mpy-cross.

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.

2 participants
0