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.
This commit is contained in:
Damien George 2017-11-16 14:02:28 +11:00
parent 1f1d5194d7
commit 8d956c26d1
1 changed files with 7 additions and 0 deletions

View File

@ -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;