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 f0b62ba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
2 changes: 2 additions & 0 deletions python/google/protobuf/internal/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,8 @@ def testExtendInt32WithPythonList(self, message_module):
self.assertSequenceEqual([0, 1, 2], m.repeated_int32)
m.repeated_int32.extend([3, 4])
self.assertSequenceEqual([0, 1, 2, 3, 4], m.repeated_int32)
with self.assertRaises(TypeError):
m.repeated_int32.extend([5, 6, 'hi', 7])

def testExtendFloatWithPythonList(self, message_module):
"""Test extending repeated float fields with python lists."""
Expand Down
37 changes: 29 additions & 8 deletions python/repeated.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,36 @@ 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)) {
Py_DECREF(e);
break;
}
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 f0b62ba

Please sign in to comment.