From 53d900445bcbd5ef1101c99ffe83aebf3bdc85d3 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Wed, 4 Feb 2026 09:38:40 +0000 Subject: [PATCH] py/objarray: Avoid double zero init on sized bytearrays. As per the implementation of m_malloc0, if MICROPY_GC_CONSERVATIVE_CLEAR is set then all RAM is guaranteed to be zero-init by gc_alloc. py/objarray.c: Guard the explicit zero init in bytearray_make_new against being run, initialising the RAM to zero a second time, if this flag is set. Note that MICROPY_GC_CONSERVATIVE_CLEAR is default enabled by MICROPY_ENABLE_GC, and no ports currently override this value. Co-authored-by: Mike Bell Signed-off-by: Phil Howard --- py/objarray.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/py/objarray.c b/py/objarray.c index ac4e343d06..75bc671c16 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -191,7 +191,10 @@ static mp_obj_t bytearray_make_new(const mp_obj_type_t *type_in, size_t n_args, // 1 arg, an integer: construct a blank bytearray of that length mp_uint_t len = mp_obj_get_int(args[0]); mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len); + // If this config is set then the GC clears all memory, so we don't need to. + #if !MICROPY_GC_CONSERVATIVE_CLEAR memset(o->items, 0, len); + #endif return MP_OBJ_FROM_PTR(o); } else { // 1 arg: construct the bytearray from that