diff options
author | Damien George <damien.p.george@gmail.com> | 2015-01-21 19:14:25 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-01-21 23:17:27 +0000 |
commit | 0b9ee86133a2a0524691c6cdac209dbfcb3bf116 (patch) | |
tree | e17f1f8c26b2d92991a8481ba5d2fc0649a8c6ce /py/objstrunicode.c | |
parent | 2e526ff1a15cfe50aa46fe5611d3160b2a854f49 (diff) |
py: Add mp_obj_new_str_from_vstr, and use it where relevant.
This patch allows to reuse vstr memory when creating str/bytes object.
This improves memory usage.
Also saves code ROM: 128 bytes on stmhal, 92 bytes on bare-arm, and 88
bytes on unix x64.
Diffstat (limited to 'py/objstrunicode.c')
-rw-r--r-- | py/objstrunicode.c | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/py/objstrunicode.c b/py/objstrunicode.c index 49eddcca3..348eaff39 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -114,8 +114,6 @@ STATIC mp_obj_t uni_unary_op(mp_uint_t op, mp_obj_t self_in) { } STATIC mp_obj_t str_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { - (void)type_in; - #if MICROPY_CPYTHON_COMPAT if (n_kw != 0) { mp_arg_error_unimpl_kw(); @@ -126,13 +124,11 @@ STATIC mp_obj_t str_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, case 0: return MP_OBJ_NEW_QSTR(MP_QSTR_); - case 1: - { - vstr_t *vstr = vstr_new(); - mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[0], PRINT_STR); - mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false); - vstr_free(vstr); - return s; + case 1: { + vstr_t vstr; + vstr_init(&vstr, 16); + mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, &vstr, args[0], PRINT_STR); + return mp_obj_new_str_from_vstr(type_in, &vstr); } case 2: @@ -142,7 +138,7 @@ STATIC mp_obj_t str_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, if (MP_OBJ_IS_TYPE(args[0], &mp_type_bytes)) { GET_STR_DATA_LEN(args[0], str_data, str_len); GET_STR_HASH(args[0], str_hash); - mp_obj_str_t *o = mp_obj_new_str_of_type(&mp_type_str, NULL, str_len); + mp_obj_str_t *o = mp_obj_new_str_of_type(type_in, NULL, str_len); o->data = str_data; o->hash = str_hash; return o; |