mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
Revert gzip readline performance patch #1281707 until a more generic performance improvement can be found
This commit is contained in:
parent
d72aab5e31
commit
b97597316b
1 changed files with 19 additions and 27 deletions
46
Lib/gzip.py
46
Lib/gzip.py
|
@ -107,7 +107,6 @@ class GzipFile:
|
||||||
self.extrabuf = ""
|
self.extrabuf = ""
|
||||||
self.extrasize = 0
|
self.extrasize = 0
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.min_readsize = 64 # Starts small, scales exponentially
|
|
||||||
|
|
||||||
elif mode[0:1] == 'w' or mode[0:1] == 'a':
|
elif mode[0:1] == 'w' or mode[0:1] == 'a':
|
||||||
self.mode = WRITE
|
self.mode = WRITE
|
||||||
|
@ -382,39 +381,32 @@ class GzipFile:
|
||||||
self.read(count % 1024)
|
self.read(count % 1024)
|
||||||
|
|
||||||
def readline(self, size=-1):
|
def readline(self, size=-1):
|
||||||
if size < 0:
|
if size < 0: size = sys.maxint
|
||||||
size = sys.maxint # Line can be as long as maxint
|
bufs = []
|
||||||
readsize = self.min_readsize # Read from file in small chunks
|
readsize = min(100, size) # Read from the file in small chunks
|
||||||
else:
|
|
||||||
readsize = size # Only read in as much as specified
|
|
||||||
|
|
||||||
bufs = ""
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if size == 0: return bufs # Return line (reached max len)
|
if size == 0:
|
||||||
|
return "".join(bufs) # Return resulting line
|
||||||
|
|
||||||
c = self.read(readsize)
|
c = self.read(readsize)
|
||||||
i = c.find('\n')
|
i = c.find('\n')
|
||||||
|
if size is not None:
|
||||||
|
# We set i=size to break out of the loop under two
|
||||||
|
# conditions: 1) there's no newline, and the chunk is
|
||||||
|
# larger than size, or 2) there is a newline, but the
|
||||||
|
# resulting line would be longer than 'size'.
|
||||||
|
if i==-1 and len(c) > size: i=size-1
|
||||||
|
elif size <= i: i = size -1
|
||||||
|
|
||||||
# If there is a newline, or the string is empty
|
|
||||||
if i >= 0 or c == '':
|
if i >= 0 or c == '':
|
||||||
if size <= i: i = size - 1 # Another larger than size check
|
bufs.append(c[:i+1]) # Add portion of last chunk
|
||||||
|
self._unread(c[i+1:]) # Push back rest of chunk
|
||||||
|
return ''.join(bufs) # Return resulting line
|
||||||
|
|
||||||
self._unread(c[i+1:]) # Push back rest of chunk
|
# Append chunk to list, decrease 'size',
|
||||||
|
bufs.append(c)
|
||||||
return bufs + c[:i+1] # Stored line, plus new segment
|
size = size - len(c)
|
||||||
|
readsize = min(size, readsize * 2)
|
||||||
# If there is no newline
|
|
||||||
else:
|
|
||||||
if len(c) > size: i = size - 1 # If lineis larger than size
|
|
||||||
|
|
||||||
bufs = bufs + c
|
|
||||||
size = size - len(c)
|
|
||||||
readsize = min(size, int(readsize * 1.1))
|
|
||||||
|
|
||||||
# Optimize future readline() calls
|
|
||||||
if readsize > self.min_readsize:
|
|
||||||
self.min_readsize = readsize
|
|
||||||
|
|
||||||
def readlines(self, sizehint=0):
|
def readlines(self, sizehint=0):
|
||||||
# Negative numbers result in reading all the lines
|
# Negative numbers result in reading all the lines
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue