Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make BytesQueueBuffer.get_all more memory efficient #3236

Merged
merged 2 commits into from
Dec 19, 2023

Conversation

illia-v
Copy link
Member

@illia-v illia-v commented Dec 15, 2023

This PR adds memray tests for functionality added in #3186 and improves memory efficiency of BytesQueueBuffer.get_all.

@illia-v illia-v added the Skip Changelog Pull requests that don't require a changelog entry label Dec 15, 2023
Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests are great, thanks! I would be interested in seeing proof that the get_all change makes a difference.

Comment on lines -294 to +296
result = b"".join(buffer)
buffer.clear()
ret = io.BytesIO()
ret.writelines(buffer.popleft() for _ in range(len(buffer)))
result = ret.getvalue()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you able to measure the memory gain? I would expect str.join to do exactly that under the hood. If it made a difference, the surely we could lower some of the pytest.mark.limit_memory decorators?

Copy link
Member Author

@illia-v illia-v Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With b"".join(buffer) there was a moment when result took memory for all the data, but buffer has not been cleared yet and it took the same amount of memory additionally. So test_memory_usage reported around 20 MB instead of around 12 MB (5 * 2 MiB chunks + 2 MiB for overhead from the last chunk.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks. I guess I was surprised that no other test showed a memory usage improvement!

Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM.

@illia-v
Copy link
Member Author

illia-v commented Dec 19, 2023

@pquentin thanks for your review!

@illia-v illia-v merged commit 8359450 into urllib3:main Dec 19, 2023
35 of 37 checks passed
@illia-v illia-v deleted the read1-mem-tests branch December 19, 2023 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Skip Changelog Pull requests that don't require a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants