Skip to content

Commit 65ef6b7

Browse files
committed
py: Only allocate strings/bytes once for load_const_obj.
1 parent d95b519 commit 65ef6b7

1 file changed

Lines changed: 36 additions & 13 deletions

File tree

py/compile.c

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,6 +2618,12 @@ STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
26182618
}
26192619
}
26202620

2621+
// if we are not in the last pass, just load a dummy object
2622+
if (comp->pass != MP_PASS_EMIT) {
2623+
EMIT_ARG(load_const_obj, mp_const_none);
2624+
return;
2625+
}
2626+
26212627
// concatenate string/bytes
26222628
byte *s_dest;
26232629
mp_obj_t obj = mp_obj_str_builder_start(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, n_bytes, &s_dest);
@@ -2633,6 +2639,8 @@ STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
26332639
s_dest += (mp_uint_t)pns_string->nodes[1];
26342640
}
26352641
}
2642+
2643+
// load the object
26362644
EMIT_ARG(load_const_obj, mp_obj_str_builder_end(obj));
26372645
}
26382646

@@ -2928,6 +2936,24 @@ STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
29282936
}
29292937
}
29302938

2939+
STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2940+
// only create and load the actual str object on the last pass
2941+
if (comp->pass != MP_PASS_EMIT) {
2942+
EMIT_ARG(load_const_obj, mp_const_none);
2943+
} else {
2944+
EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2945+
}
2946+
}
2947+
2948+
STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2949+
// only create and load the actual bytes object on the last pass
2950+
if (comp->pass != MP_PASS_EMIT) {
2951+
EMIT_ARG(load_const_obj, mp_const_none);
2952+
} else {
2953+
EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2954+
}
2955+
}
2956+
29312957
typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
29322958
STATIC compile_function_t compile_function[] = {
29332959
#define nc NULL
@@ -2937,6 +2963,9 @@ STATIC compile_function_t compile_function[] = {
29372963
#undef nc
29382964
#undef c
29392965
#undef DEF_RULE
2966+
NULL,
2967+
compile_string,
2968+
compile_bytes,
29402969
};
29412970

29422971
STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
@@ -2967,21 +2996,15 @@ STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
29672996
} else {
29682997
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
29692998
EMIT_ARG(set_line_number, pns->source_line);
2970-
if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
2971-
EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2972-
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_bytes) {
2973-
EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2974-
} else {
2975-
compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2976-
if (f == NULL) {
2999+
compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
3000+
if (f == NULL) {
29773001
#if MICROPY_DEBUG_PRINTERS
2978-
printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2979-
mp_parse_node_print(pn, 0);
3002+
printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
3003+
mp_parse_node_print(pn, 0);
29803004
#endif
2981-
compile_syntax_error(comp, pn, "internal compiler error");
2982-
} else {
2983-
f(comp, pns);
2984-
}
3005+
compile_syntax_error(comp, pn, "internal compiler error");
3006+
} else {
3007+
f(comp, pns);
29853008
}
29863009
}
29873010
}

0 commit comments

Comments
 (0)