[PR #10601/f7cac7e6 backport][3.12] Reduce WebSocket buffer slicing overhead #10640
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a backport of PR #10601 as merged into master (f7cac7e).
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 aroundPyBytes
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 theself._state == READ_PAYLOAD
block that we will never try to read out of bounds.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.