Skip to content

Commit bd5a3a3

Browse files
authored
Merge pull request #4583 from tyomitch/patch-2
[qstr] Separate hash and len from string data
2 parents a490376 + c3e40d5 commit bd5a3a3

File tree

6 files changed

+113
-98
lines changed

6 files changed

+113
-98
lines changed

py/makeqstrdata.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -456,27 +456,24 @@ def parse_input_headers(infiles):
456456

457457
return qcfgs, qstrs, i18ns
458458

459+
def escape_bytes(qstr):
460+
if all(32 <= ord(c) <= 126 and c != "\\" and c != '"' for c in qstr):
461+
# qstr is all printable ASCII so render it as-is (for easier debugging)
462+
return qstr
463+
else:
464+
# qstr contains non-printable codes so render entire thing as hex pairs
465+
qbytes = bytes_cons(qstr, "utf8")
466+
return "".join(("\\x%02x" % b) for b in qbytes)
459467

460468
def make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr):
461469
qbytes = bytes_cons(qstr, "utf8")
462470
qlen = len(qbytes)
463471
qhash = compute_hash(qbytes, cfg_bytes_hash)
464-
if all(32 <= ord(c) <= 126 and c != "\\" and c != '"' for c in qstr):
465-
# qstr is all printable ASCII so render it as-is (for easier debugging)
466-
qdata = qstr
467-
else:
468-
# qstr contains non-printable codes so render entire thing as hex pairs
469-
qdata = "".join(("\\x%02x" % b) for b in qbytes)
470472
if qlen >= (1 << (8 * cfg_bytes_len)):
471473
print("qstr is too long:", qstr)
472474
assert False
473-
qlen_str = ("\\x%02x" * cfg_bytes_len) % tuple(
474-
((qlen >> (8 * i)) & 0xFF) for i in range(cfg_bytes_len)
475-
)
476-
qhash_str = ("\\x%02x" * cfg_bytes_hash) % tuple(
477-
((qhash >> (8 * i)) & 0xFF) for i in range(cfg_bytes_hash)
478-
)
479-
return '(const byte*)"%s%s" "%s"' % (qhash_str, qlen_str, qdata)
475+
qdata = escape_bytes(qstr)
476+
return '%d, %d, "%s"' % (qhash, qlen, qdata)
480477

481478

482479
def print_qstr_data(encoding_table, qcfgs, qstrs, i18ns):
@@ -489,10 +486,7 @@ def print_qstr_data(encoding_table, qcfgs, qstrs, i18ns):
489486
print("")
490487

491488
# add NULL qstr with no hash or data
492-
print(
493-
'QDEF(MP_QSTR_NULL, (const byte*)"%s%s" "")'
494-
% ("\\x00" * cfg_bytes_hash, "\\x00" * cfg_bytes_len)
495-
)
489+
print('QDEF(MP_QSTR_NULL, 0, 0, "")')
496490

497491
total_qstr_size = 0
498492
total_qstr_compressed_size = 0

py/mpstate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ typedef struct _mp_state_vm_t {
197197

198198
// pointer and sizes to store interned string data
199199
// (qstr_last_chunk can be root pointer but is also stored in qstr pool)
200-
byte *qstr_last_chunk;
200+
char *qstr_last_chunk;
201201
size_t qstr_last_alloc;
202202
size_t qstr_last_used;
203203

py/qstr.c

Lines changed: 65 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
// NOTE: we are using linear arrays to store and search for qstr's (unique strings, interned strings)
3939
// ultimately we will replace this with a static hash table of some kind
40-
// also probably need to include the length in the string data, to allow null bytes in the string
4140

4241
#if MICROPY_DEBUG_VERBOSE // print debugging info
4342
#define DEBUG_printf DEBUG_printf
@@ -46,34 +45,9 @@
4645
#endif
4746

4847
// A qstr is an index into the qstr pool.
49-
// The data for a qstr contains (hash, length, data):
50-
// - hash (configurable number of bytes)
51-
// - length (configurable number of bytes)
52-
// - data ("length" number of bytes)
53-
// - \0 terminated (so they can be printed using printf)
54-
55-
#if MICROPY_QSTR_BYTES_IN_HASH == 1
56-
#define Q_HASH_MASK (0xff)
57-
#define Q_GET_HASH(q) ((mp_uint_t)(q)[0])
58-
#define Q_SET_HASH(q, hash) do { (q)[0] = (hash); } while (0)
59-
#elif MICROPY_QSTR_BYTES_IN_HASH == 2
60-
#define Q_HASH_MASK (0xffff)
61-
#define Q_GET_HASH(q) ((mp_uint_t)(q)[0] | ((mp_uint_t)(q)[1] << 8))
62-
#define Q_SET_HASH(q, hash) do { (q)[0] = (hash); (q)[1] = (hash) >> 8; } while (0)
63-
#else
64-
#error unimplemented qstr hash decoding
65-
#endif
66-
#define Q_GET_ALLOC(q) (MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + Q_GET_LENGTH(q) + 1)
67-
#define Q_GET_DATA(q) ((q) + MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN)
68-
#if MICROPY_QSTR_BYTES_IN_LEN == 1
69-
#define Q_GET_LENGTH(q) ((q)[MICROPY_QSTR_BYTES_IN_HASH])
70-
#define Q_SET_LENGTH(q, len) do { (q)[MICROPY_QSTR_BYTES_IN_HASH] = (len); } while (0)
71-
#elif MICROPY_QSTR_BYTES_IN_LEN == 2
72-
#define Q_GET_LENGTH(q) ((q)[MICROPY_QSTR_BYTES_IN_HASH] | ((q)[MICROPY_QSTR_BYTES_IN_HASH + 1] << 8))
73-
#define Q_SET_LENGTH(q, len) do { (q)[MICROPY_QSTR_BYTES_IN_HASH] = (len); (q)[MICROPY_QSTR_BYTES_IN_HASH + 1] = (len) >> 8; } while (0)
74-
#else
75-
#error unimplemented qstr length decoding
76-
#endif
48+
// The data for a qstr is \0 terminated (so they can be printed using printf)
49+
50+
#define Q_HASH_MASK ((1 << (8 * MICROPY_QSTR_BYTES_IN_HASH)) - 1)
7751

7852
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
7953
#define QSTR_ENTER() mp_thread_mutex_lock(&MP_STATE_VM(qstr_mutex), 1)
@@ -98,14 +72,25 @@ mp_uint_t qstr_compute_hash(const byte *data, size_t len) {
9872
return hash;
9973
}
10074

75+
const qstr_attr_t mp_qstr_const_attr[] = {
76+
#ifndef NO_QSTR
77+
#define QDEF(id, hash, len, str) { hash, len },
78+
#define TRANSLATION(id, length, compressed ...)
79+
#include "genhdr/qstrdefs.generated.h"
80+
#undef TRANSLATION
81+
#undef QDEF
82+
#endif
83+
};
84+
10185
const qstr_pool_t mp_qstr_const_pool = {
10286
NULL, // no previous pool
10387
0, // no previous pool
10488
10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
10589
MP_QSTRnumber_of, // corresponds to number of strings in array just below
90+
(qstr_attr_t *)mp_qstr_const_attr,
10691
{
10792
#ifndef NO_QSTR
108-
#define QDEF(id, str) str,
93+
#define QDEF(id, hash, len, str) str,
10994
#define TRANSLATION(id, length, compressed ...)
11095
#include "genhdr/qstrdefs.generated.h"
11196
#undef TRANSLATION
@@ -130,32 +115,37 @@ void qstr_init(void) {
130115
#endif
131116
}
132117

133-
STATIC const byte *find_qstr(qstr q) {
118+
STATIC const char *find_qstr(qstr q, qstr_attr_t *attr) {
134119
// search pool for this qstr
135120
// total_prev_len==0 in the final pool, so the loop will always terminate
136121
qstr_pool_t *pool = MP_STATE_VM(last_pool);
137122
while (q < pool->total_prev_len) {
138123
pool = pool->prev;
139124
}
140-
assert(q - pool->total_prev_len < pool->len);
141-
return pool->qstrs[q - pool->total_prev_len];
125+
q -= pool->total_prev_len;
126+
assert(q < pool->len);
127+
*attr = pool->attrs[q];
128+
return pool->qstrs[q];
142129
}
143130

144131
// qstr_mutex must be taken while in this function
145-
STATIC qstr qstr_add(const byte *q_ptr) {
146-
DEBUG_printf("QSTR: add hash=%d len=%d data=%.*s\n", Q_GET_HASH(q_ptr), Q_GET_LENGTH(q_ptr), Q_GET_LENGTH(q_ptr), Q_GET_DATA(q_ptr));
132+
STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) {
133+
DEBUG_printf("QSTR: add hash=%d len=%d data=%.*s\n", hash, len, len, q_ptr);
147134

148135
// make sure we have room in the pool for a new qstr
149136
if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) {
150137
uint32_t new_pool_length = MP_STATE_VM(last_pool)->alloc * 2;
151138
if (new_pool_length > MICROPY_QSTR_POOL_MAX_ENTRIES) {
152139
new_pool_length = MICROPY_QSTR_POOL_MAX_ENTRIES;
153140
}
154-
qstr_pool_t *pool = m_new_ll_obj_var_maybe(qstr_pool_t, const char *, new_pool_length);
155-
if (pool == NULL) {
141+
mp_uint_t pool_size = sizeof(qstr_pool_t) + sizeof(const char *) * new_pool_length;
142+
void *chunk = m_malloc_maybe(pool_size + sizeof(qstr_attr_t) * new_pool_length, true);
143+
if (chunk == NULL) {
156144
QSTR_EXIT();
157145
m_malloc_fail(new_pool_length);
158146
}
147+
qstr_pool_t *pool = (qstr_pool_t *)chunk;
148+
pool->attrs = (qstr_attr_t *)(void *)((char *)chunk + pool_size);
159149
pool->prev = MP_STATE_VM(last_pool);
160150
pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len;
161151
pool->alloc = new_pool_length;
@@ -165,10 +155,14 @@ STATIC qstr qstr_add(const byte *q_ptr) {
165155
}
166156

167157
// add the new qstr
168-
MP_STATE_VM(last_pool)->qstrs[MP_STATE_VM(last_pool)->len++] = q_ptr;
158+
mp_uint_t at = MP_STATE_VM(last_pool)->len;
159+
MP_STATE_VM(last_pool)->attrs[at].hash = hash;
160+
MP_STATE_VM(last_pool)->attrs[at].len = len;
161+
MP_STATE_VM(last_pool)->qstrs[at] = q_ptr;
162+
MP_STATE_VM(last_pool)->len++;
169163

170164
// return id for the newly-added qstr
171-
return MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len - 1;
165+
return MP_STATE_VM(last_pool)->total_prev_len + at;
172166
}
173167

174168
qstr qstr_find_strn(const char *str, size_t str_len) {
@@ -177,9 +171,10 @@ qstr qstr_find_strn(const char *str, size_t str_len) {
177171

178172
// search pools for the data
179173
for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
180-
for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
181-
if (Q_GET_HASH(*q) == str_hash && Q_GET_LENGTH(*q) == str_len && memcmp(Q_GET_DATA(*q), str, str_len) == 0) {
182-
return pool->total_prev_len + (q - pool->qstrs);
174+
qstr_attr_t *attrs = pool->attrs;
175+
for (mp_uint_t at = 0, top = pool->len; at < top; at++) {
176+
if (attrs[at].hash == str_hash && attrs[at].len == str_len && memcmp(pool->qstrs[at], str, str_len) == 0) {
177+
return pool->total_prev_len + at;
183178
}
184179
}
185180
}
@@ -200,14 +195,14 @@ qstr qstr_from_strn(const char *str, size_t len) {
200195
// qstr does not exist in interned pool so need to add it
201196

202197
// compute number of bytes needed to intern this string
203-
size_t n_bytes = MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + len + 1;
198+
size_t n_bytes = len + 1;
204199

205200
if (MP_STATE_VM(qstr_last_chunk) != NULL && MP_STATE_VM(qstr_last_used) + n_bytes > MP_STATE_VM(qstr_last_alloc)) {
206201
// not enough room at end of previously interned string so try to grow
207-
byte *new_p = m_renew_maybe(byte, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_alloc) + n_bytes, false);
202+
char *new_p = m_renew_maybe(char, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_alloc) + n_bytes, false);
208203
if (new_p == NULL) {
209204
// could not grow existing memory; shrink it to fit previous
210-
(void)m_renew_maybe(byte, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_used), false);
205+
(void)m_renew_maybe(char, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_used), false);
211206
MP_STATE_VM(qstr_last_chunk) = NULL;
212207
} else {
213208
// could grow existing memory
@@ -221,10 +216,10 @@ qstr qstr_from_strn(const char *str, size_t len) {
221216
if (al < MICROPY_ALLOC_QSTR_CHUNK_INIT) {
222217
al = MICROPY_ALLOC_QSTR_CHUNK_INIT;
223218
}
224-
MP_STATE_VM(qstr_last_chunk) = m_new_ll_maybe(byte, al);
219+
MP_STATE_VM(qstr_last_chunk) = m_new_ll_maybe(char, al);
225220
if (MP_STATE_VM(qstr_last_chunk) == NULL) {
226221
// failed to allocate a large chunk so try with exact size
227-
MP_STATE_VM(qstr_last_chunk) = m_new_ll_maybe(byte, n_bytes);
222+
MP_STATE_VM(qstr_last_chunk) = m_new_ll_maybe(char, n_bytes);
228223
if (MP_STATE_VM(qstr_last_chunk) == NULL) {
229224
QSTR_EXIT();
230225
m_malloc_fail(n_bytes);
@@ -236,39 +231,41 @@ qstr qstr_from_strn(const char *str, size_t len) {
236231
}
237232

238233
// allocate memory from the chunk for this new interned string's data
239-
byte *q_ptr = MP_STATE_VM(qstr_last_chunk) + MP_STATE_VM(qstr_last_used);
234+
char *q_ptr = MP_STATE_VM(qstr_last_chunk) + MP_STATE_VM(qstr_last_used);
240235
MP_STATE_VM(qstr_last_used) += n_bytes;
241236

242237
// store the interned strings' data
243238
mp_uint_t hash = qstr_compute_hash((const byte *)str, len);
244-
Q_SET_HASH(q_ptr, hash);
245-
Q_SET_LENGTH(q_ptr, len);
246-
memcpy(q_ptr + MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN, str, len);
247-
q_ptr[MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0';
248-
q = qstr_add(q_ptr);
239+
memcpy(q_ptr, str, len);
240+
q_ptr[len] = '\0';
241+
q = qstr_add(hash, len, q_ptr);
249242
}
250243
QSTR_EXIT();
251244
return q;
252245
}
253246

254247
mp_uint_t PLACE_IN_ITCM(qstr_hash)(qstr q) {
255-
return Q_GET_HASH(find_qstr(q));
248+
qstr_attr_t attr;
249+
find_qstr(q, &attr);
250+
return attr.hash;
256251
}
257252

258253
size_t qstr_len(qstr q) {
259-
const byte *qd = find_qstr(q);
260-
return Q_GET_LENGTH(qd);
254+
qstr_attr_t attr;
255+
find_qstr(q, &attr);
256+
return attr.len;
261257
}
262258

263259
const char *qstr_str(qstr q) {
264-
const byte *qd = find_qstr(q);
265-
return (const char *)Q_GET_DATA(qd);
260+
qstr_attr_t attr;
261+
return find_qstr(q, &attr);
266262
}
267263

268264
const byte *qstr_data(qstr q, size_t *len) {
269-
const byte *qd = find_qstr(q);
270-
*len = Q_GET_LENGTH(qd);
271-
return Q_GET_DATA(qd);
265+
qstr_attr_t attr;
266+
const char *qd = find_qstr(q, &attr);
267+
*len = attr.len;
268+
return (byte *)qd;
272269
}
273270

274271
void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes) {
@@ -280,13 +277,14 @@ void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, si
280277
for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
281278
*n_pool += 1;
282279
*n_qstr += pool->len;
283-
for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
284-
*n_str_data_bytes += Q_GET_ALLOC(*q);
280+
for (const qstr_attr_t *q = pool->attrs, *q_top = pool->attrs + pool->len; q < q_top; q++) {
281+
*n_str_data_bytes += sizeof(*q) + q->len + 1;
285282
}
286283
#if MICROPY_ENABLE_GC
287-
*n_total_bytes += gc_nbytes(pool); // this counts actual bytes used in heap
284+
// this counts actual bytes used in heap
285+
*n_total_bytes += gc_nbytes(pool) - sizeof(qstr_attr_t) * pool->alloc;
288286
#else
289-
*n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc;
287+
*n_total_bytes += sizeof(qstr_pool_t) + sizeof(const char *) * pool->alloc;
290288
#endif
291289
}
292290
*n_total_bytes += *n_str_data_bytes;
@@ -297,8 +295,8 @@ void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, si
297295
void qstr_dump_data(void) {
298296
QSTR_ENTER();
299297
for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
300-
for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
301-
mp_printf(&mp_plat_print, "Q(%s)\n", Q_GET_DATA(*q));
298+
for (const char **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
299+
mp_printf(&mp_plat_print, "Q(%s)\n", *q);
302300
}
303301
}
304302
QSTR_EXIT();

py/qstr.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,30 @@ enum {
4747

4848
typedef size_t qstr;
4949

50+
typedef struct _qstr_attr_t {
51+
#if MICROPY_QSTR_BYTES_IN_HASH == 1
52+
uint8_t hash;
53+
#elif MICROPY_QSTR_BYTES_IN_HASH == 2
54+
uint16_t hash;
55+
#else
56+
#error unimplemented qstr hash decoding
57+
#endif
58+
#if MICROPY_QSTR_BYTES_IN_LEN == 1
59+
uint8_t len;
60+
#elif MICROPY_QSTR_BYTES_IN_LEN == 2
61+
uint16_t len;
62+
#else
63+
#error unimplemented qstr length decoding
64+
#endif
65+
} qstr_attr_t;
66+
5067
typedef struct _qstr_pool_t {
5168
struct _qstr_pool_t *prev;
5269
size_t total_prev_len;
5370
size_t alloc;
5471
size_t len;
55-
const byte *qstrs[];
72+
qstr_attr_t *attrs;
73+
const char *qstrs[];
5674
} qstr_pool_t;
5775

5876
#define QSTR_FROM_STR_STATIC(s) (qstr_from_strn((s), strlen(s)))

supervisor/shared/translate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ __attribute__((always_inline))
129129
#endif
130130
const compressed_string_t *translate(const char *original) {
131131
#ifndef NO_QSTR
132-
#define QDEF(id, str)
132+
#define QDEF(id, hash, len, str)
133133
#define TRANSLATION(id, firstbyte, ...) if (strcmp(original, id) == 0) { static const compressed_string_t v = { .data = firstbyte, .tail = { __VA_ARGS__ } }; return &v; } else
134134
#include "genhdr/qstrdefs.generated.h"
135135
#undef TRANSLATION

0 commit comments

Comments
 (0)