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.
diff --git a/py/objstrunicode.c b/py/objstrunicode.c
index 49eddcc..348eaff 100644
--- a/py/objstrunicode.c
+++ b/py/objstrunicode.c
@@ -114,8 +114,6 @@
}
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 @@
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 @@
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;