-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-139871: Optimize bytearray unique bytes iconcat #141862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
db4c09a
08364c1
70c15bf
614e743
c5e9da6
e6fd741
fe114fb
c689a0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -333,6 +333,42 @@ bytearray_iconcat_lock_held(PyObject *op, PyObject *other) | |
| _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op); | ||
| PyByteArrayObject *self = _PyByteArray_CAST(op); | ||
|
|
||
| // optimization: Avoid copying the bytes coming in when possible. | ||
| if (self->ob_alloc == 0 && _PyObject_IsUniquelyReferenced(other)) { | ||
| assert(_Py_IsImmortal(self->ob_bytes_object)); | ||
| if (!_canresize(self)) { | ||
|
vstinner marked this conversation as resolved.
|
||
| return NULL; | ||
| } | ||
|
|
||
| /* Get the bytes out of the temporary bytearray. | ||
| * Just returning other doesn't work as __init__ calls this and can't | ||
| * change self. */ | ||
| if (PyByteArray_CheckExact(other)) { | ||
| PyObject *taken = PyObject_CallMethodNoArgs(other, | ||
| &_Py_ID(take_bytes)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks unsafe to me. If you call a method, you may invalidate the assumptions you verified earlier
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe call bytearray_take_bytes_impl() directly to reduce the risk of side effects? And you can check again _PyObject_IsUniquelyReferenced() in an assertion. |
||
| if (taken == NULL) { | ||
| return NULL; | ||
| } | ||
| // Avoid Py_INCREF needed for argument case. | ||
| Py_ssize_t size = Py_SIZE(taken); | ||
| self->ob_bytes_object = taken; | ||
| bytearray_reinit_from_bytes(self, size, size); | ||
| return Py_NewRef(self); | ||
| } | ||
|
|
||
| if (PyBytes_CheckExact(other)) { | ||
| Py_ssize_t size = Py_SIZE(other); | ||
| self->ob_bytes_object = other; | ||
| bytearray_reinit_from_bytes(self, size, size); | ||
| Py_INCREF(self->ob_bytes_object); | ||
|
|
||
| // Caller has a reference still and its decref will return | ||
| // bytes to be uniquely referenced. | ||
| assert(Py_REFCNT(self->ob_bytes_object) == 2); | ||
| return Py_NewRef(self); | ||
| } | ||
| } | ||
|
|
||
| Py_buffer vo; | ||
| if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) { | ||
| PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", | ||
|
|
@@ -977,22 +1013,14 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, | |
| } | ||
| } | ||
|
|
||
| /* Use the buffer API */ | ||
| /* Use the buffer API. Defer to iconcat which optimizes. */ | ||
| if (PyObject_CheckBuffer(arg)) { | ||
| Py_ssize_t size; | ||
| Py_buffer view; | ||
| if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0) | ||
| PyObject *new = bytearray_iconcat((PyObject *)self, arg); | ||
| if (new == NULL) { | ||
| return -1; | ||
| size = view.len; | ||
| if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail; | ||
| if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self), | ||
| &view, size, 'C') < 0) | ||
| goto fail; | ||
| PyBuffer_Release(&view); | ||
| } | ||
| Py_DECREF(new); | ||
| return 0; | ||
| fail: | ||
| PyBuffer_Release(&view); | ||
| return -1; | ||
| } | ||
|
|
||
| if (PyList_CheckExact(arg) || PyTuple_CheckExact(arg)) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.