From 0ad6b021921785d35aff6958912f892d7259fd3f Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Sun, 1 Nov 2026 12:49:47 -0800 Subject: [PATCH] py: skip scanning string and bytes data during garbage collection String and bytes buffers never hold pointers on firmware, so allocate them without the collect flag and collections get cheaper. Co-Authored-By: Claude Opus 5.5 --- py/objstr.c | 3 ++- py/objstringio.c | 3 ++- py/vstr.c | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/py/objstr.c b/py/objstr.c index 99a620cb59c..a157228c363 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -2408,7 +2408,8 @@ mp_obj_t mp_obj_new_str_copy(const mp_obj_type_t *type, const byte *data, size_t o->len = len; if (data) { o->hash = qstr_compute_hash(data, len); - byte *p = m_new(byte, len + 1); + // CIRCUITPY-CHANGE: Don't scan string data for pointers. + byte *p = m_malloc_without_collect(sizeof(byte) * (len + 1)); o->data = p; memcpy(p, data, len * sizeof(byte)); p[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings diff --git a/py/objstringio.c b/py/objstringio.c index 25e04cb4c69..a5dbc74ab07 100644 --- a/py/objstringio.c +++ b/py/objstringio.c @@ -83,7 +83,8 @@ static mp_uint_t stringio_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *er static void stringio_copy_on_write(mp_obj_stringio_t *o) { const void *buf = o->vstr->buf; - o->vstr->buf = m_new(char, o->vstr->len); + // CIRCUITPY-CHANGE: Don't scan string data for pointers. + o->vstr->buf = m_malloc_without_collect(sizeof(char) * o->vstr->len); o->vstr->fixed_buf = false; o->ref_obj = MP_OBJ_NULL; memcpy(o->vstr->buf, buf, o->vstr->len); diff --git a/py/vstr.c b/py/vstr.c index 95607d11bb8..3d66d535ac2 100644 --- a/py/vstr.c +++ b/py/vstr.c @@ -44,7 +44,8 @@ void vstr_init(vstr_t *vstr, size_t alloc) { } vstr->alloc = alloc; vstr->len = 0; - vstr->buf = m_new(char, vstr->alloc); + // CIRCUITPY-CHANGE: Don't scan string data for pointers. + vstr->buf = m_malloc_without_collect(sizeof(char) * vstr->alloc); vstr->fixed_buf = false; }