@@ -216,23 +216,22 @@ def write(self, data):
216216 else :
217217 return self .file_write (data )
218218
219- def write_many (self , * chunks ):
220- total_size = sum (len (c ) for c in chunks )
221- if self .current_frame and total_size >= self ._FRAME_SIZE_TARGET :
219+ def write_large (self , header , payload ):
220+ if len (payload ) >= self ._FRAME_SIZE_TARGET and self .current_frame :
222221 # Terminate the current frame to write the next frame directly into
223222 # the underlying file to skip the unnecessary memory allocations
224223 # of a large temporary buffer.
225224 self .commit_frame (force = True )
226225 write = self .file_write
227- write (FRAME + pack ("<Q" , total_size ))
228- else :
229- write = self .write
226+ write (FRAME + pack ("<Q" , len (header ) + len (payload )))
230227
231- # Be careful to not concatenate the chunks prior to calling 'write' as
232- # some chunks (typically the last of the list) can be very large and we
233- # do not want to allocate a large temporary bytes object.
234- for chunk in chunks :
235- write (chunk )
228+ # Be careful to not concatenate the header and the payload prior to
229+ # calling 'write' as do not want to allocate a large temporary
230+ # bytes object.
231+ write (header )
232+ write (payload )
233+ else :
234+ self .write (header + payload )
236235
237236
238237class _Unframer :
@@ -397,7 +396,7 @@ def __init__(self, file, protocol=None, *, fix_imports=True):
397396 raise TypeError ("file must have a 'write' attribute" )
398397 self .framer = _Framer (self ._file_write )
399398 self .write = self .framer .write
400- self ._write_many = self .framer .write_many
399+ self ._write_large = self .framer .write_large
401400 self .memo = {}
402401 self .proto = int (protocol )
403402 self .bin = protocol >= 1
@@ -715,9 +714,9 @@ def save_bytes(self, obj):
715714 if n <= 0xff :
716715 self .write (SHORT_BINBYTES + pack ("<B" , n ) + obj )
717716 elif n > 0xffffffff and self .proto >= 4 :
718- self ._write_many (BINBYTES8 + pack ("<Q" , n ), obj )
717+ self ._write_large (BINBYTES8 + pack ("<Q" , n ), obj )
719718 else :
720- self ._write_many (BINBYTES + pack ("<I" , n ), obj )
719+ self ._write_large (BINBYTES + pack ("<I" , n ), obj )
721720 self .memoize (obj )
722721 dispatch [bytes ] = save_bytes
723722
@@ -728,9 +727,9 @@ def save_str(self, obj):
728727 if n <= 0xff and self .proto >= 4 :
729728 self .write (SHORT_BINUNICODE + pack ("<B" , n ) + encoded )
730729 elif n > 0xffffffff and self .proto >= 4 :
731- self ._write_many (BINUNICODE8 + pack ("<Q" , n ), encoded )
730+ self ._write_large (BINUNICODE8 + pack ("<Q" , n ), encoded )
732731 else :
733- self ._write_many (BINUNICODE + pack ("<I" , n ), encoded )
732+ self ._write_large (BINUNICODE + pack ("<I" , n ), encoded )
734733 else :
735734 obj = obj .replace ("\\ " , "\\ u005c" )
736735 obj = obj .replace ("\n " , "\\ u000a" )
0 commit comments