Skip to content

Commit 0d21d8d

Browse files
authored
Refactor WebSocket reader to avoid creating lists (aio-libs#10740)
1 parent 8d74e26 commit 0d21d8d

4 files changed

Lines changed: 340 additions & 371 deletions

File tree

CHANGES/10740.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved performance of the WebSocket reader -- by :user:`bdraco`.

aiohttp/_websocket/reader_c.pxd

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ cdef unsigned int READ_PAYLOAD_LENGTH
88
cdef unsigned int READ_PAYLOAD_MASK
99
cdef unsigned int READ_PAYLOAD
1010

11-
cdef unsigned int OP_CODE_CONTINUATION
12-
cdef unsigned int OP_CODE_TEXT
13-
cdef unsigned int OP_CODE_BINARY
14-
cdef unsigned int OP_CODE_CLOSE
15-
cdef unsigned int OP_CODE_PING
16-
cdef unsigned int OP_CODE_PONG
11+
cdef int OP_CODE_NOT_SET
12+
cdef int OP_CODE_CONTINUATION
13+
cdef int OP_CODE_TEXT
14+
cdef int OP_CODE_BINARY
15+
cdef int OP_CODE_CLOSE
16+
cdef int OP_CODE_PING
17+
cdef int OP_CODE_PONG
18+
19+
cdef int COMPRESSED_NOT_SET
20+
cdef int COMPRESSED_FALSE
21+
cdef int COMPRESSED_TRUE
1722

1823
cdef object UNPACK_LEN3
1924
cdef object UNPACK_CLOSE_CODE
@@ -66,9 +71,9 @@ cdef class WebSocketReader:
6671
cdef bytearray _partial
6772
cdef unsigned int _state
6873

69-
cdef object _opcode
70-
cdef object _frame_fin
71-
cdef object _frame_opcode
74+
cdef int _opcode
75+
cdef bint _frame_fin
76+
cdef int _frame_opcode
7277
cdef object _frame_payload
7378
cdef unsigned long long _frame_payload_len
7479

@@ -77,7 +82,7 @@ cdef class WebSocketReader:
7782
cdef bytes _frame_mask
7883
cdef unsigned long long _payload_length
7984
cdef unsigned int _payload_length_flag
80-
cdef object _compressed
85+
cdef int _compressed
8186
cdef object _decompressobj
8287
cdef bint _compress
8388

@@ -88,22 +93,21 @@ cdef class WebSocketReader:
8893
fin=bint,
8994
has_partial=bint,
9095
payload_merged=bytes,
91-
opcode="unsigned int",
9296
)
93-
cpdef void _feed_data(self, bytes data)
97+
cpdef void _handle_frame(self, bint fin, int opcode, object payload, int compressed) except *
9498

9599
@cython.locals(
96100
start_pos="unsigned int",
97-
buf_len="unsigned int",
101+
data_len="unsigned int",
98102
length="unsigned int",
99103
chunk_size="unsigned int",
100104
chunk_len="unsigned int",
101-
buf_length="unsigned int",
102-
buf_cstr="const unsigned char *",
105+
data_length="unsigned int",
106+
data_cstr="const unsigned char *",
103107
first_byte="unsigned char",
104108
second_byte="unsigned char",
105109
end_pos="unsigned int",
106110
has_mask=bint,
107111
fin=bint,
108112
)
109-
cpdef list parse_frame(self, bytes buf)
113+
cpdef void _feed_data(self, bytes data) except *

0 commit comments

Comments
 (0)