From 05005f679e00241e15a87751d89327f2c4630cb6 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Wed, 21 Jan 2015 22:48:37 +0000
Subject: [PATCH] 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.
---
 extmod/modubinascii.c |  7 ++--
 extmod/moduhashlib.c  |  8 ++---
 py/compile.c          |  7 ++--
 py/misc.h             |  1 +
 py/modstruct.c        |  8 +++--
 py/obj.h              |  3 --
 py/objint.c           |  7 ++--
 py/objstr.c           | 79 ++++++++++++++-----------------------------
 py/stream.c           | 19 ++++++-----
 py/vstr.c             |  5 +++
 stmhal/bufhelper.c    | 14 ++++----
 stmhal/bufhelper.h    |  2 +-
 stmhal/can.c          |  8 ++---
 stmhal/i2c.c          | 26 +++++++-------
 stmhal/moduos.c       |  8 ++---
 stmhal/modusocket.c   | 29 ++++++++--------
 stmhal/spi.c          | 32 ++++++++++--------
 stmhal/usb.c          | 10 +++---
 18 files changed, 130 insertions(+), 143 deletions(-)

diff --git a/extmod/modubinascii.c b/extmod/modubinascii.c
index de8b5d9ae..e258818af 100644
--- a/extmod/modubinascii.c
+++ b/extmod/modubinascii.c
@@ -39,8 +39,9 @@ STATIC mp_obj_t mod_binascii_hexlify(mp_uint_t n_args, const mp_obj_t *args) {
     mp_buffer_info_t bufinfo;
     mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
 
-    byte *in = bufinfo.buf, *out;
-    mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, bufinfo.len * 2, &out);
+    vstr_t vstr;
+    vstr_init_len(&vstr, bufinfo.len * 2);
+    byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
     for (mp_uint_t i = bufinfo.len; i--;) {
         byte d = (*in >> 4);
         if (d > 9) {
@@ -53,7 +54,7 @@ STATIC mp_obj_t mod_binascii_hexlify(mp_uint_t n_args, const mp_obj_t *args) {
         }
         *out++ = d + '0';
     }
-    return mp_obj_str_builder_end(o);
+    return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
 }
 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj, 1, 2, mod_binascii_hexlify);
 
diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c
index 40a0f0609..e9100fa1b 100644
--- a/extmod/moduhashlib.c
+++ b/extmod/moduhashlib.c
@@ -63,10 +63,10 @@ MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update);
 
 STATIC mp_obj_t hash_digest(mp_obj_t self_in) {
     mp_obj_hash_t *self = self_in;
-    byte *hash;
-    mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, SHA256_BLOCK_SIZE, &hash);
-    sha256_final((SHA256_CTX*)self->state, hash);
-    return mp_obj_str_builder_end(o);
+    vstr_t vstr;
+    vstr_init_len(&vstr, SHA256_BLOCK_SIZE);
+    sha256_final((SHA256_CTX*)self->state, (byte*)vstr.buf);
+    return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
 }
 MP_DEFINE_CONST_FUN_OBJ_1(hash_digest_obj, hash_digest);
 
diff --git a/py/compile.c b/py/compile.c
index ea0782e3c..85f5dab83 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -2624,8 +2624,9 @@ STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
     }
 
     // concatenate string/bytes
-    byte *s_dest;
-    mp_obj_t obj = mp_obj_str_builder_start(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, n_bytes, &s_dest);
+    vstr_t vstr;
+    vstr_init_len(&vstr, n_bytes);
+    byte *s_dest = (byte*)vstr.buf;
     for (int i = 0; i < n; i++) {
         if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
             mp_uint_t s_len;
@@ -2640,7 +2641,7 @@ STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
     }
 
     // load the object
-    EMIT_ARG(load_const_obj, mp_obj_str_builder_end(obj));
+    EMIT_ARG(load_const_obj, mp_obj_new_str_from_vstr(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, &vstr));
 }
 
 // pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
diff --git a/py/misc.h b/py/misc.h
index df017f44a..b800efa02 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -124,6 +124,7 @@ typedef struct _vstr_t {
 #define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf);
 
 void vstr_init(vstr_t *vstr, size_t alloc);
+void vstr_init_len(vstr_t *vstr, size_t len);
 void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf);
 void vstr_clear(vstr_t *vstr);
 vstr_t *vstr_new(void);
diff --git a/py/modstruct.c b/py/modstruct.c
index 114135dfd..681c58577 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);
 
diff --git a/py/obj.h b/py/obj.h
index d0ea4c112..2d506858d 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -500,9 +500,6 @@ mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);
 void mp_init_emergency_exception_buf(void);
 
 // str
-mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, mp_uint_t len, byte **data);
-mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in);
-mp_obj_t mp_obj_str_builder_end_with_len(mp_obj_t o_in, mp_uint_t len);
 bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
 mp_uint_t mp_obj_str_get_hash(mp_obj_t self_in);
 mp_uint_t mp_obj_str_get_len(mp_obj_t self_in);
diff --git a/py/objint.c b/py/objint.c
index 049d0410e..e8d86ad7e 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -340,8 +340,9 @@ STATIC mp_obj_t int_to_bytes(mp_uint_t n_args, const mp_obj_t *args) {
     mp_int_t val = mp_obj_int_get_checked(args[0]);
     mp_uint_t len = MP_OBJ_SMALL_INT_VALUE(args[1]);
 
-    byte *data;
-    mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, len, &data);
+    vstr_t vstr;
+    vstr_init_len(&vstr, len);
+    byte *data = (byte*)vstr.buf;
     memset(data, 0, len);
 
     if (MP_ENDIANNESS_LITTLE) {
@@ -353,7 +354,7 @@ STATIC mp_obj_t int_to_bytes(mp_uint_t n_args, const mp_obj_t *args) {
         }
     }
 
-    return mp_obj_str_builder_end(o);
+    return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 2, 4, int_to_bytes);
 
diff --git a/py/objstr.c b/py/objstr.c
index b67913e51..f25ec1737 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -200,11 +200,10 @@ STATIC mp_obj_t bytes_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
 
     if (MP_OBJ_IS_SMALL_INT(args[0])) {
         uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
-        byte *data;
-
-        mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, len, &data);
-        memset(data, 0, len);
-        return mp_obj_str_builder_end(o);
+        vstr_t vstr;
+        vstr_init_len(&vstr, len);
+        memset(vstr.buf, 0, len);
+        return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
     }
 
     // check if argument has the buffer protocol
@@ -302,10 +301,10 @@ mp_obj_t mp_obj_str_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
                 return mp_const_empty_bytes;
             }
         }
-        byte *data;
-        mp_obj_t s = mp_obj_str_builder_start(lhs_type, lhs_len * n, &data);
-        mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, data);
-        return mp_obj_str_builder_end(s);
+        vstr_t vstr;
+        vstr_init_len(&vstr, lhs_len * n);
+        mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, vstr.buf);
+        return mp_obj_new_str_from_vstr(lhs_type, &vstr);
     }
 
     // From now on all operations allow:
@@ -344,12 +343,11 @@ mp_obj_t mp_obj_str_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
     switch (op) {
         case MP_BINARY_OP_ADD:
         case MP_BINARY_OP_INPLACE_ADD: {
-            mp_uint_t alloc_len = lhs_len + rhs_len;
-            byte *data;
-            mp_obj_t s = mp_obj_str_builder_start(lhs_type, alloc_len, &data);
-            memcpy(data, lhs_data, lhs_len);
-            memcpy(data + lhs_len, rhs_data, rhs_len);
-            return mp_obj_str_builder_end(s);
+            vstr_t vstr;
+            vstr_init_len(&vstr, lhs_len + rhs_len);
+            memcpy(vstr.buf, lhs_data, lhs_len);
+            memcpy(vstr.buf + lhs_len, rhs_data, rhs_len);
+            return mp_obj_new_str_from_vstr(lhs_type, &vstr);
         }
 
         case MP_BINARY_OP_IN:
@@ -441,8 +439,9 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
     }
 
     // make joined string
-    byte *data;
-    mp_obj_t joined_str = mp_obj_str_builder_start(self_type, required_len, &data);
+    vstr_t vstr;
+    vstr_init_len(&vstr, required_len);
+    byte *data = (byte*)vstr.buf;
     for (mp_uint_t i = 0; i < seq_len; i++) {
         if (i > 0) {
             memcpy(data, sep_str, sep_len);
@@ -454,7 +453,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
     }
 
     // return joined string
-    return mp_obj_str_builder_end(joined_str);
+    return mp_obj_new_str_from_vstr(self_type, &vstr);
 }
 
 #define is_ws(c) ((c) == ' ' || (c) == '\t')
@@ -1485,7 +1484,7 @@ STATIC mp_obj_t str_replace(mp_uint_t n_args, const mp_obj_t *args) {
 
     // data for the replaced string
     byte *data = NULL;
-    mp_obj_t replaced_str = MP_OBJ_NULL;
+    vstr_t vstr;
 
     // do 2 passes over the string:
     //   first pass computes the required length of the replaced string
@@ -1537,7 +1536,8 @@ STATIC mp_obj_t str_replace(mp_uint_t n_args, const mp_obj_t *args) {
                 return args[0];
             } else {
                 // substr found, allocate new string
-                replaced_str = mp_obj_str_builder_start(self_type, replaced_str_index, &data);
+                vstr_init_len(&vstr, replaced_str_index);
+                data = (byte*)vstr.buf;
                 assert(data != NULL);
             }
         } else {
@@ -1546,7 +1546,7 @@ STATIC mp_obj_t str_replace(mp_uint_t n_args, const mp_obj_t *args) {
         }
     }
 
-    return mp_obj_str_builder_end(replaced_str);
+    return mp_obj_new_str_from_vstr(self_type, &vstr);
 }
 
 STATIC mp_obj_t str_count(mp_uint_t n_args, const mp_obj_t *args) {
@@ -1643,13 +1643,13 @@ STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg) {
 // Supposedly not too critical operations, so optimize for code size
 STATIC mp_obj_t str_caseconv(unichar (*op)(unichar), mp_obj_t self_in) {
     GET_STR_DATA_LEN(self_in, self_data, self_len);
-    byte *data;
-    mp_obj_t s = mp_obj_str_builder_start(mp_obj_get_type(self_in), self_len, &data);
+    vstr_t vstr;
+    vstr_init_len(&vstr, self_len);
+    byte *data = (byte*)vstr.buf;
     for (mp_uint_t i = 0; i < self_len; i++) {
         *data++ = op(*self_data++);
     }
-    *data = 0;
-    return mp_obj_str_builder_end(s);
+    return mp_obj_new_str_from_vstr(mp_obj_get_type(self_in), &vstr);
 }
 
 STATIC mp_obj_t str_lower(mp_obj_t self_in) {
@@ -1856,35 +1856,6 @@ const mp_obj_type_t mp_type_bytes = {
 // the zero-length bytes
 const mp_obj_str_t mp_const_empty_bytes_obj = {{&mp_type_bytes}, 0, 0, NULL};
 
-mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, mp_uint_t len, byte **data) {
-    mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
-    o->base.type = type;
-    o->len = len;
-    o->hash = 0;
-    byte *p = m_new(byte, len + 1);
-    o->data = p;
-    *data = p;
-    return o;
-}
-
-mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
-    mp_obj_str_t *o = o_in;
-    o->hash = qstr_compute_hash(o->data, o->len);
-    byte *p = (byte*)o->data;
-    p[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
-    return o;
-}
-
-mp_obj_t mp_obj_str_builder_end_with_len(mp_obj_t o_in, mp_uint_t len) {
-    mp_obj_str_t *o = o_in;
-    o->data = m_renew(byte, (byte*)o->data, o->len + 1, len + 1);
-    o->len = len;
-    o->hash = qstr_compute_hash(o->data, o->len);
-    byte *p = (byte*)o->data;
-    p[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
-    return o;
-}
-
 mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, mp_uint_t len) {
     mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
     o->base.type = type;
diff --git a/py/stream.c b/py/stream.c
index b7d4a9000..3415b74b9 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -160,11 +160,12 @@ STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) {
     }
     #endif
 
-    byte *buf;
-    mp_obj_t ret_obj = mp_obj_str_builder_start(STREAM_CONTENT_TYPE(o->type->stream_p), sz, &buf);
+    vstr_t vstr;
+    vstr_init_len(&vstr, sz);
     int error;
-    mp_uint_t out_sz = o->type->stream_p->read(o, buf, sz, &error);
+    mp_uint_t out_sz = o->type->stream_p->read(o, vstr.buf, sz, &error);
     if (out_sz == MP_STREAM_ERROR) {
+        vstr_clear(&vstr);
         if (is_nonblocking_error(error)) {
             // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
             // "If the object is in non-blocking mode and no bytes are available,
@@ -175,7 +176,9 @@ STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) {
         }
         nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
     } else {
-        return mp_obj_str_builder_end_with_len(ret_obj, out_sz);
+        vstr.len = out_sz;
+        vstr.buf[vstr.len] = '\0';
+        return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr);
     }
 }
 
@@ -252,7 +255,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
     vstr_t vstr;
     vstr_init(&vstr, DEFAULT_BUFFER_SIZE);
     char *p = vstr.buf;
-    mp_uint_t current_read = DEFAULT_BUFFER_SIZE;
+    mp_uint_t current_read = DEFAULT_BUFFER_SIZE - 1; // save 1 byte for null termination
     while (true) {
         int error;
         mp_uint_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error);
@@ -276,8 +279,8 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
             current_read -= out_sz;
             p += out_sz;
         } else {
-            current_read = DEFAULT_BUFFER_SIZE;
-            p = vstr_extend(&vstr, current_read);
+            p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE);
+            current_read = DEFAULT_BUFFER_SIZE - 1; // save 1 byte for null termination
             if (p == NULL) {
                 // TODO
                 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
@@ -286,7 +289,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
     }
 
     vstr.len = total_size;
-    vstr.buf[vstr.len] = '\0'; // XXX is there enough space?
+    vstr.buf[vstr.len] = '\0';
     return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr);
 }
 
diff --git a/py/vstr.c b/py/vstr.c
index 6856cfe39..63358d595 100644
--- a/py/vstr.c
+++ b/py/vstr.c
@@ -52,6 +52,11 @@ void vstr_init(vstr_t *vstr, size_t alloc) {
     vstr->fixed_buf = false;
 }
 
+void vstr_init_len(vstr_t *vstr, size_t len) {
+    vstr_init(vstr, len + 1);
+    vstr_add_len(vstr, len);
+}
+
 void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf) {
     assert(alloc > 0); // need at least room for the null byte
     vstr->alloc = alloc;
diff --git a/stmhal/bufhelper.c b/stmhal/bufhelper.c
index 57aef5a91..ca76e9496 100644
--- a/stmhal/bufhelper.c
+++ b/stmhal/bufhelper.c
@@ -38,15 +38,17 @@ void pyb_buf_get_for_send(mp_obj_t o, mp_buffer_info_t *bufinfo, byte *tmp_data)
     }
 }
 
-mp_obj_t pyb_buf_get_for_recv(mp_obj_t o, mp_buffer_info_t *bufinfo) {
+mp_obj_t pyb_buf_get_for_recv(mp_obj_t o, vstr_t *vstr) {
     if (MP_OBJ_IS_INT(o)) {
         // allocate a new bytearray of given length
-        bufinfo->len = mp_obj_get_int(o);
-        bufinfo->typecode = 'B';
-        return mp_obj_str_builder_start(&mp_type_bytes, bufinfo->len, (byte**)&bufinfo->buf);
+        vstr_init_len(vstr, mp_obj_get_int(o));
+        return MP_OBJ_NULL;
     } else {
         // get the existing buffer
-        mp_get_buffer_raise(o, bufinfo, MP_BUFFER_WRITE);
-        return MP_OBJ_NULL;
+        mp_buffer_info_t bufinfo;
+        mp_get_buffer_raise(o, &bufinfo, MP_BUFFER_WRITE);
+        vstr->buf = bufinfo.buf;
+        vstr->len = bufinfo.len;
+        return o;
     }
 }
diff --git a/stmhal/bufhelper.h b/stmhal/bufhelper.h
index febbe7e4f..abdeea6a8 100644
--- a/stmhal/bufhelper.h
+++ b/stmhal/bufhelper.h
@@ -25,4 +25,4 @@
  */
 
 void pyb_buf_get_for_send(mp_obj_t o, mp_buffer_info_t *bufinfo, byte *tmp_data);
-mp_obj_t pyb_buf_get_for_recv(mp_obj_t o, mp_buffer_info_t *bufinfo);
+mp_obj_t pyb_buf_get_for_recv(mp_obj_t o, vstr_t *vstr);
diff --git a/stmhal/can.c b/stmhal/can.c
index 28f00c994..b10ecd13d 100644
--- a/stmhal/can.c
+++ b/stmhal/can.c
@@ -395,12 +395,12 @@ STATIC mp_obj_t pyb_can_recv(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
     }
     tuple->items[1] = MP_OBJ_NEW_SMALL_INT(rx_msg.RTR);
     tuple->items[2] = MP_OBJ_NEW_SMALL_INT(rx_msg.FMI);
-    byte *data;
-    tuple->items[3] = mp_obj_str_builder_start(&mp_type_bytes, rx_msg.DLC, &data);
+    vstr_t vstr;
+    vstr_init_len(&vstr, rx_msg.DLC);
     for (mp_uint_t i = 0; i < rx_msg.DLC; i++) {
-        data[i] = rx_msg.Data[i]; // Data is uint32_t but holds only 1 byte
+        vstr.buf[i] = rx_msg.Data[i]; // Data is uint32_t but holds only 1 byte
     }
-    tuple->items[3] = mp_obj_str_builder_end(tuple->items[3]);
+    tuple->items[3] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
     return tuple;
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_can_recv_obj, 1, pyb_can_recv);
diff --git a/stmhal/i2c.c b/stmhal/i2c.c
index e4d406b4a..80b40b887 100644
--- a/stmhal/i2c.c
+++ b/stmhal/i2c.c
@@ -418,8 +418,8 @@ STATIC mp_obj_t pyb_i2c_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k
     mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_I2C_RECV_NUM_ARGS, pyb_i2c_recv_args, vals);
 
     // get the buffer to receive into
-    mp_buffer_info_t bufinfo;
-    mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &bufinfo);
+    vstr_t vstr;
+    mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr);
 
     // receive the data
     HAL_StatusTypeDef status;
@@ -428,9 +428,9 @@ STATIC mp_obj_t pyb_i2c_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k
             nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "addr argument required"));
         }
         mp_uint_t i2c_addr = vals[1].u_int << 1;
-        status = HAL_I2C_Master_Receive(self->i2c, i2c_addr, bufinfo.buf, bufinfo.len, vals[2].u_int);
+        status = HAL_I2C_Master_Receive(self->i2c, i2c_addr, (uint8_t*)vstr.buf, vstr.len, vals[2].u_int);
     } else {
-        status = HAL_I2C_Slave_Receive(self->i2c, bufinfo.buf, bufinfo.len, vals[2].u_int);
+        status = HAL_I2C_Slave_Receive(self->i2c, (uint8_t*)vstr.buf, vstr.len, vals[2].u_int);
     }
 
     if (status != HAL_OK) {
@@ -438,10 +438,10 @@ STATIC mp_obj_t pyb_i2c_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k
     }
 
     // return the received data
-    if (o_ret == MP_OBJ_NULL) {
-        return vals[0].u_obj;
+    if (o_ret != MP_OBJ_NULL) {
+        return o_ret;
     } else {
-        return mp_obj_str_builder_end(o_ret);
+        return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
     }
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_recv_obj, 1, pyb_i2c_recv);
@@ -479,8 +479,8 @@ STATIC mp_obj_t pyb_i2c_mem_read(mp_uint_t n_args, const mp_obj_t *args, mp_map_
     mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_I2C_MEM_READ_NUM_ARGS, pyb_i2c_mem_read_args, vals);
 
     // get the buffer to read into
-    mp_buffer_info_t bufinfo;
-    mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &bufinfo);
+    vstr_t vstr;
+    mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr);
 
     // get the addresses
     mp_uint_t i2c_addr = vals[1].u_int << 1;
@@ -491,17 +491,17 @@ STATIC mp_obj_t pyb_i2c_mem_read(mp_uint_t n_args, const mp_obj_t *args, mp_map_
         mem_addr_size = I2C_MEMADD_SIZE_16BIT;
     }
 
-    HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c, i2c_addr, mem_addr, mem_addr_size, bufinfo.buf, bufinfo.len, vals[3].u_int);
+    HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c, i2c_addr, mem_addr, mem_addr_size, (uint8_t*)vstr.buf, vstr.len, vals[3].u_int);
 
     if (status != HAL_OK) {
         mp_hal_raise(status);
     }
 
     // return the read data
-    if (o_ret == MP_OBJ_NULL) {
-        return vals[0].u_obj;
+    if (o_ret != MP_OBJ_NULL) {
+        return o_ret;
     } else {
-        return mp_obj_str_builder_end(o_ret);
+        return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
     }
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_read_obj, 1, pyb_i2c_mem_read);
diff --git a/stmhal/moduos.c b/stmhal/moduos.c
index 8706f4981..9f8de8046 100644
--- a/stmhal/moduos.c
+++ b/stmhal/moduos.c
@@ -322,12 +322,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync);
 /// random number generator.
 STATIC mp_obj_t os_urandom(mp_obj_t num) {
     mp_int_t n = mp_obj_get_int(num);
-    byte *data;
-    mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, n, &data);
+    vstr_t vstr;
+    vstr_init_len(&vstr, n);
     for (int i = 0; i < n; i++) {
-        data[i] = rng_get();
+        vstr.buf[i] = rng_get();
     }
-    return mp_obj_str_builder_end(o);
+    return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
 #endif
diff --git a/stmhal/modusocket.c b/stmhal/modusocket.c
index 781720b53..caa8843eb 100644
--- a/stmhal/modusocket.c
+++ b/stmhal/modusocket.c
@@ -201,17 +201,19 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
         nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOTCONN)));
     }
     mp_int_t len = mp_obj_get_int(len_in);
-    byte *buf;
-    mp_obj_t ret_obj = mp_obj_str_builder_start(&mp_type_bytes, len, &buf);
+    vstr_t vstr;
+    vstr_init_len(&vstr, len);
     int _errno;
-    mp_uint_t ret = self->nic_type->recv(self, buf, len, &_errno);
+    mp_uint_t ret = self->nic_type->recv(self, (byte*)vstr.buf, len, &_errno);
     if (ret == -1) {
         nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
     }
     if (ret == 0) {
         return mp_const_empty_bytes;
     }
-    return mp_obj_str_builder_end_with_len(ret_obj, ret);
+    vstr.len = ret;
+    vstr.buf[vstr.len] = '\0';
+    return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv);
 
@@ -248,25 +250,24 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
         // not connected
         nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOTCONN)));
     }
-    mp_int_t len = mp_obj_get_int(len_in);
-    byte *buf;
-    mp_obj_t ret_obj = mp_obj_str_builder_start(&mp_type_bytes, len, &buf);
+    vstr_t vstr;
+    vstr_init_len(&vstr, mp_obj_get_int(len_in));
     byte ip[4];
     mp_uint_t port;
     int _errno;
-    mp_int_t ret = self->nic_type->recvfrom(self, buf, len, ip, &port, &_errno);
+    mp_int_t ret = self->nic_type->recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno);
     if (ret == -1) {
         nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
     }
+    mp_obj_t tuple[2];
     if (ret == 0) {
-        ret_obj = mp_const_empty_bytes;
+        tuple[0] = mp_const_empty_bytes;
     } else {
-        ret_obj = mp_obj_str_builder_end_with_len(ret_obj, ret);
+        vstr.len = ret;
+        vstr.buf[vstr.len] = '\0';
+        tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
     }
-    mp_obj_t tuple[2] = {
-        ret_obj,
-        mod_network_format_inet_addr(ip, port),
-    };
+    tuple[1] = mod_network_format_inet_addr(ip, port);
     return mp_obj_new_tuple(2, tuple);
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recvfrom_obj, socket_recvfrom);
diff --git a/stmhal/spi.c b/stmhal/spi.c
index 109bfe49e..6f8b8f1db 100644
--- a/stmhal/spi.c
+++ b/stmhal/spi.c
@@ -565,15 +565,15 @@ STATIC mp_obj_t pyb_spi_recv(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
     mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
 
     // get the buffer to receive into
-    mp_buffer_info_t bufinfo;
-    mp_obj_t o_ret = pyb_buf_get_for_recv(args[0].u_obj, &bufinfo);
+    vstr_t vstr;
+    mp_obj_t o_ret = pyb_buf_get_for_recv(args[0].u_obj, &vstr);
 
     // receive the data
     HAL_StatusTypeDef status;
     if (query_irq() == IRQ_STATE_DISABLED) {
-        status = HAL_SPI_Receive(self->spi, bufinfo.buf, bufinfo.len, args[1].u_int);
+        status = HAL_SPI_Receive(self->spi, (uint8_t*)vstr.buf, vstr.len, args[1].u_int);
     } else {
-        status = HAL_SPI_Receive_DMA(self->spi, bufinfo.buf, bufinfo.len);
+        status = HAL_SPI_Receive_DMA(self->spi, (uint8_t*)vstr.buf, vstr.len);
         if (status == HAL_OK) {
             status = spi_wait_dma_finished(self->spi, args[1].u_int);
         }
@@ -584,10 +584,10 @@ STATIC mp_obj_t pyb_spi_recv(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
     }
 
     // return the received data
-    if (o_ret == MP_OBJ_NULL) {
-        return args[0].u_obj;
+    if (o_ret != MP_OBJ_NULL) {
+        return o_ret;
     } else {
-        return mp_obj_str_builder_end(o_ret);
+        return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
     }
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_recv_obj, 1, pyb_spi_recv);
@@ -621,13 +621,14 @@ STATIC mp_obj_t pyb_spi_send_recv(mp_uint_t n_args, const mp_obj_t *pos_args, mp
     mp_buffer_info_t bufinfo_send;
     uint8_t data_send[1];
     mp_buffer_info_t bufinfo_recv;
+    vstr_t vstr_recv;
     mp_obj_t o_ret;
 
     if (args[0].u_obj == args[1].u_obj) {
         // same object for send and receive, it must be a r/w buffer
         mp_get_buffer_raise(args[0].u_obj, &bufinfo_send, MP_BUFFER_RW);
         bufinfo_recv = bufinfo_send;
-        o_ret = MP_OBJ_NULL;
+        o_ret = args[0].u_obj;
     } else {
         // get the buffer to send from
         pyb_buf_get_for_send(args[0].u_obj, &bufinfo_send, data_send);
@@ -635,16 +636,17 @@ STATIC mp_obj_t pyb_spi_send_recv(mp_uint_t n_args, const mp_obj_t *pos_args, mp
         // get the buffer to receive into
         if (args[1].u_obj == MP_OBJ_NULL) {
             // only send argument given, so create a fresh buffer of the send length
-            bufinfo_recv.len = bufinfo_send.len;
-            bufinfo_recv.typecode = 'B';
-            o_ret = mp_obj_str_builder_start(&mp_type_bytes, bufinfo_recv.len, (byte**)&bufinfo_recv.buf);
+            vstr_init_len(&vstr_recv, bufinfo_send.len);
+            bufinfo_recv.len = vstr_recv.len;
+            bufinfo_recv.buf = vstr_recv.buf;
+            o_ret = MP_OBJ_NULL;
         } else {
             // recv argument given
             mp_get_buffer_raise(args[1].u_obj, &bufinfo_recv, MP_BUFFER_WRITE);
             if (bufinfo_recv.len != bufinfo_send.len) {
                 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "recv must be same length as send"));
             }
-            o_ret = MP_OBJ_NULL;
+            o_ret = args[1].u_obj;
         }
     }
 
@@ -664,10 +666,10 @@ STATIC mp_obj_t pyb_spi_send_recv(mp_uint_t n_args, const mp_obj_t *pos_args, mp
     }
 
     // return the received data
-    if (o_ret == MP_OBJ_NULL) {
-        return args[1].u_obj;
+    if (o_ret != MP_OBJ_NULL) {
+        return o_ret;
     } else {
-        return mp_obj_str_builder_end(o_ret);
+        return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr_recv);
     }
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_send_recv_obj, 1, pyb_spi_send_recv);
diff --git a/stmhal/usb.c b/stmhal/usb.c
index 0bda0aaeb..8ea5fb561 100644
--- a/stmhal/usb.c
+++ b/stmhal/usb.c
@@ -232,17 +232,17 @@ STATIC mp_obj_t pyb_usb_vcp_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_
     mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_USB_VCP_SEND_NUM_ARGS, pyb_usb_vcp_send_args, vals);
 
     // get the buffer to receive into
-    mp_buffer_info_t bufinfo;
-    mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &bufinfo);
+    vstr_t vstr;
+    mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr);
 
     // receive the data
-    int ret = USBD_CDC_Rx(bufinfo.buf, bufinfo.len, vals[1].u_int);
+    int ret = USBD_CDC_Rx((uint8_t*)vstr.buf, vstr.len, vals[1].u_int);
 
     // return the received data
-    if (o_ret == MP_OBJ_NULL) {
+    if (o_ret != MP_OBJ_NULL) {
         return mp_obj_new_int(ret); // number of bytes read into given buffer
     } else {
-        return mp_obj_str_builder_end_with_len(o_ret, ret); // create a new buffer
+        return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); // create a new buffer
     }
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_vcp_recv_obj, 1, pyb_usb_vcp_recv);
-- 
GitLab