bpo-32503: Avoid creating too small frames in pickles. (#5127)

This commit is contained in:
Serhiy Storchaka 2018-01-20 16:42:44 +02:00 committed by GitHub
parent bd5c7d238c
commit 1211c9a989
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 50 deletions

View file

@ -119,8 +119,8 @@ enum {
/* Prefetch size when unpickling (disabled on unpeekable streams) */
PREFETCH = 8192 * 16,
FRAME_SIZE_MIN = 4,
FRAME_SIZE_TARGET = 64 * 1024,
FRAME_HEADER_SIZE = 9
};
@ -949,13 +949,6 @@ _write_size64(char *out, size_t value)
}
}
static void
_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
{
qdata[0] = FRAME;
_write_size64(qdata + 1, frame_len);
}
static int
_Pickler_CommitFrame(PicklerObject *self)
{
@ -966,7 +959,14 @@ _Pickler_CommitFrame(PicklerObject *self)
return 0;
frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
_Pickler_WriteFrameHeader(self, qdata, frame_len);
if (frame_len >= FRAME_SIZE_MIN) {
qdata[0] = FRAME;
_write_size64(qdata + 1, frame_len);
}
else {
memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len);
self->output_len -= FRAME_HEADER_SIZE;
}
self->frame_start = -1;
return 0;
}