From 89be0081aa3397d011c26ce2f6f349b605bf3fea Mon Sep 17 00:00:00 2001 From: Igor Solovey Date: Wed, 11 Sep 2024 16:39:31 -0400 Subject: [PATCH 1/2] Optimize multipart message decoding Avoid repeated truncation/reallocation of a bytes array, and expensive call to split() --- src/dicomweb_client/web.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/dicomweb_client/web.py b/src/dicomweb_client/web.py index 3b305a2..c420169 100644 --- a/src/dicomweb_client/web.py +++ b/src/dicomweb_client/web.py @@ -673,7 +673,7 @@ def _decode_multipart_message( marker = b''.join((b'--', boundary)) delimiter = b''.join((b'\r\n', marker)) - data = b'' + data = bytearray() j = 0 with response: logger.debug('decode message content') @@ -685,16 +685,23 @@ def _decode_multipart_message( if stream: logger.debug(f'decode message content chunk #{i}') data += chunk - while delimiter in data: + + prev_part_index = 0 + while ( + delimiter_index := data.find(delimiter, prev_part_index) + ) >= 0: logger.debug(f'decode message part #{j}') - part, data = data.split(delimiter, maxsplit=1) - content = self._extract_part_content(part) + content = self._extract_part_content( + data[prev_part_index:delimiter_index] + ) + prev_part_index = delimiter_index + len(delimiter) j += 1 if content is not None: logger.debug( f'extracted {len(content)} bytes from part #{j}' ) yield content + data = data[prev_part_index:] content = self._extract_part_content(data) if content is not None: From f0b58c1a7ca97fc7ef3c9af5ebe1953be55c4012 Mon Sep 17 00:00:00 2001 From: Chris Bridge Date: Sat, 19 Oct 2024 13:57:58 -0400 Subject: [PATCH 2/2] Remove walrus operator --- src/dicomweb_client/web.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/dicomweb_client/web.py b/src/dicomweb_client/web.py index c420169..d7b96d2 100644 --- a/src/dicomweb_client/web.py +++ b/src/dicomweb_client/web.py @@ -687,9 +687,10 @@ def _decode_multipart_message( data += chunk prev_part_index = 0 - while ( - delimiter_index := data.find(delimiter, prev_part_index) - ) >= 0: + while True: + delimiter_index = data.find(delimiter, prev_part_index) + if delimiter_index < 0: + break logger.debug(f'decode message part #{j}') content = self._extract_part_content( data[prev_part_index:delimiter_index]