-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Constants and efficiency #266
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
Comments
Good write up, close to what I had in mind ;-). So, some additions/ideas. First of all, if we have very high-level language, then first of all we want clarity and user-friendliness, so we definitely want to have "GPIO_OutputMode" instead of "0x40" as general, user-facing API. Granted, next thing we may want is to gain some efficiency still. Good way to get good gains is to do static optimization (like constant propagation), but statically optimizing dynamic language means giving up some dynamicity. Let's just accept that, and it will be easier to make choices. Specific points:
Yeah, it sucks a bit that python can't annotate statements, or at least declarations, one might hope for following syntax, but alas:
Yes, then we apparently should go for such syntax, this is similar to how MyPy annotates vars for example: http://www.mypy-lang.org/tutorial.html#expilicitypes
Ok, so code which can be optimized is one which won't play such tricks on modules. The way to implement it can be:
Also, this stuff don't have to be implemented by on-device compiler - this can be a unix version exporting optimized bytecode, or even separate tool. Though it would be both cool and IMHO possible to support simplest constant propagation by embedded compiler ;-). |
Whatever the solution, the same code must work in an equivalent way without optimisation / in CPython. |
Sure, that should be easy. |
Also note that 3.4 introduces Enums with constant values: http://docs.python.org/3.4/library/enum.html . But IMHO, it's a bit overengineered to use just to represent dead simple constants (but I can't be trusted here, I have the same feeling for most features introduced since 1.5.2). |
It seems that having cake and eating it too is always a problem :). So I feel its OK for a user to not get superoptimised results ALL the time.
But I certainly don't want to halt the discussion... |
@pfalcon I missed the enums but actually - even though I agree with you about 1.5.2 - this particular class is useful for allowing the definition in pure python of constants like this so it will run the same in Python, Cpython, and embedded. I wonder if we can't take advantage of these people shoving C constructs into Python (which as a LISP guy makes me physically ill) to use to our advantage. I.e use it specifically to define pins for example. Good examples of IntEnum and the planet inheritance example on the enum page... |
Probably doesn't help, but https://docs.python.org/3/library/constants.html has a small section talking about “true” constants. Edit: And just to keep all the cross-references in place: 57e99eb |
Constants have been supported for a long time now, via |
This allows us to re-enable `os`. `random` is also enabled because it solely depends on `os`. Fixes micropython#266. Its also a pre-requisite for micropython#260.
* atmel-samd: Add support for internal filesystems. This allows us to re-enable `os`. `random` is also enabled because it solely depends on `os`. Fixes micropython#266. Its also a pre-requisite for micropython#260. * atmel-samd: Update SAMD51 linker script comments and MICROPY_MAX_STACK_USAGE enabling.
Coming out of #227 was a discussion on using constants to specify ports, pins, modes, etc for configuring GPIO and peripherals. For example:
This is not very efficient because the first 2 lines store into the module namespace the 2 constants, taking up RAM. The above could be optimised by the compiler remembering the value of the constants and substituting the value whenever they are used. But this would break general Python semantics (since the globals can be written to elsewhere in the code), and not all global numbers are constants. To fix this one could wrap the number in a special function which declares it as a constant:
Okay, but what about sharing such definitions over multiple Python files? They would need to be put in a separate file (say pyb.py) and imported:
That can't be optimised because pyb may be assigned to something else during the course of execution. Thus we require something like:
and the compiler would recognise this as saying that pyb will not change and so it can lookup constants inside this import. But that requires the compiler having in RAM a very large hash table with all these constants.
The text was updated successfully, but these errors were encountered: