Skip to content

Make the @micropython.native decorator no-op if support isn't enabled #2282

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

Merged
merged 1 commit into from
Nov 26, 2019

Conversation

theacodes
Copy link

When adding the ability for boards to turn on the @micropython.native, viper, and asm_thumb decorators it was pointed out that it's somewhat awkward to write libraries and drivers that can take advantage of this since the decorators raise SyntaxErrors if they aren't enabled. In the case of viper and asm_thumb this behavior makes sense as they require writing non-normative code. Drivers could have a normal and viper/thumb implementation and implement them as such:

try:
    import _viper_impl as _impl
except SyntaxError:
    import _python_impl as _impl

def do_thing():
    return _impl.do_thing()

For native, however, this behavior and the pattern to work around it is less than ideal. Since native code should also be valid Python code (although not necessarily the other way around) using the pattern above means duplicating the Python implementation and adding @micropython.native in the code. This is an unnecessary maintenance burden.

So, this PR adds a new decorator: @micropython.native_if_available. On boards with CIRCUITPY_ENABLE_MPY_NATIVE turned on it operates exactly like @micropython.native. On boards with it turned off it does nothing- it doesn't raise a SyntaxError and doesn't apply optimizations. This means we can write our drivers/libraries once and take advantage of speedups on boards where they are enabled.

@theacodes
Copy link
Author

This has not be tested yet on hardware, I'll update when I'd had a chance to test it out - just wanted to start the discussion.

@tannewt
Copy link
Member

tannewt commented Nov 14, 2019

What if we just make @micropython.native work this way? We could add a separate way python code could test to see if it is supported.

@theacodes
Copy link
Author

theacodes commented Nov 14, 2019 via email

@dhalbert
Copy link
Collaborator

I'm up for that but I didn't want to deviate from MicroPython's behavior with something that uses the same name.

I'm not sure we have to be completely consistent with MicroPython here, since any code that uses this is not too likely to be shared without change between MicroPython and CircuitPython.

We could also add circuitpython.viper that is a no-op if viper is off, but that might be a more radical step :) .

@theacodes
Copy link
Author

Cool, if everyone is cool with deviating from MicroPython I'll just change this to make native behave as described here.

We could also make it circuitpython.native instead of micropython.native (same for viper and asm_thumb, even though we're not yet changing their behavior).

Making viper no-op isn't an option as far as I can tell, as viper code isn't standard python core and can't be processed by the normal bytecode emitter.

@tannewt
Copy link
Member

tannewt commented Nov 19, 2019

+1 deviating.

@siddacious
Copy link

Sounds bueno to me

@theacodes theacodes changed the title Add @micropython.native_if_available decorator Make the @micropython.native decorator no-op if support isn't enabled Nov 25, 2019
@theacodes
Copy link
Author

Okay, I've made this where micropython.native will no-op instead of adding a new, separate decorator. I still need to test this on actual hardware.

(Updated the PR title as well)

@theacodes
Copy link
Author

Okay, tested on real hardware and all seems well.

Unfortunately, it looks like the added native QSTR pushes pirkey_m0s Pinyin translation over the size limit. I'd love to hear any suggestions on how to resolve that.

@tannewt
Copy link
Member

tannewt commented Nov 26, 2019

Sit tight. I'm freeing up some space in pIRkey with PR #2328.

@theacodes
Copy link
Author

theacodes commented Nov 26, 2019 via email

When adding the ability for boards to turn on the `@micropython.native`, `viper`, and `asm_thumb` decorators it was pointed out that it's somewhat awkward to write libraries and drivers that can take advantage of this since the decorators raise `SyntaxErrors` if they aren't enabled. In the case of `viper` and `asm_thumb` this behavior makes sense as they require writing non-normative code. Drivers could have a normal and viper/thumb implementation and implement them as such:

```python
try:
    import _viper_impl as _impl
except SyntaxError:
    import _python_impl as _impl

def do_thing():
    return _impl.do_thing()
```

For `native`, however, this behavior and the pattern to work around it is less than ideal. Since `native` code should also be valid Python code (although not necessarily the other way around) using the pattern above means *duplicating* the Python implementation and adding `@micropython.native` in the code. This is an unnecessary maintenance burden.

This commit *modifies* the behavior of the `@micropython.native` decorator. On boards with `CIRCUITPY_ENABLE_MPY_NATIVE` turned on it operates as usual. On boards with it turned off it does *nothing*- it doesn't raise a `SyntaxError` and doesn't apply optimizations. This means we can write our drivers/libraries once and take advantage of speedups on boards where they are enabled.
@theacodes
Copy link
Author

Rebased to pick up the pIRkey changes. Fingers crossed that CI passes now. :)

@tannewt
Copy link
Member

tannewt commented Nov 26, 2019

🤞

@theacodes
Copy link
Author

Tests pass, yay!

Are there any other concerns or is this good to merge?

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thank you!

@tannewt tannewt merged commit cc00859 into adafruit:master Nov 26, 2019
@theacodes theacodes deleted the native-if-available branch November 26, 2019 23:59
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.

4 participants