diff --git a/mypyc/lib-rt/vecs/vec_nested.c b/mypyc/lib-rt/vecs/vec_nested.c index a958ce9b8dba..3a3ef89a4fff 100644 --- a/mypyc/lib-rt/vecs/vec_nested.c +++ b/mypyc/lib-rt/vecs/vec_nested.c @@ -458,9 +458,7 @@ VecNested VecNested_Remove(VecNested self, VecNestedBufItem arg) { if (i < self.len - 1) { // Decref the removed item last, since it could run arbitrary code VecNestedBufItem removed = items[i]; - for (; i < self.len - 1; i++) { - items[i] = items[i + 1]; - } + memmove(&items[i], &items[i + 1], (self.len - 1 - i) * sizeof(VecNestedBufItem)); VecNested_ItemXINCREF(self_buf, items[self.len - 1]); VecNested_ItemXDECREF(self_buf, removed); } @@ -499,8 +497,7 @@ VecNestedPopResult VecNested_Pop(VecNested v, Py_ssize_t index) { VecNestedBufItem *items = v.items; VecNestedBufObject *v_buf = VEC_BUF(v); result.f1 = items[index]; - for (Py_ssize_t i = index; i < v.len - 1; i++) - items[i] = items[i + 1]; + memmove(&items[index], &items[index + 1], (v.len - 1 - index) * sizeof(VecNestedBufItem)); if (v.len > 0) VecNested_ItemXINCREF(v_buf, items[v.len - 1]); v.len--; diff --git a/mypyc/lib-rt/vecs/vec_t.c b/mypyc/lib-rt/vecs/vec_t.c index 56565c8789d8..3208c3704d91 100644 --- a/mypyc/lib-rt/vecs/vec_t.c +++ b/mypyc/lib-rt/vecs/vec_t.c @@ -507,9 +507,7 @@ VecT VecT_Remove(VecT v, PyObject *arg) { if (i < v.len - 1) { // Decref the removed item last, since it could run arbitrary code PyObject *removed = items[i]; - for (; i < v.len - 1; i++) { - items[i] = items[i + 1]; - } + memmove(&items[i], &items[i + 1], (v.len - 1 - i) * sizeof(PyObject *)); // Keep a duplicate item, since there could be another reference // to the buffer with a longer length, and they expect a valid reference. Py_XINCREF(items[v.len - 1]); @@ -546,8 +544,7 @@ VecTPopResult VecT_Pop(VecT v, Py_ssize_t index) { PyObject **items = v.items; result.f1 = items[index]; - for (Py_ssize_t i = index; i < v.len - 1; i++) - items[i] = items[i + 1]; + memmove(&items[index], &items[index + 1], (v.len - 1 - index) * sizeof(PyObject *)); // Keep duplicate item, since there could be another reference // to the buffer with a longer length, and they expect a valid reference. Py_XINCREF(items[v.len - 1]); diff --git a/mypyc/lib-rt/vecs/vec_template.c b/mypyc/lib-rt/vecs/vec_template.c index fd8d1bd1b10c..f07d7394b85c 100644 --- a/mypyc/lib-rt/vecs/vec_template.c +++ b/mypyc/lib-rt/vecs/vec_template.c @@ -645,9 +645,7 @@ PyObject *FUNC(ToTuple)(VEC v) { VEC FUNC(Remove)(VEC v, ITEM_C_TYPE x) { for (Py_ssize_t i = 0; i < v.len; i++) { if (v.items[i] == x) { - for (; i < v.len - 1; i++) { - v.items[i] = v.items[i + 1]; - } + memmove(&v.items[i], &v.items[i + 1], (v.len - 1 - i) * sizeof(ITEM_C_TYPE)); v.len--; // Return the stolen reference without INCREF return v; @@ -678,9 +676,7 @@ NAME(PopResult) FUNC(Pop)(VEC v, Py_ssize_t index) { } result.f1 = v.items[index]; - for (Py_ssize_t i = index; i < v.len - 1; i++) { - v.items[i] = v.items[i + 1]; - } + memmove(&v.items[index], &v.items[index + 1], (v.len - 1 - index) * sizeof(ITEM_C_TYPE)); v.len--; // Return the stolen reference without INCREF