Page 1 of 2

const() function

Posted: Mon Dec 15, 2014 7:02 pm
by fma
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?

Re: const() function

Posted: Mon Dec 15, 2014 7:08 pm
by pfalcon

Re: const() function

Posted: Mon Dec 15, 2014 7:24 pm
by fma
Still not sure to clearly understand what it does... I made a quick test, and it seems that it consumes memory :?

Re: const() function

Posted: Mon Dec 15, 2014 10:49 pm
by pfalcon

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
)

Re: const() function

Posted: Tue Dec 16, 2014 6:05 am
by fma
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?

Re: const() function

Posted: Tue Dec 16, 2014 12:06 pm
by Damien
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.

Re: const() function

Posted: Tue Dec 16, 2014 12:58 pm
by fma
Ok, I understand ; it is a speed-vs-size tool...

Thanks for the explanation.

Re: const() function

Posted: Tue Dec 16, 2014 1:58 pm
by kfricke
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.

Re: const() function

Posted: Tue Dec 16, 2014 9:52 pm
by Damien
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.

Re: const() function

Posted: Wed Dec 17, 2014 6:15 am
by fma
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