Perf: Optimize non-seekable stream buffering using shutil.copyfileobj #1510
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
Optimize buffering of non-seekable streams by replacing the manual chunk reading loop with
shutil.copyfileobj(). The proposed method is more memory efficient with fewer buffer reallocations, all while maintaining the existing buffer growth pattern. Proposed code changes enhance code readability and maintainability.Changes
convert.stream()method, replace thewhile Truestatement withshutil.copyfileobj(),shutil.copyfileobj()uses a larger internal buffer size of 64KB instead of the current 4KBshutil.copyfileobj()leverages a C-optimized implemented, reducing Python bytecode overheadExample Performance Enhancement
For a 10MB non-seekable stream: