summaryrefslogtreecommitdiffstatshomepage
path: root/py/modstruct.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-01-21 22:48:37 +0000
committerDamien George <damien.p.george@gmail.com>2015-01-21 23:18:02 +0000
commit05005f679e00241e15a87751d89327f2c4630cb6 (patch)
tree50319ab1dee5af8016e95e3c80a3b28346cdca21 /py/modstruct.c
parent0b9ee86133a2a0524691c6cdac209dbfcb3bf116 (diff)
downloadmicropython-05005f679e00241e15a87751d89327f2c4630cb6.tar.gz
micropython-05005f679e00241e15a87751d89327f2c4630cb6.zip
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.
Diffstat (limited to 'py/modstruct.c')
-rw-r--r--py/modstruct.c8
1 files changed, 5 insertions, 3 deletions
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);