-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Websocket XOR performance improved. Fixes #686 #687
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,48 @@ | ||
from cpython cimport PyBytes_FromStringAndSize, PyBytes_AsString | ||
from cpython.ref cimport PyObject | ||
from cpython cimport PyBytes_AsString | ||
|
||
#from cpython cimport PyByteArray_AsString # cython still not exports that | ||
cdef extern from "Python.h": | ||
char* PyByteArray_AsString(object bytearray) except NULL | ||
char* PyByteArray_AsString(bytearray ba) except NULL | ||
|
||
from libc.stdint cimport uint32_t, uint64_t, uintmax_t | ||
|
||
def _websocket_mask_cython(bytes mask, bytearray data): | ||
cdef Py_ssize_t mask_len, data_len, i | ||
cdef char * in_buf | ||
cdef char * out_buf | ||
cdef char * mask_buf | ||
cdef bytes ret | ||
mask_len = len(mask) | ||
"""Note, this function mutates it's `data` argument | ||
""" | ||
cdef: | ||
Py_ssize_t data_len, i | ||
# bit operations on signed integers are implementation-specific | ||
unsigned char * in_buf | ||
const unsigned char * mask_buf | ||
uint32_t uint32_msk | ||
uint64_t uint64_msk | ||
|
||
assert len(mask) == 4 | ||
|
||
data_len = len(data) | ||
in_buf = PyByteArray_AsString(data) | ||
mask_buf = PyBytes_AsString(mask) | ||
in_buf = <unsigned char*>PyByteArray_AsString(data) | ||
mask_buf = <const unsigned char*>PyBytes_AsString(mask) | ||
uint32_msk = (<uint32_t*>mask_buf)[0] | ||
|
||
# TODO: align in_data ptr to achieve even faster speeds | ||
# does it need in python ?! malloc() always aligns to sizeof(long) bytes | ||
|
||
if sizeof(uintmax_t) >= 8: | ||
uint64_msk = uint32_msk | ||
uint64_msk = (uint64_msk << 32) | uint32_msk | ||
|
||
while data_len >= 8: | ||
(<uint64_t*>in_buf)[0] ^= uint64_msk | ||
in_buf += 8 | ||
data_len -= 8 | ||
|
||
|
||
while data_len >= 4: | ||
(<uint32_t*>in_buf)[0] ^= uint32_msk | ||
in_buf += 4 | ||
data_len -= 4 | ||
|
||
for i in range(0, data_len): | ||
in_buf[i] = in_buf[i] ^ mask_buf[i % 4] | ||
in_buf[i] ^= mask_buf[i] | ||
|
||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sys.byteorder
is little (at least for me), while iirc websocket uses network one which is big. Could anyone confirm this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in that operation, byte order is irrelevant, since actual numeric value is not used in that code. i.e. byte representation of xored result does not depend on that order.
But, it is a question, which order is optimal to internal bigint logic in python.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok. Thanks!