Skip to content

Commit 487dbdb

Browse files
committed
py/compile: Use alloca instead of qstr_build when compiling import name.
The technique of using alloca is how dotted import names are composed in mp_import_from and mp_builtin___import__, so use the same technique in the compiler. This puts less pressure on the heap (only the stack is used if the qstr already exists, and if it doesn't exist then the standard qstr block memory is used for the new qstr rather than a separate chunk of the heap) and reduces overall code size.
1 parent fe45d78 commit 487dbdb

3 files changed

Lines changed: 3 additions & 29 deletions

File tree

py/compile.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,8 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
10501050
for (int i = 0; i < n; i++) {
10511051
len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
10521052
}
1053-
byte *q_ptr;
1054-
byte *str_dest = qstr_build_start(len, &q_ptr);
1053+
char *q_ptr = alloca(len);
1054+
char *str_dest = q_ptr;
10551055
for (int i = 0; i < n; i++) {
10561056
if (i > 0) {
10571057
*str_dest++ = '.';
@@ -1061,7 +1061,7 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
10611061
memcpy(str_dest, str_src, str_src_len);
10621062
str_dest += str_src_len;
10631063
}
1064-
qstr q_full = qstr_build_end(q_ptr);
1064+
qstr q_full = qstr_from_strn(q_ptr, len);
10651065
EMIT_ARG(import_name, q_full);
10661066
if (is_as) {
10671067
for (int i = 1; i < n; i++) {

py/qstr.c

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -243,29 +243,6 @@ qstr qstr_from_strn(const char *str, size_t len) {
243243
return q;
244244
}
245245

246-
byte *qstr_build_start(size_t len, byte **q_ptr) {
247-
assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN)));
248-
*q_ptr = m_new(byte, MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + len + 1);
249-
Q_SET_LENGTH(*q_ptr, len);
250-
return Q_GET_DATA(*q_ptr);
251-
}
252-
253-
qstr qstr_build_end(byte *q_ptr) {
254-
QSTR_ENTER();
255-
qstr q = qstr_find_strn((const char*)Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr));
256-
if (q == 0) {
257-
size_t len = Q_GET_LENGTH(q_ptr);
258-
mp_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len);
259-
Q_SET_HASH(q_ptr, hash);
260-
q_ptr[MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0';
261-
q = qstr_add(q_ptr);
262-
} else {
263-
m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr));
264-
}
265-
QSTR_EXIT();
266-
return q;
267-
}
268-
269246
mp_uint_t qstr_hash(qstr q) {
270247
return Q_GET_HASH(find_qstr(q));
271248
}

py/qstr.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ qstr qstr_find_strn(const char *str, size_t str_len); // returns MP_QSTR_NULL if
6565
qstr qstr_from_str(const char *str);
6666
qstr qstr_from_strn(const char *str, size_t len);
6767

68-
byte *qstr_build_start(size_t len, byte **q_ptr);
69-
qstr qstr_build_end(byte *q_ptr);
70-
7168
mp_uint_t qstr_hash(qstr q);
7269
const char *qstr_str(qstr q);
7370
size_t qstr_len(qstr q);

0 commit comments

Comments
 (0)