-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Description
Port, board and/or hardware
Raspberry Pi Pico W, esp32
MicroPython version
MicroPython v1.23.0 on 2024-06-02; Raspberry Pi Pico W with RP2040
Reproduction
Not optimized
- Save file
from micropython import const
_MY_CONST1: int = const(1)
_MY_CONST2: int = const(2)
_MY_CONST3: int = const(2)
def print_const():
print(f"1:{_MY_CONST1} 2:{_MY_CONST2} 3:{_MY_CONST3}")
- run
mpy-cross c.py
- run
mpy-tool.py -d c.mpy
mpy_source_file: c.mpy
source_file: c.py
header: 4d:06:00:1f
qstr_table[10]:
c.py
<module>
const
micropython
print_const
format
_MY_CONST1
_MY_CONST2
_MY_CONST3
print
obj_table: ['1:{} 2:{} 3:{}']
simple_name: <module>
raw bytecode: 46 08:0a:01:4c:27:27:67:80:10:02:2a:01:1b:03:1c:02:16:02:59:11:02:81:34:01:16:06:11:02:82:34:01:16:07:11:02:82:34:01:16:08:32:00:16:04:51:63
prelude: (2, 0, 0, 0, 0, 0)
args: []
line info: 4c:27:27:67
80 LOAD_CONST_SMALL_INT 0
10:02 LOAD_CONST_STRING const
2a:01 BUILD_TUPLE 1
1b:03 IMPORT_NAME micropython
1c:02 IMPORT_FROM const
16:02 STORE_NAME const
59 POP_TOP
11:02 LOAD_NAME const
81 LOAD_CONST_SMALL_INT 1
34:01 CALL_FUNCTION 1
16:06 STORE_NAME _MY_CONST1
11:02 LOAD_NAME const
82 LOAD_CONST_SMALL_INT 2
34:01 CALL_FUNCTION 1
16:07 STORE_NAME _MY_CONST2
11:02 LOAD_NAME const
82 LOAD_CONST_SMALL_INT 2
34:01 CALL_FUNCTION 1
16:08 STORE_NAME _MY_CONST3
32:00 MAKE_FUNCTION 0
16:04 STORE_NAME print_const
51 LOAD_CONST_NONE
63 RETURN_VALUE
children: ['print_const']
simple_name: print_const
raw bytecode: 24 28:06:04:80:08:12:09:23:00:14:05:12:06:12:07:12:08:36:03:34:01:59:51:63
prelude: (6, 0, 0, 0, 0, 0)
args: []
line info: 80:08
12:09 LOAD_GLOBAL print
23:00 LOAD_CONST_OBJ '1:{} 2:{} 3:{}'
14:05 LOAD_METHOD format
12:06 LOAD_GLOBAL _MY_CONST1
12:07 LOAD_GLOBAL _MY_CONST2
12:08 LOAD_GLOBAL _MY_CONST3
36:03 CALL_METHOD 3
34:01 CALL_FUNCTION 1
59 POP_TOP
51 LOAD_CONST_NONE
63 RETURN_VALUE
children: []
Optimized
Not optimized
- Save file
from micropython import const
_MY_CONST1 = const(1)
_MY_CONST2 = const(2)
_MY_CONST3 = const(2)
def print_const():
print(f"1:{_MY_CONST1} 2:{_MY_CONST2} 3:{_MY_CONST3}")
- run
mpy-cross c.py
- run
mpy-tool.py -d c.mpy
mpy_source_file: c.mpy
source_file: c.py
header: 4d:06:00:1f
qstr_table[7]:
c.py
<module>
const
micropython
print_const
format
print
obj_table: ['1:{} 2:{} 3:{}']
simple_name: <module>
raw bytecode: 23 08:06:01:8c:07:80:10:02:2a:01:1b:03:1c:02:16:02:59:32:00:16:04:51:63
prelude: (2, 0, 0, 0, 0, 0)
args: []
line info: 8c:07
80 LOAD_CONST_SMALL_INT 0
10:02 LOAD_CONST_STRING const
2a:01 BUILD_TUPLE 1
1b:03 IMPORT_NAME micropython
1c:02 IMPORT_FROM const
16:02 STORE_NAME const
59 POP_TOP
32:00 MAKE_FUNCTION 0
16:04 STORE_NAME print_const
51 LOAD_CONST_NONE
63 RETURN_VALUE
children: ['print_const']
simple_name: print_const
raw bytecode: 21 28:06:04:80:08:12:06:23:00:14:05:81:82:82:36:03:34:01:59:51:63
prelude: (6, 0, 0, 0, 0, 0)
args: []
line info: 80:08
12:06 LOAD_GLOBAL print
23:00 LOAD_CONST_OBJ '1:{} 2:{} 3:{}'
14:05 LOAD_METHOD format
81 LOAD_CONST_SMALL_INT 1
82 LOAD_CONST_SMALL_INT 2
82 LOAD_CONST_SMALL_INT 2
36:03 CALL_METHOD 3
34:01 CALL_FUNCTION 1
59 POP_TOP
51 LOAD_CONST_NONE
63 RETURN_VALUE
children: []
Expected behaviour
_MY_CONST1: int = const(1)
should be optimized as _MY_CONST1 = const(1)
Observed behaviour
PyCharm supports type hints, which is very useful. When using type hints e.g. _MY_CONST1: int = const(1)
optimizer does not work as expected (used mpy-cross
and mpy-tool
to show the content of .mpy file). In .mpy
file you can see _MY_CONST1
in qstr table, STORE_NAME _MY_CONST1
and LOAD_NAME const
. The result file has 162 bytes
When not using type hints e.g. _MY_CONST1 = const(1)
everything is fine, the code has been optimized, and you can see in .mpy
file LOAD_CONST_SMALL_INT 1
instead of _MY_CONST1
(100 bytes),
Additional Information
Used mpy_cross 1.24.0rc0
as optimizer
Code of Conduct
Yes, I agree