Skip to content

Commit

Permalink
py/modbuiltins: chr: Intern only ASCII chars.
Browse files Browse the repository at this point in the history
Non-ASCII chars are too many and likely have lower probability of reuse,
but can asily take up a lot of non-recoverable qstr storage.

Change-Id: I20909f989bd53a650d5fa3cdb16717093738b3a4
  • Loading branch information
pfalcon committed Jan 25, 2019
1 parent 38818b0 commit 0c31164
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion py/modbuiltins.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
int len = 0;
if (c < 0x80) {
*str = c; len = 1;
// Intern only ASCII chars, which have good probability of being reused.
return mp_obj_new_str_via_qstr((char*)str, len);
} else if (c < 0x800) {
str[0] = (c >> 6) | 0xC0;
str[1] = (c & 0x3F) | 0x80;
Expand All @@ -159,7 +161,8 @@ STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
} else {
mp_raise_ValueError("chr() arg not in range(0x110000)");
}
return mp_obj_new_str_via_qstr((char*)str, len);
// Don't intern non-ASCII chars, as they may take a lot of qstr storage uselessly
return mp_obj_new_str((char*)str, len);
#else
mp_int_t ord = mp_obj_get_int(o_in);
if (0 <= ord && ord <= 0xff) {
Expand Down

0 comments on commit 0c31164

Please sign in to comment.