const() function

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
fma
Posts: 163
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

const() function

Post by fma » Mon Dec 15, 2014 7:02 pm

Hi!

Reading code from the mpr121 module, I discovered that the author used a const() function to define some constants.

What exactly does this function()? Is it to put the value in the flash, rather than in memory?
Frédéric

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: const() function

Post by pfalcon » Mon Dec 15, 2014 7:08 pm

Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

fma
Posts: 163
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: const() function

Post by fma » Mon Dec 15, 2014 7:24 pm

Still not sure to clearly understand what it does... I made a quick test, and it seems that it consumes memory :?
Frédéric

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: const() function

Post by pfalcon » Mon Dec 15, 2014 10:49 pm

Code: Select all

FOO = const(1)
defines FOO to be a constant with value 1. That's all, and should be pretty simple. Or if you want to know implications and details, see the ticket above.

Note that it's extension to CPython, and won't work with it out of the box. (But making it work is trivial:

Code: Select all

const = lambda x: x
)
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

fma
Posts: 163
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: const() function

Post by fma » Tue Dec 16, 2014 6:05 am

Sorry, but I don't really understand what is said in the post; I'm not a C-coder :oops:

It's a constant from a C-dev point of view, am I right?

But from a python point of view, how and when should I use it? What are the pros and cons?
Frédéric

Damien
Site Admin
Posts: 635
Joined: Mon Dec 09, 2013 5:02 pm

Re: const() function

Post by Damien » Tue Dec 16, 2014 12:06 pm

Using const allows optimisation, but retains compatibility with Python semantics (as much as possible).

The idea is that you want to substitute the constant value directly in your code. If you write some Python code that uses a (global) variable, then looking up that global variable is relatively expensive (in time). If it's a constant value then you could just write that constant value in the code directly (eg i2c.send(0x42, 0x54)), but that would make it difficult to read (don't want magic numbers!).

Micro Python recognises the "x = const(123)" pattern in your script and during the compile stage will replace "x" in your code with "123", in that module/file. This makes it a lot faster when running.

The reason it also uses RAM is because it still puts "x" in memory as a global variable, in case someone external to the module also wants to use this constant; eg mpr121.DEBOUNCE will still work.

The recommended use for const is: use it when you have a global variable which is a constant integer that is set once and never changed.

fma
Posts: 163
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: const() function

Post by fma » Tue Dec 16, 2014 12:58 pm

Ok, I understand ; it is a speed-vs-size tool...

Thanks for the explanation.
Frédéric

User avatar
kfricke
Posts: 342
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: const() function

Post by kfricke » Tue Dec 16, 2014 1:58 pm

Thanks for this feature and the graspable explanation. The example from Damien did give a good use-case for this. Especially with I2C addresses and bitmasks this is a nice feature. Reasonable close to a 'define'ment in C.

Damien
Site Admin
Posts: 635
Joined: Mon Dec 09, 2013 5:02 pm

Re: const() function

Post by Damien » Tue Dec 16, 2014 9:52 pm

fma wrote:Ok, I understand ; it is a speed-vs-size tool...
Actually, the const optimisation can in some cases use slightly less RAM than the non-optimised case: in the non-optimised case the lookup of the const global requires some byte code to do the lookup. If it's replaced with a constant, then the load of the constant can take less RAM (in the byte code function) if the constant is a small integer.

Also, with constants the compiler can optimise things like REG | FLAG into a single constant, thus eliminating the byte code to load the 2 values and compute the or. This therefore uses less RAM.

All in all, using constants is best all round.

fma
Posts: 163
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: const() function

Post by fma » Wed Dec 17, 2014 6:15 am

I'll give a try with the app I'm currently developing, which uses a lot of constants (this is a port from a C app, which uses a lot of #define) :

https://github.com/fma38/micropython/tree/master/baos
Frédéric

Post Reply