Reduce WebSocket buffer slicing overhead (#10601)
<!-- Thank you for your contribution! -->
## What do these changes do?
Use a `const unsigned char *` for the buffer (Cython will automatically
extract is using `__Pyx_PyBytes_AsUString`) as its a lot faster than
copying around `PyBytes` objects. We do need to be careful that all
slices are bounded and we bound check everything to make sure we do not
do an out of bounds read since Cython does not bounds check C strings.
I checked that all accesses to `buf_cstr` are proceeded by a bounds
check but it would be good to get another set of eyes on that to verify
in the `self._state == READ_PAYLOAD` block that we will never try to
read out of bounds.
<img width="376" alt="Screenshot 2025-03-19 at 10 21 54 AM"
src="https://github.com/user-attachments/assets/a340ffa2-f09b-4aff-a4f7-c487dae186c8"
/>
## Are there changes in behavior for the user?
performance improvement
## Is it a substantial burden for the maintainers to support this?
no
There is a small risk that someone could remove a bounds check in the
future and create a memory safety issue, however in this case its likely
we would already be trying to read data that wasn't there if we are
missing the bounds checking so the pure python version would throw if we
are testing properly.
---------
Co-authored-by: Sam Bull <git@sambull.org>
(cherry picked from commit f7cac7e63f18691e4261af353e84f9073b16624a)
0e583e9
18 days ago
by bdraco
0%
Merge branch '3.11' into patchback/backports/3.11/f7cac7e63f18691e4261af353e84f9073b16624a/pr-10601