8000 py: Free non-interned strings in the parser when not needed. · comfuture/micropython@52b5d76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 52b5d76

Browse files
committed
py: Free non-interned strings in the parser when not needed.
mp_parse_node_free now frees the memory associated with non-interned strings. And the parser calls mp_parse_node_free when discarding a non-used node (such as a doc string). Also, the compiler now frees the parse tree explicitly just before it exits (as opposed to relying on the caller to do this). Addresses issue micropython#708 as best we can.
1 parent d6230f6 commit 52b5d76

File tree

9 files changed

+7
-8
lines changed

9 files changed

+7
-8
lines changed

bare-arm/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ void do_str(const char *src) {
3535
qstr source_name = mp_lexer_source_name(lex);
3636
mp_lexer_free(lex);
3737
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
38-
mp_parse_node_free(pn);
3938

4039
if (module_fun == mp_const_none) {
4140
// compile error

py/builtinevex.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ STATIC mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse
6363

6464
// compile the string
6565
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
66-
mp_parse_node_free(pn);
6766

6867
if (module_fun == mp_const_none) {
6968
// TODO handle compile error correctly

py/builtinimport.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
141141

142142
// compile the imported script
143143
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
144-
mp_parse_node_free(pn);
145144

146145
if (module_fun == mp_const_none) {
147146
// TODO handle compile error correctly

py/compile.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3710,6 +3710,9 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
37103710
#endif
37113711
#endif // !MICROPY_EMIT_CPYTHON
37123712

3713+
// free the parse tree
3714+
mp_parse_node_free(pn);
3715+
37133716
// free the scopes
37143717
mp_raw_code_t *outer_raw_code = module_scope->raw_code;
37153718
for (scope_t *s = module_scope; s;) {

py/compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ enum {
3333
MP_EMIT_OPT_ASM_THUMB,
3434
};
3535

36+
// the compiler will free the parse tree (pn) before it returns
3637
mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl);

py/parse.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ void mp_parse_node_free(mp_parse_node_t pn) {
179179
mp_uint_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
180180
mp_uint_t rule_id = MP_PARSE_NODE_STRUCT_KIND(pns);
181181
if (rule_id == RULE_string) {
182+
m_del(char, (char*)pns->nodes[0], (mp_uint_t)pns->nodes[1]);
182183
return;
183184
}
184185
bool adjust = ADD_BLANK_NODE(rule_id);
@@ -562,8 +563,8 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
562563
if (input_kind != MP_PARSE_SINGLE_INPUT && rule->rule_id == RULE_expr_stmt && peek_result(&parser, 0) == MP_PARSE_NODE_NULL) {
563564
mp_parse_node_t p = peek_result(&parser, 1);
564565
if ((MP_PARSE_NODE_IS_LEAF(p) && !MP_PARSE_NODE_IS_ID(p)) || MP_PARSE_NODE_IS_STRUCT_KIND(p, RULE_string)) {
565-
pop_result(&parser);
566-
pop_result(&parser);
566+
pop_result(&parser); // MP_PARSE_NODE_NULL
567+
mp_parse_node_free(pop_result(&parser)); // RULE_string
567568
push_result_rule(&parser, rule_src_line, rules[RULE_pass_stmt], 0);
568569
break;
569570
}

qemu-arm/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ void do_str(const char *src) {
3535
qstr source_name = mp_lexer_source_name(lex);
3636
mp_lexer_free(lex);
3737
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
38-
mp_parse_node_free(pn);
3938

4039
if (module_fun == mp_const_none) {
4140
// compile error

qemu-arm/test_main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ inline void do_str(const char *src) {
3838
qstr source_name = mp_lexer_source_name(lex);
3939
mp_lexer_free(lex);
4040
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
41-
mp_parse_node_free(pn);
4241

4342
if (module_fun == mp_const_none) {
4443
tt_abort_msg("Computer error");

stmhal/pyexec.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ bool parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bo
7171
mp_lexer_free(lex);
7272

7373
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, is_repl);
74-
mp_parse_node_free(pn);
7574

7675
if (module_fun == mp_const_none) {
7776
return false;

0 commit comments

Comments
 (0)
0