diff options
author | Damien George <damien.p.george@gmail.com> | 2017-11-16 13:53:04 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-11-16 13:53:04 +1100 |
commit | 1f1d5194d775ad996f1d341c1a44b56af7ea4d4c (patch) | |
tree | 1ec3138b14dfbd7a759bd065991b346a112fe1cb /py/parse.c | |
parent | 4601759bf59e16b860a3f082e9aa4ea78356bf92 (diff) |
py/objstr: Make mp_obj_new_str_of_type check for existing interned qstr.
The function mp_obj_new_str_of_type is a general str object constructor
used in many places in the code to create either a str or bytes object.
When creating a str it should first check if the string data already exists
as an interned qstr, and if so then return the qstr object. This patch
makes the function have such behaviour, which helps to reduce heap usage by
reusing existing interned data where possible.
The old behaviour of mp_obj_new_str_of_type (which didn't check for
existing interned data) is made available through the function
mp_obj_new_str_copy, but should only be used in very special cases.
One consequence of this patch is that the following expression is now True:
'abc' is ' abc '.split()[0]
Diffstat (limited to 'py/parse.c')
-rw-r--r-- | py/parse.c | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/py/parse.c b/py/parse.c index 8c51b0349..f7fe30418 100644 --- a/py/parse.c +++ b/py/parse.c @@ -417,7 +417,7 @@ STATIC void push_result_token(parser_t *parser, const rule_t *rule) { pn = mp_parse_node_new_leaf(lex->tok_kind == MP_TOKEN_STRING ? MP_PARSE_NODE_STRING : MP_PARSE_NODE_BYTES, qst); } else { // not interned, make a node holding a pointer to the string/bytes object - mp_obj_t o = mp_obj_new_str_of_type( + mp_obj_t o = mp_obj_new_str_copy( lex->tok_kind == MP_TOKEN_STRING ? &mp_type_str : &mp_type_bytes, (const byte*)lex->vstr.buf, lex->vstr.len); pn = make_node_const_object(parser, lex->tok_line, o); |