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 <mdb036@gmail.com>
Signed-off-by: Phil Howard <github@gadgetoid.com>
This commit is contained in:
Phil Howard
2026-02-04 09:38:40 +00:00
committed by Damien George
parent b2073a09eb
commit 53d900445b

View File

@@ -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