Skip to content

Commit 72e65fe

Browse files
methanertobar
andauthored
packer: add buf_size option (#604)
And change the default buffer size to 256KiB. Signed-off-by: Rodrigo Tobar <[email protected]> Co-authored-by: Rodrigo Tobar <[email protected]>
1 parent bf2413f commit 72e65fe

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

msgpack/_packer.pyx

+9-4
Original file line numberDiff line numberDiff line change
@@ -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

msgpack/_unpacker.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def unpackb(object packed, *, object object_hook=None, object list_hook=None,
210210
raise ValueError("Unpack failed: error = %d" % (ret,))
211211

212212

213-
cdef class Unpacker(object):
213+
cdef class Unpacker:
214214
"""Streaming unpacker.
215215
216216
Arguments:

msgpack/fallback.py

+5-22
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ class Unpacker:
232232
def __init__(
233233
self,
234234
file_like=None,
235+
*,
235236
read_size=0,
236237
use_list=True,
237238
raw=False,
@@ -650,39 +651,21 @@ class Packer:
650651
The error handler for encoding unicode. (default: 'strict')
651652
DO NOT USE THIS!! This option is kept for very specific usage.
652653
653-
Example of streaming deserialize from file-like object::
654-
655-
unpacker = Unpacker(file_like)
656-
for o in unpacker:
657-
process(o)
658-
659-
Example of streaming deserialize from socket::
660-
661-
unpacker = Unpacker()
662-
while True:
663-
buf = sock.recv(1024**2)
664-
if not buf:
665-
break
666-
unpacker.feed(buf)
667-
for o in unpacker:
668-
process(o)
669-
670-
Raises ``ExtraData`` when *packed* contains extra bytes.
671-
Raises ``OutOfData`` when *packed* is incomplete.
672-
Raises ``FormatError`` when *packed* is not valid msgpack.
673-
Raises ``StackError`` when *packed* contains too nested.
674-
Other exceptions can be raised during unpacking.
654+
:param int buf_size:
655+
Internal buffer size. This option is used only for C implementation.
675656
"""
676657

677658
def __init__(
678659
self,
660+
*,
679661
default=None,
680662
use_single_float=False,
681663
autoreset=True,
682664
use_bin_type=True,
683665
strict_types=False,
684666
datetime=False,
685667
unicode_errors=None,
668+
buf_size=None,
686669
):
687670
self._strict_types = strict_types
688671
self._use_float = use_single_float

0 commit comments

Comments
 (0)