Repository navigation
GzipFile.write should be buffered #89550
Description
Activity
rhpvorderman commented
on Oct 6, 2021 rhpvordermanmannequinMannequinAuthorMore actionsPlease consider the following code snippet:
import gzip import sys with gzip.open(sys.argv[1], "rt") as in_file_h: with gzip.open(sys.argv[2], "wt", compresslevel=1) as out_file_h: for line in in_file_h: # Do processing on line here modified_line = line # End processing out_file_h.write(modified_line)
This is very slow, due to write being called for every line. This is the current implementation of write:
Line 272 in c379bc5
def write(self,data): It:
- Checks if the file is not closed
- Checks if the correct mode is set
- Checks if the file is not closed (again, but in a different way)
- Checks if the data is bytes, bytearray or something that supports the buffer protocol
- Gets the length
- Compresses the data
- updates the size and offset
- updates the checksum
Doing this for every line written is very costly and creates a lot of overhead in Python calls. We spent a lot of time in Python and a lot less in the fast C zlib code that does the actual compression.
This problem is already solved on the read side. A _GzipReader object is used for reading. This is put in an io.BufferedReader which is used as the underlying buffer for GzipFile.read. This way, lines are read quite fast from a GzipFile without the checksum etc. being updated on every line read.
A similar solution should be written for write.
I volunteer (I have done some other work on gzip.py already), although I cannot give an ETA at this time.- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory3.7 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of life3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usage
on Oct 6, 2021 9 remaining items
- added a commit that references this issue
on May 8, 2023 A PR has been merged, thanks for the report!
- added a commit that references this issue
on Feb 12, 2025 - added a commit that references this issue
on Feb 28, 2025
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs