-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Allow boards to enable the micropython.native
decorator
#2271
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
Conversation
Adds the `CIRCUITPY_ENABLE_MPY_NATIVE` for `mpconfigboard.mk` that enables the `micropython.native` decorator.
I think this is laudable, because it's a port-agnostic way of getting speedups (at the expense of RAM). At the same time, there is further work that could be done to speed up CircuitPython in general, by probably about a factor of 2: see #2142. I do worry about fixes to MicroPython related to this that haven't been merged. It's been a long time since we merged from upstream, so I think it would be worth looking at that. You could do a trial merge into a junk branch and see what's affected, or just browse the MicroPython "blame" info for the relevant code sections. |
Yeah, I'm happy to take the time and do this as "correctly" as possible and see if the latest code can be pulled in. I just didn't know if we've done any sort of piecemeal merging from upstream before. #2142 is great because it improves the speed of all ports without any need to opt-in (unlike this). This is a great parallel effort, though. |
(psst, im into this too, there's a lot of times we could have some optimized code for drivers!) |
After some initial exploration it looks like the changes to Not sure how we feel about it right now. We have three options:
|
I'm happy to have us try this, since it's all optional anyway. Yes, there may be bugs, but that's true of anything. We have no short-term plans for a merge from upstream: certainly not for 5.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one question: it looks great!
py/objfun.c
Outdated
@@ -546,7 +546,9 @@ STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) { | |||
STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { | |||
mp_obj_fun_asm_t *self = self_in; | |||
|
|||
#pragma GCC diagnostic ignored "-Wint-conversion" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tracked down why you need to do this? Is this older code that could use a change in parameter types, or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 3439c36, this one turned out to be pretty easy to fix once I looked at what the code was trying to do.
Fwiw I spent about an hour trying option (3) and got reasonably far, but there's some significant changes in |
In case anyone is curious about my work to update all of the native emitters there's a branch here. Perhaps after supercon I'll have some time to get that into a mergeable state. |
@theacodes Are you satisfied with the PR now, or are you still exploring? Happy to merge at this point. |
Satisfied for now. I did some more exploration and I think it'd be easier
to just do a full merge than graft the newer compiler code into our tree.
This is good to go.
Btw, I do actually have an nRF board so once I find some free time I'll
test it out on nRF boards, but this should be good to go for SAMD51 boards.
…On Thu, Nov 7, 2019, 9:04 AM Dan Halbert ***@***.***> wrote:
@theacodes <https://github.com/theacodes> So are you satisfied with the
PR now, or are you still exploring?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2271?email_source=notifications&email_token=AAB5I46LQXM47DL54NTOKXLQSRDABA5CNFSM4JJLOEF2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEDNDKWY#issuecomment-551171419>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAB5I4ZCHQD6NXH7FDXR4T3QSRDABANCNFSM4JJLOEFQ>
.
|
Sounds good. I'll wait for you to test on nRF and then merge if that goes fine. |
Tested on nRF (Circuit Playground Bluefruit alpha) Build size without
Build size with it:
So 20 more bytes? Test code: import time
import micropython
def math_py():
x = 5
for n in range(100000):
x += 1
x -= 1
x *= 10
x /= 10
@micropython.native
def math_native():
x = 5
for n in range(100000):
x += 1
x -= 1
x *= 10
x /= 10
now = time.monotonic()
math_py()
print("Python elapsed: ", time.monotonic() - now)
now = time.monotonic()
math_native()
print("Native elapsed: ", time.monotonic() - now) Result:
🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, great! On to real-world testing and use!
Thanks for this! I've been manually tweaking my repos when I wanted these for the past year or two. Oddities if you're used to CPython:
As seen in https://github.com/gpshead/epaper-circuitpython/blob/master/main.py Other things... |
I think upstream implemented it but we'd have to do a full merge to get
that feature.
I definitely agree that supporting both boards with and without this
feature is a bit tedious.
…On Fri, Nov 8, 2019 at 11:36 AM Gregory P. Smith ***@***.***> wrote:
Thanks for this! I've been manually tweaking my repos when I wanted these
for the past year or two.
Oddities if you're used to CPython: @micropython.asm_thumb et. al. aren't
*actual* callable decorators. On builds without these enabled, the file
fails to parse. Minor bummer, but that's micro-life. Not a big deal. What I
did in this scenario is just keep different implementations in different
files, only loading one of them:
try:
# Optimized version - requires a custom CircuitPython build.
from asm_thumb import fractal
from asm_thumb import monobitmap
HAVE_ASM = True
except (ImportError, SyntaxError):
import fractal
import monobitmap
HAVE_ASM = False
*As seen in
https://github.com/gpshead/epaper-circuitpython/blob/master/main.py
<https://github.com/gpshead/epaper-circuitpython/blob/master/main.py>*
Other things... mpycross doesn't handle @micropython.asm_thumb files (I
haven't tried with native or viper). Meaning a lot more ram is required
just to load code using it. It seems plausible to implement but I'm not
sure if it ever was. I haven't checked upstream micropython or tried to
dive into that rabbit hole.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2271?email_source=notifications&email_token=AAB5I47MMYTVOT3FRP7P633QSW5VLA5CNFSM4JJLOEF2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEDTEODY#issuecomment-551962383>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAB5I45XWOHKMHITNUNTZSTQSW5VLANCNFSM4JJLOEFQ>
.
|
@gpshead We should add noop decorators to the micropython module in Blinka: https://github.com/adafruit/Adafruit_Blinka/blob/master/src/micropython.py |
I tried the sample code above on an nRF52840 Feather running CircuitPython 5.3.0-184-g90bd93180 on 2020-05-09. The native runs slightly slower: Python elapsed: 4.591 Python elapsed: 4.59094 Python elapsed: 4.59009 |
Adds
CIRCUITPY_ENABLE_MPY_NATIVE
formpconfigboard.mk
that allows boards to enable themicropython.native
decorator.I am 100% open to suggestions on this PR. I wanted to open a PR that more or less does the minimal amount of work to enable this. As such, I had to disable a few warnings to get this to build. The upstream code for this has deviated a bit, so if it's preferable to backport that code instead I'm up for it.
Related to #1248. I would enable it and test and mark as closes, but I don't have an nRF board to test with. It works fine for my SAMD51 boards (Feather M4 Express & Winterbloom Sol).