From 8d956c26d150749375115346f4ca319455107587 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 16 Nov 2017 14:02:28 +1100 Subject: [PATCH] py/objstr: When constructing str from bytes, check for existing qstr. This patch uses existing qstr data where possible when constructing a str from a bytes object. --- py/objstr.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/py/objstr.c b/py/objstr.c index bca2af801b..1ff5132d29 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -164,6 +164,13 @@ mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_ mp_raise_msg(&mp_type_UnicodeError, NULL); } #endif + + // Check if a qstr with this data already exists + qstr q = qstr_find_strn((const char*)str_data, str_len); + if (q != MP_QSTR_NULL) { + return MP_OBJ_NEW_QSTR(q); + } + mp_obj_str_t *o = MP_OBJ_TO_PTR(mp_obj_new_str_copy(type, NULL, str_len)); o->data = str_data; o->hash = str_hash;