feat: optimize unmarshaller by dropping exception that was only used internally (#145)

This commit is contained in:
J. Nick Koston
2022-11-04 00:10:29 +01:00
committed by GitHub
parent f1668264bc
commit 79d52a50bd
2 changed files with 11 additions and 26 deletions

View File

@@ -71,6 +71,7 @@ cdef unsigned int TOKEN_O_AS_INT
cdef unsigned int TOKEN_S_AS_INT cdef unsigned int TOKEN_S_AS_INT
cdef unsigned int TOKEN_G_AS_INT cdef unsigned int TOKEN_G_AS_INT
cdef object MARSHALL_STREAM_END_ERROR
cpdef get_signature_tree cpdef get_signature_tree
@@ -87,8 +88,6 @@ cdef inline unsigned short _cast_uint16_native(const char * payload, unsigned i
return u16p[0] return u16p[0]
cdef class MarshallerStreamEndError(Exception):
pass
cdef class Unmarshaller: cdef class Unmarshaller:

View File

@@ -109,6 +109,8 @@ HEADER_MESSAGE_ARG_NAME = {
READER_TYPE = Callable[["Unmarshaller", SignatureType], Any] READER_TYPE = Callable[["Unmarshaller", SignatureType], Any]
MARSHALL_STREAM_END_ERROR = BlockingIOError
def unpack_parser_factory(unpack_from: Callable, size: int) -> READER_TYPE: def unpack_parser_factory(unpack_from: Callable, size: int) -> READER_TYPE:
"""Build a parser that unpacks the bytes using the given unpack_from function.""" """Build a parser that unpacks the bytes using the given unpack_from function."""
@@ -134,17 +136,6 @@ def build_simple_parsers(
return parsers return parsers
class MarshallerStreamEndError(Exception):
"""This exception is raised when the end of the stream is reached.
This means more data is expected on the wire that has not yet been
received. The caller should call unmarshall later when more data is
available.
"""
pass
try: try:
import cython import cython
except ImportError: except ImportError:
@@ -235,14 +226,9 @@ class Unmarshaller:
def _read_sock(self, length: int) -> bytes: def _read_sock(self, length: int) -> bytes:
"""reads from the socket, storing any fds sent and handling errors """reads from the socket, storing any fds sent and handling errors
from the read itself""" from the read itself"""
# This will raise BlockingIOError if there is no data to read
try: # which we store in the MARSHALL_STREAM_END_ERROR object
msg, ancdata, _flags, _addr = self._sock.recvmsg( msg, ancdata, _flags, _addr = self._sock.recvmsg(length, UNIX_FDS_CMSG_LENGTH)
length, UNIX_FDS_CMSG_LENGTH
)
except BlockingIOError:
raise MarshallerStreamEndError()
for level, type_, data in ancdata: for level, type_, data in ancdata:
if not (level == SOL_SOCKET and type_ == SCM_RIGHTS): if not (level == SOL_SOCKET and type_ == SCM_RIGHTS):
continue continue
@@ -256,7 +242,7 @@ class Unmarshaller:
""" """
Read from underlying socket into buffer. Read from underlying socket into buffer.
Raises MarshallerStreamEndError if there is not enough data to be read. Raises BlockingIOError if there is not enough data to be read.
:arg pos: :arg pos:
The pos to read to. If not enough bytes are available in the The pos to read to. If not enough bytes are available in the
@@ -274,10 +260,10 @@ class Unmarshaller:
if data == b"": if data == b"":
raise EOFError() raise EOFError()
if data is None: if data is None:
raise MarshallerStreamEndError() raise MARSHALL_STREAM_END_ERROR
self._buf += data self._buf += data
if len(data) + start_len != pos: if len(data) + start_len != pos:
raise MarshallerStreamEndError() raise MARSHALL_STREAM_END_ERROR
def read_uint32_unpack(self, type_: SignatureType) -> int: def read_uint32_unpack(self, type_: SignatureType) -> int:
return self._read_uint32_unpack() return self._read_uint32_unpack()
@@ -610,7 +596,7 @@ class Unmarshaller:
def unmarshall(self) -> Optional[Message]: def unmarshall(self) -> Optional[Message]:
"""Unmarshall the message. """Unmarshall the message.
The underlying read function will raise MarshallerStreamEndError The underlying read function will raise BlockingIOError if the
if there are not enough bytes in the buffer. This allows unmarshall if there are not enough bytes in the buffer. This allows unmarshall
to be resumed when more data comes in over the wire. to be resumed when more data comes in over the wire.
""" """
@@ -618,7 +604,7 @@ class Unmarshaller:
if not self._msg_len: if not self._msg_len:
self._read_header() self._read_header()
self._read_body() self._read_body()
except MarshallerStreamEndError: except MARSHALL_STREAM_END_ERROR:
return None return None
return self._message return self._message