@@ -53,7 +53,7 @@ cdef inline int PyBytesLike_CheckExact(object o):
5353 return PyBytes_CheckExact(o) or PyByteArray_CheckExact(o)
5454
5555
56- cdef class Packer( object ) :
56+ cdef class Packer:
5757 """
5858 MessagePack Packer
5959
@@ -97,6 +97,11 @@ cdef class Packer(object):
9797 :param str unicode_errors:
9898 The error handler for encoding unicode. (default: 'strict')
9999 DO NOT USE THIS!! This option is kept for very specific usage.
100+
101+ :param int buf_size:
102+ The size of the internal buffer. (default: 256*1024)
103+ Useful if serialisation size can be correctly estimated,
104+ avoid unnecessary reallocations.
100105 """
101106 cdef msgpack_packer pk
102107 cdef object _default
@@ -107,8 +112,7 @@ cdef class Packer(object):
107112 cdef bint autoreset
108113 cdef bint datetime
109114
110- def __cinit__ (self ):
111- cdef int buf_size = 1024 * 1024
115+ def __cinit__ (self , buf_size = 256 * 1024 , **_kwargs ):
112116 self .pk.buf = < char * > PyMem_Malloc(buf_size)
113117 if self .pk.buf == NULL :
114118 raise MemoryError (" Unable to allocate internal buffer." )
@@ -117,7 +121,8 @@ cdef class Packer(object):
117121
118122 def __init__ (self , *, default = None ,
119123 bint use_single_float = False , bint autoreset = True , bint use_bin_type = True ,
120- bint strict_types = False , bint datetime = False , unicode_errors = None ):
124+ bint strict_types = False , bint datetime = False , unicode_errors = None ,
125+ buf_size = 256 * 1024 ):
121126 self .use_float = use_single_float
122127 self .strict_types = strict_types
123128 self .autoreset = autoreset
0 commit comments