Skip to content

Commit 6a388aa

Browse files
committed
py: reduce array slice assignment code size
1 parent 2af846e commit 6a388aa

1 file changed

Lines changed: 15 additions & 22 deletions

File tree

py/objarray.c

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -402,37 +402,30 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
402402

403403
// TODO: check src/dst compat
404404
mp_int_t len_adj = src_len - (slice.stop - slice.start);
405-
if (len_adj > 0) {
406-
#if MICROPY_PY_BUILTINS_MEMORYVIEW
407-
if (o->base.type == &mp_type_memoryview) {
405+
uint8_t* dest_items = o->items;
406+
#if MICROPY_PY_BUILTINS_MEMORYVIEW
407+
if (o->base.type == &mp_type_memoryview) {
408+
if (len_adj != 0) {
408409
goto compat_error;
409410
}
410-
#endif
411+
dest_items += o->free * item_sz;
412+
}
413+
#endif
414+
if (len_adj > 0) {
411415
if (len_adj > o->free) {
412416
// TODO: alloc policy; at the moment we go conservative
413417
o->items = m_renew(byte, o->items, (o->len + o->free) * item_sz, (o->len + len_adj) * item_sz);
414418
o->free = 0;
415419
}
416-
mp_seq_replace_slice_grow_inplace(o->items, o->len,
420+
mp_seq_replace_slice_grow_inplace(dest_items, o->len,
417421
slice.start, slice.stop, src_items, src_len, len_adj, item_sz);
418422
} else {
419-
#if MICROPY_PY_BUILTINS_MEMORYVIEW
420-
if (o->base.type == &mp_type_memoryview) {
421-
if (len_adj != 0) {
422-
goto compat_error;
423-
}
424-
mp_seq_replace_slice_no_grow((uint8_t*)o->items + (o->free * item_sz), o->len,
425-
slice.start, slice.stop, src_items, src_len, item_sz);
426-
} else
427-
#endif
428-
{
429-
mp_seq_replace_slice_no_grow(o->items, o->len,
430-
slice.start, slice.stop, src_items, src_len, item_sz);
431-
// Clear "freed" elements at the end of list
432-
// TODO: This is actually only needed for typecode=='O'
433-
mp_seq_clear(o->items, o->len + len_adj, o->len, item_sz);
434-
// TODO: alloc policy after shrinking
435-
}
423+
mp_seq_replace_slice_no_grow(dest_items, o->len,
424+
slice.start, slice.stop, src_items, src_len, item_sz);
425+
// Clear "freed" elements at the end of list
426+
// TODO: This is actually only needed for typecode=='O'
427+
mp_seq_clear(dest_items, o->len + len_adj, o->len, item_sz);
428+
// TODO: alloc policy after shrinking
436429
}
437430
o->len += len_adj;
438431
return mp_const_none;

0 commit comments

Comments
 (0)