Skip to content

Commit

Permalink
Optimize _encode_multipart_message (#75)
Browse files Browse the repository at this point in the history
* Optimize _encode_multipart_message

By using BytesIO while generating the body instead of continuous string
concatenations.

This drastically reduces execution time by not recreating the string for
each concatenation.

In one case, execution time fell from 140 seconds to 0.14; three orders
of magnitude!

* Fix comment about default chunk_size in MB
  • Loading branch information
dimrozakis authored Nov 13, 2022
1 parent a59af6e commit d7b1552
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/dicomweb_client/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def __init__(
Maximum number of bytes that should be transferred per data chunk
when streaming data from the server using chunked transfer encoding
(used by ``iter_*()`` methods as well as the ``store_instances()``
method); defaults to ``10**6`` bytes (10MB)
method); defaults to ``10**6`` bytes (1MB)
Warning
-------
Expand Down Expand Up @@ -743,13 +743,15 @@ def _encode_multipart_message(
raise ValueError(
'No "boundary" parameter in found in content-type field'
)
body = b''
for part in content:
body += f'\r\n--{boundary}'.encode('utf-8')
body += f'\r\nContent-Type: {content_type}\r\n\r\n'.encode('utf-8')
body += part
body += f'\r\n--{boundary}--'.encode('utf-8')
return body
with BytesIO() as b:
for part in content:
b.write(f'\r\n--{boundary}'.encode('utf-8'))
b.write(
f'\r\nContent-Type: {content_type}\r\n\r\n'.encode('utf-8')
)
b.write(part)
b.write(f'\r\n--{boundary}--'.encode('utf-8'))
return b.getvalue()

@classmethod
def _assert_media_type_is_valid(cls, media_type: str):
Expand Down

0 comments on commit d7b1552

Please sign in to comment.