Skip to content

Commit

Permalink
py/objstr: When constructing str from bytes, check for existing qstr.
Browse files Browse the repository at this point in the history
This patch uses existing qstr data where possible when constructing a str
from a bytes object.
  • Loading branch information
dpgeorge committed Nov 16, 2017
1 parent 1f1d519 commit 8d956c2
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions py/objstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8d956c2

Please sign in to comment.