Skip to content

Commit 95836f8

Browse files
committed
py: Add MICROPY_QSTR_BYTES_IN_LEN config option, defaulting to 1.
This new config option sets how many fixed-number-of-bytes to use to store the length of each qstr. Previously this was hard coded to 2, but, as per issue adafruit#1056, this is considered overkill since no-one needs identifiers longer than 255 bytes. With this patch the number of bytes for the length is configurable, and defaults to 1 byte. The configuration option filters through to the makeqstrdata.py script. Code size savings going from 2 to 1 byte: - unix x64 down by 592 bytes - stmhal down by 1148 bytes - bare-arm down by 284 bytes Also has RAM savings, and will be slightly more efficient in execution.
1 parent 6942f80 commit 95836f8

4 files changed

Lines changed: 41 additions & 17 deletions

File tree

py/makeqstrdata.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,27 @@ def do_work(infiles):
7373
# add the qstr to the list, with order number to retain original order in file
7474
qstrs[ident] = (len(qstrs), ident, qstr)
7575

76-
# process the qstrs, printing out the generated C header file
76+
# get config variables
77+
cfg_bytes_len = int(qcfgs['BYTES_IN_LEN'])
78+
cfg_max_len = 1 << (8 * cfg_bytes_len)
79+
80+
# print out the starte of the generated C header file
7781
print('// This file was automatically generated by makeqstrdata.py')
7882
print('')
83+
7984
# add NULL qstr with no hash or data
80-
print('QDEF(MP_QSTR_NULL, (const byte*)"\\x00\\x00\\x00\\x00" "")')
85+
print('QDEF(MP_QSTR_NULL, (const byte*)"\\x00\\x00%s" "")' % ('\\x00' * cfg_bytes_len))
86+
87+
# go through each qstr and print it out
8188
for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
8289
qhash = compute_hash(qstr)
8390
qlen = len(qstr)
8491
qdata = qstr.replace('"', '\\"')
85-
print('QDEF(MP_QSTR_%s, (const byte*)"\\x%02x\\x%02x\\x%02x\\x%02x" "%s")' % (ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qdata))
92+
if qlen >= cfg_max_len:
93+
print('qstr is too long:', qstr)
94+
assert False
95+
qlen_str = ('\\x%02x' * cfg_bytes_len) % tuple(qlen.to_bytes(cfg_bytes_len, 'little'))
96+
print('QDEF(MP_QSTR_%s, (const byte*)"\\x%02x\\x%02x%s" "%s")' % (ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen_str, qdata))
8697

8798
return True
8899

py/mpconfig.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@
112112
#define MICROPY_MODULE_DICT_SIZE (1)
113113
#endif
114114

115+
// Number of bytes used to store qstr length
116+
// Dictates hard limit on maximum Python identifier length, but 1 byte
117+
// (limit of 255 bytes in an identifier) should be enough for everyone
118+
#ifndef MICROPY_QSTR_BYTES_IN_LEN
119+
#define MICROPY_QSTR_BYTES_IN_LEN (1)
120+
#endif
121+
115122
/*****************************************************************************/
116123
/* Micro Python emitters */
117124

py/qstr.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,17 @@
5050
// - \0 terminated (for now, so they can be printed using printf)
5151

5252
#define Q_GET_HASH(q) ((q)[0] | ((q)[1] << 8))
53-
#define Q_GET_ALLOC(q) (4 + Q_GET_LENGTH(q) + 1)
54-
#define Q_GET_LENGTH(q) ((q)[2] | ((q)[3] << 8))
55-
#define Q_GET_DATA(q) ((q) + 4)
53+
#define Q_GET_ALLOC(q) (2 + MICROPY_QSTR_BYTES_IN_LEN + Q_GET_LENGTH(q) + 1)
54+
#define Q_GET_DATA(q) ((q) + 2 + MICROPY_QSTR_BYTES_IN_LEN)
55+
#if MICROPY_QSTR_BYTES_IN_LEN == 1
56+
#define Q_GET_LENGTH(q) ((q)[2])
57+
#define Q_SET_LENGTH(q, len) do { (q)[2] = (len); } while (0)
58+
#elif MICROPY_QSTR_BYTES_IN_LEN == 2
59+
#define Q_GET_LENGTH(q) ((q)[2] | ((q)[3] << 8))
60+
#define Q_SET_LENGTH(q, len) do { (q)[2] = (len); (q)[3] = (len) >> 8; } while (0)
61+
#else
62+
#error unimplemented qstr length decoding
63+
#endif
5664

5765
// this must match the equivalent function in makeqstrdata.py
5866
mp_uint_t qstr_compute_hash(const byte *data, mp_uint_t len) {
@@ -143,23 +151,21 @@ qstr qstr_from_strn(const char *str, mp_uint_t len) {
143151
qstr q = qstr_find_strn(str, len);
144152
if (q == 0) {
145153
mp_uint_t hash = qstr_compute_hash((const byte*)str, len);
146-
byte *q_ptr = m_new(byte, 4 + len + 1);
154+
byte *q_ptr = m_new(byte, 2 + MICROPY_QSTR_BYTES_IN_LEN + len + 1);
147155
q_ptr[0] = hash;
148156
q_ptr[1] = hash >> 8;
149-
q_ptr[2] = len;
150-
q_ptr[3] = len >> 8;
151-
memcpy(q_ptr + 4, str, len);
152-
q_ptr[4 + len] = '\0';
157+
Q_SET_LENGTH(q_ptr, len);
158+
memcpy(q_ptr + 2 + MICROPY_QSTR_BYTES_IN_LEN, str, len);
159+
q_ptr[2 + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0';
153160
q = qstr_add(q_ptr);
154161
}
155162
return q;
156163
}
157164

158165
byte *qstr_build_start(mp_uint_t len, byte **q_ptr) {
159-
assert(len <= 65535);
160-
*q_ptr = m_new(byte, 4 + len + 1);
161-
(*q_ptr)[2] = len;
162-
(*q_ptr)[3] = len >> 8;
166+
assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN)));
167+
*q_ptr = m_new(byte, 2 + MICROPY_QSTR_BYTES_IN_LEN + len + 1);
168+
Q_SET_LENGTH(*q_ptr, len);
163169
return Q_GET_DATA(*q_ptr);
164170
}
165171

@@ -170,7 +176,7 @@ qstr qstr_build_end(byte *q_ptr) {
170176
mp_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len);
171177
q_ptr[0] = hash;
172178
q_ptr[1] = hash >> 8;
173-
q_ptr[4 + len] = '\0';
179+
q_ptr[2 + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0';
174180
q = qstr_add(q_ptr);
175181
} else {
176182
m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr));

py/qstrdefs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// That is, they are in ROM and you can reference them simply as MP_QSTR_xxxx.
3131

3232
// qstr configuration passed to makeqstrdata.py of the form QCFG(key, value)
33-
//QCFG(somekey, somevalue)
33+
QCFG(BYTES_IN_LEN, MICROPY_QSTR_BYTES_IN_LEN)
3434

3535
Q()
3636
Q(*)

0 commit comments

Comments
 (0)