Skip to content

Commit 640643e

Browse files
committed
take multiplication out of the loop
1 parent 3d287c7 commit 640643e

3 files changed

Lines changed: 10 additions & 9 deletions

File tree

Include/internal/pycore_object.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,14 @@ static inline int _PyType_SUPPORTS_WEAKREFS(PyTypeObject *type) {
254254

255255
/// Method to copy objects for specified number of times inside a buffer
256256
static inline void
257-
_objects_repeat(PyObject** items, Py_ssize_t copied, Py_ssize_t len_dest)
257+
_objects_repeat(char *dest, Py_ssize_t len_dest, Py_ssize_t len_src)
258258
{
259-
while (copied < len_dest) {
260-
Py_ssize_t items_to_copy = Py_MIN(copied, len_dest - copied);
261-
memcpy(items + copied, items, sizeof(PyObject*) * items_to_copy);
262-
copied += items_to_copy;
263-
}
259+
Py_ssize_t copied = len_src;
260+
while (copied < len_dest) {
261+
Py_ssize_t bytes_to_copy = Py_MIN(copied, len_dest - copied);
262+
memcpy(dest + copied, dest, bytes_to_copy);
263+
copied += bytes_to_copy;
264+
}
264265
}
265266

266267
extern PyObject* _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems);

Objects/listobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ list_repeat(PyListObject *a, Py_ssize_t n)
573573
*dest++ = *src++;
574574
}
575575

576-
_objects_repeat(np->ob_item, input_size, output_size);
576+
_objects_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size, sizeof(PyObject *)*input_size);
577577
}
578578

579579
Py_SET_SIZE(np, output_size);
@@ -752,7 +752,7 @@ list_inplace_repeat(PyListObject *self, Py_ssize_t n)
752752
for (Py_ssize_t j = 0; j < input_size; j++) {
753753
Py_INCREF_n(items[j], n-1);
754754
}
755-
_objects_repeat(items, input_size, output_size);
755+
_objects_repeat((char *)items, sizeof(PyObject *)*output_size, sizeof(PyObject *)*input_size);
756756

757757
Py_INCREF(self);
758758
return (PyObject *)self;

Objects/tupleobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ tuplerepeat(PyTupleObject *a, Py_ssize_t n)
535535
*dest++ = *src++;
536536
}
537537

538-
_objects_repeat(np->ob_item, input_size, output_size);
538+
_objects_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size, sizeof(PyObject *)*input_size);
539539
}
540540
_PyObject_GC_TRACK(np);
541541
return (PyObject *) np;

0 commit comments

Comments
 (0)