native and viper decorators on ESP32

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: native and viper decorators on ESP32

Post by OutoftheBOTS_ » Sat Mar 31, 2018 9:30 pm

Holy shit batman
viper code is about 10-20 timer faster than standard Python code
I still don't understand if this is possible why is it not the standard rather than only used when specified by the decorator?? What's the downside??
Not supported on ESP32.
Is this because the ESP32 isn't capable or is it that no one has written the code to support it yet??


There is 2 reasons that I ask all these questions:
1. To become better educated on all the possibilities so that I can write better code.
2. The keimatics of my robots are very maths heavy especially floating point (trigonometry and square root)

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: native and viper decorators on ESP32

Post by Roberthh » Sun Apr 01, 2018 7:00 am

Why is it not the standard: As said, viper code supports only a small subset of Python. In my view, it is more a set of assembler macros with interfaces to the bytecode world. It is good to access hardware directly, it is poor in dealing with Python data structures, and there is not protection at all. Still it can be helpful, if you split the parts with need for speed from the parts with data complexity.
I do not know how much is missing for ESP32, since it is available for ESP8266. The capability is there, may just a few missing interfaces & libs, that have to be arranged.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: native and viper decorators on ESP32

Post by OutoftheBOTS_ » Sun Apr 01, 2018 10:40 am

@RobertHH

Thanks for your answer.

The docs on native and viper emitters is very short and I am not going to be able to teach myself from them. Is there any blogs/tutorials you can suggest for me to study. I am a fast learner, only started to learn programming with my sons Lego mindstorm about 1.5yrs ago and started to learn about hardware a few months ago.

I do have very basic understanding of how the ALU works. I watched some videos of designing and constructing a chipset from basic NAND gate to building an adder then constructing an ALU with some basic concepts of assembler.

What I would like to learn to do is optimize floating point maths, firstly trig fuctions (tan, atan, sin, asin, cos, acos ). All these trig functions either return a float between -1 and 1 or radians between 2*pi and -2*pi, I don't need many decimal places. So not big number or many decimal places.

How many bytes does Micro-Python use to store floats?? does it use a 32bit machine word??

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: native and viper decorators on ESP32

Post by pythoncoder » Mon Apr 02, 2018 6:47 am

The docs are here. There is also information here and here (more technical).

Floats are stored in a single machine word, as are integers below a certain size (Python and MicroPython support integers of arbitrary size). Some MicroPython ports (e.g. Unix) support double precision FP which must use two machine words.

Pyboard:

Code: Select all

>>> 1/3
0.3333333
Unix:

Code: Select all

>>> 1/3
0.3333333333333333
Re optimising floating point: the FP functions are written in C. So I doubt that reduced precision versions written in MicroPython will be faster. Python is an interpreted language and MicroPython is optimised for code size rather than speed. But you could try with one function and see how it performs.

For real speed you can use the inline assembler. This can be over 100 times faster in real applications compared to MicroPython's standard code emitter. But writing a full suite of trig functions in assembler would be fairly challenging. It's certainly possible to gain speed by reducing precision: I have done this in the past to solve the specific problem of doing fast transforms between Cartesian and polar coordinates.
Peter Hinch
Index to my micropython libraries.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: native and viper decorators on ESP32

Post by OutoftheBOTS_ » Mon Apr 02, 2018 9:30 am

Thank you very much pythoncoder for the links :)

Very interesting read and very clever by the Micro-Python designers.

So am I right in understanding that normal byte-code emitters is just designed to optimize Ram usage due to the constrained environment that Micro-Python usually runs in and Native emitter will run all Micro-Python code but are designed more for speed and use more RAM??

It is a great pity that these capabilities are not yet included in the ESP32 port.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: native and viper decorators on ESP32

Post by pythoncoder » Wed Apr 04, 2018 8:14 am

Yes, that's broadly right. Bytecode is portable, in that if MicroPython is ported to some architecture, it will generate the bytecode and the MicroPython VM will run it. Since MicroPython is written in C, in principle all you need to achieve the port is a C compiler on the target.

To port the Native and Viper emitters is more difficult since you need to emit architecture dependent machine code. So code has to be specifically written to emit that instruction set.

This is as I understand it - I'm not a MicroPython developer.
Peter Hinch
Index to my micropython libraries.

Post Reply