From 05005f679e00241e15a87751d89327f2c4630cb6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 21 Jan 2015 22:48:37 +0000 Subject: py: Remove mp_obj_str_builder and use vstr instead. With this patch str/bytes construction is streamlined. Always use a vstr to build a str/bytes object. If the size is known beforehand then use vstr_init_len to allocate only required memory. Otherwise use vstr_init and the vstr will grow as needed. Then use mp_obj_new_str_from_vstr to create a str/bytes object using the vstr memory. Saves code ROM: 68 bytes on stmhal, 108 bytes on bare-arm, and 336 bytes on unix x64. --- py/modstruct.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'py/modstruct.c') diff --git a/py/modstruct.c b/py/modstruct.c index 114135dfd1..681c585779 100644 --- a/py/modstruct.c +++ b/py/modstruct.c @@ -161,8 +161,9 @@ STATIC mp_obj_t struct_pack(mp_uint_t n_args, const mp_obj_t *args) { const char *fmt = mp_obj_str_get_str(args[0]); char fmt_type = get_fmt_type(&fmt); mp_int_t size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0])); - byte *p; - mp_obj_t res = mp_obj_str_builder_start(&mp_type_bytes, size, &p); + vstr_t vstr; + vstr_init_len(&vstr, size); + byte *p = (byte*)vstr.buf; memset(p, 0, size); for (mp_uint_t i = 1; i < n_args; i++) { @@ -190,7 +191,8 @@ STATIC mp_obj_t struct_pack(mp_uint_t n_args, const mp_obj_t *args) { mp_binary_set_val(fmt_type, *fmt++, args[i], &p); } } - return res; + + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, MP_OBJ_FUN_ARGS_MAX, struct_pack); -- cgit v1.2.3