Skip to content

Conversation

@mfulgo
Copy link

@mfulgo mfulgo commented Feb 4, 2025

In the previous implementation of iter_lines, lines that spanned chunks could cause a quadratic iteration of the line data. This could result in a much slower read time for files with long lines or lines significantly longer than the chunk size used when reading them.

To avoid the issue, the StreamingBody now collects the line data and only constructs the line (copying the bytes into a new buffer) when it encounters a line ending.

Closes #2774

In the previous implementation of iter_lines, lines that spanned chunks
could cause a quadratic iteration of the line data. This could result in
a much slower read time for files with long lines or lines significantly
longer than the chunk size used when reading them.

To avoid the issue, the StreamingBody now collects the line data and
only constructs the line (copying the bytes into a new buffer) when it
encounters a line ending.

Closes boto#2774
@mfulgo
Copy link
Author

mfulgo commented Feb 4, 2025

I wasn't quite sure how you all might want a performance test written for this, so I just added and ran the following unit test before and after the change:

    def test_streaming_line_iterator_long_lines(self):
        line = b'1234567890'*1000000
        body = itertools.repeat(line, 1)
        stream = response.StreamingBody(BytesIO(b'\n'.join(body)), content_length=(1*10*1000000)+1-1)
        start = perf_counter()
        for l in stream.iter_lines():
            self.assertEqual(l, line)
        raise RuntimeError(f"It took {perf_counter()-start:.3f} seconds")

Prior to this commit, it took around 23 seconds to run. After the change, it consistently ran in about 0.009 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Very slow performance when using iter_lines on an s3 object with long lines

1 participant