summaryrefslogtreecommitdiffstatshomepage
path: root/tools/mpy-tool.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2019-09-16 22:12:59 +1000
committerDamien George <damien.p.george@gmail.com>2019-10-01 12:26:22 +1000
commitb5ebfadbd615de42c43851f27a062bacd9147996 (patch)
treee4602e96a0eaf9ee0c30913dbabfe9013dda617a /tools/mpy-tool.py
parent81d04a0200e0d4038c011e4946bfae5707ef9d9c (diff)
downloadmicropython-b5ebfadbd615de42c43851f27a062bacd9147996.tar.gz
micropython-b5ebfadbd615de42c43851f27a062bacd9147996.zip
py: Compress first part of bytecode prelude.
The start of the bytecode prelude contains 6 numbers telling the amount of stack needed for the Python values and exceptions, and the signature of the function. Prior to this patch these numbers were all encoded one after the other (2x variable unsigned integers, then 4x bytes), but using so many bytes is unnecessary. An entropy analysis of around 150,000 bytecode functions from the CPython standard library showed that the optimal Shannon coding would need about 7.1 bits on average to encode these 6 numbers, compared to the existing 48 bits. This patch attempts to get close to this optimal value by packing the 6 numbers into a single, varible-length unsigned integer via bit-wise interleaving. The interleaving scheme is chosen to minimise the average number of bytes needed, and at the same time keep the scheme simple enough so it can be implemented without too much overhead in code size or speed. The scheme requires about 10.5 bits on average to store the 6 numbers. As a result most functions which originally took 6 bytes to encode these 6 numbers now need only 1 byte (in 80% of cases).
Diffstat (limited to 'tools/mpy-tool.py')
-rwxr-xr-xtools/mpy-tool.py44
1 files changed, 32 insertions, 12 deletions
diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py
index 4f8e965d79..8671a2395d 100755
--- a/tools/mpy-tool.py
+++ b/tools/mpy-tool.py
@@ -152,13 +152,38 @@ def decode_uint(bytecode, ip):
break
return ip, unum
+def read_prelude_sig(read_byte):
+ z = read_byte()
+ # xSSSSEAA
+ S = (z >> 3) & 0xf
+ E = (z >> 2) & 0x1
+ F = 0
+ A = z & 0x3
+ K = 0
+ D = 0
+ n = 0
+ while z & 0x80:
+ z = read_byte()
+ # xFSSKAED
+ S |= (z & 0x30) << (2 * n)
+ E |= (z & 0x02) << n
+ F |= ((z & 0x40) >> 6) << n
+ A |= (z & 0x4) << n
+ K |= ((z & 0x08) >> 3) << n
+ D |= (z & 0x1) << n
+ n += 1
+ S += 1
+ return S, E, F, A, K, D
+
def extract_prelude(bytecode, ip):
- ip, n_state = decode_uint(bytecode, ip)
- ip, n_exc_stack = decode_uint(bytecode, ip)
- scope_flags = bytecode[ip]; ip += 1
- n_pos_args = bytecode[ip]; ip += 1
- n_kwonly_args = bytecode[ip]; ip += 1
- n_def_pos_args = bytecode[ip]; ip += 1
+ def local_read_byte():
+ b = bytecode[ip_ref[0]]
+ ip_ref[0] += 1
+ return b
+ ip_ref = [ip] # to close over ip in Python 2 and 3
+ n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args = read_prelude_sig(local_read_byte)
+ ip = ip_ref[0]
+
ip2, code_info_size = decode_uint(bytecode, ip)
ip += code_info_size
while bytecode[ip] != 0xff:
@@ -557,12 +582,7 @@ def read_obj(f):
assert 0
def read_prelude(f, bytecode):
- n_state = read_uint(f, bytecode)
- n_exc_stack = read_uint(f, bytecode)
- scope_flags = read_byte(f, bytecode)
- n_pos_args = read_byte(f, bytecode)
- n_kwonly_args = read_byte(f, bytecode)
- n_def_pos_args = read_byte(f, bytecode)
+ n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args = read_prelude_sig(lambda: read_byte(f, bytecode))
l1 = bytecode.idx
code_info_size = read_uint(f, bytecode)
l2 = bytecode.idx