summaryrefslogtreecommitdiff
path: root/Lib/pickle.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-01-20 16:42:44 +0200
committerGitHub <noreply@github.com>2018-01-20 16:42:44 +0200
commit1211c9a9897a174b7261ca258cabf289815a40d8 (patch)
treea43814a9172d44d64d7427d10249c816e5f4ef0e /Lib/pickle.py
parentbd5c7d238c01b90fbfae8ea45b47bd601900abaf (diff)
downloadcpython-git-1211c9a9897a174b7261ca258cabf289815a40d8.tar.gz
bpo-32503: Avoid creating too small frames in pickles. (#5127)
Diffstat (limited to 'Lib/pickle.py')
-rw-r--r--Lib/pickle.py12
1 files changed, 7 insertions, 5 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index 301e8cf558..e6d003787b 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -183,6 +183,7 @@ __all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$", x)])
class _Framer:
+ _FRAME_SIZE_MIN = 4
_FRAME_SIZE_TARGET = 64 * 1024
def __init__(self, file_write):
@@ -203,11 +204,12 @@ class _Framer:
if f.tell() >= self._FRAME_SIZE_TARGET or force:
data = f.getbuffer()
write = self.file_write
- # Issue a single call to the write method of the underlying
- # file object for the frame opcode with the size of the
- # frame. The concatenation is expected to be less expensive
- # than issuing an additional call to write.
- write(FRAME + pack("<Q", len(data)))
+ if len(data) >= self._FRAME_SIZE_MIN:
+ # Issue a single call to the write method of the underlying
+ # file object for the frame opcode with the size of the
+ # frame. The concatenation is expected to be less expensive
+ # than issuing an additional call to write.
+ write(FRAME + pack("<Q", len(data)))
# Issue a separate call to write to append the frame
# contents without concatenation to the above to avoid a