Skip to content

Commit 099cc0c

Browse files
authored
[PR #10740/0d21d8d backport][3.11] Refactor WebSocket reader to avoid creating lists (#10746)
1 parent 2c3b885 commit 099cc0c

File tree

4 files changed

+330
-337
lines changed

4 files changed

+330
-337
lines changed

CHANGES/10740.misc.rst

+1
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

+20-16
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
@@ -60,9 +65,9 @@ cdef class WebSocketReader:
6065
cdef bytearray _partial
6166
cdef unsigned int _state
6267

63-
cdef object _opcode
64-
cdef object _frame_fin
65-
cdef object _frame_opcode
68+
cdef int _opcode
69+
cdef bint _frame_fin
70+
cdef int _frame_opcode
6671
cdef object _frame_payload
6772
cdef unsigned long long _frame_payload_len
6873

@@ -71,7 +76,7 @@ cdef class WebSocketReader:
7176
cdef bytes _frame_mask
7277
cdef unsigned long long _payload_length
7378
cdef unsigned int _payload_length_flag
74-
cdef object _compressed
79+
cdef int _compressed
7580
cdef object _decompressobj
7681
cdef bint _compress
7782

@@ -82,22 +87,21 @@ cdef class WebSocketReader:
8287
fin=bint,
8388
has_partial=bint,
8489
payload_merged=bytes,
85-
opcode="unsigned int",
8690
)
87-
cpdef void _feed_data(self, bytes data)
91+
cpdef void _handle_frame(self, bint fin, int opcode, object payload, int compressed) except *
8892

8993
@cython.locals(
9094
start_pos="unsigned int",
91-
buf_len="unsigned int",
95+
data_len="unsigned int",
9296
length="unsigned int",
9397
chunk_size="unsigned int",
9498
chunk_len="unsigned int",
95-
buf_length="unsigned int",
96-
buf_cstr="const unsigned char *",
99+
data_length="unsigned int",
100+
data_cstr="const unsigned char *",
97101
first_byte="unsigned char",
98102
second_byte="unsigned char",
99103
end_pos="unsigned int",
100104
has_mask=bint,
101105
fin=bint,
102106
)
103-
cpdef list parse_frame(self, bytes buf)
107+
cpdef void _feed_data(self, bytes data) except *

0 commit comments

Comments
 (0)