Skip to content

Commit

Permalink
Performance optimization for upb python extend repeated scalars.
Browse files Browse the repository at this point in the history
Before:
_bench_append, 100000, 241.9
_bench_extend, 100000, 112.3
_bench_assign, 100000, 83.3
_bench_pybind11, 100000, 16.2

After:
_bench_append, 100000, 224.3
_bench_extend, 100000, 71.2
_bench_assign, 100000, 81.1
_bench_pybind11, 100000, 14.7
PiperOrigin-RevId: 707749522
  • Loading branch information
anandolee authored and copybara-github committed Dec 19, 2024
1 parent 9e44913 commit 5dfc88b
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions python/repeated.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,35 @@ PyObject* PyUpb_RepeatedContainer_Extend(PyObject* _self, PyObject* value) {
bool submsg = upb_FieldDef_IsSubMessage(f);
PyObject* e;

while ((e = PyIter_Next(it))) {
PyObject* ret;
if (submsg) {
ret = PyUpb_RepeatedCompositeContainer_Append(_self, e);
} else {
ret = PyUpb_RepeatedScalarContainer_Append(_self, e);
Py_ssize_t size = PyObject_Size(value);
if (size < 0) {
PyErr_Clear();
}
if ((size > 0) && (!submsg)) {
upb_Arena* arena = PyUpb_Arena_Get(self->arena);
const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
upb_Array_Resize(arr, start_size + size, arena);
int index = start_size;
while ((e = PyIter_Next(it))) {
upb_MessageValue msgval;
if (!PyUpb_PyToUpb(e, f, &msgval, arena)) {
return NULL;
}
upb_Array_Set(arr, index, msgval);
Py_DECREF(e);
index++;
}
} else {
while ((e = PyIter_Next(it))) {
PyObject* ret;
if (submsg) {
ret = PyUpb_RepeatedCompositeContainer_Append(_self, e);
} else {
ret = PyUpb_RepeatedScalarContainer_Append(_self, e);
}
Py_XDECREF(ret);
Py_DECREF(e);
}
Py_XDECREF(ret);
Py_DECREF(e);
}

Py_DECREF(it);
Expand Down

0 comments on commit 5dfc88b

Please sign in to comment.