mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-19 20:03:16 +00:00
packer: add buf_size
option (#604)
And change the default buffer size to 256KiB. Signed-off-by: Rodrigo Tobar <rtobar@icrar.org> Co-authored-by: Rodrigo Tobar <rtobar@icrar.org>
This commit is contained in:
parent
bf2413f915
commit
72e65feb0e
3 changed files with 15 additions and 27 deletions
|
@ -53,7 +53,7 @@ cdef inline int PyBytesLike_CheckExact(object o):
|
||||||
return PyBytes_CheckExact(o) or PyByteArray_CheckExact(o)
|
return PyBytes_CheckExact(o) or PyByteArray_CheckExact(o)
|
||||||
|
|
||||||
|
|
||||||
cdef class Packer(object):
|
cdef class Packer:
|
||||||
"""
|
"""
|
||||||
MessagePack Packer
|
MessagePack Packer
|
||||||
|
|
||||||
|
@ -97,6 +97,11 @@ cdef class Packer(object):
|
||||||
:param str unicode_errors:
|
:param str unicode_errors:
|
||||||
The error handler for encoding unicode. (default: 'strict')
|
The error handler for encoding unicode. (default: 'strict')
|
||||||
DO NOT USE THIS!! This option is kept for very specific usage.
|
DO NOT USE THIS!! This option is kept for very specific usage.
|
||||||
|
|
||||||
|
:param int buf_size:
|
||||||
|
The size of the internal buffer. (default: 256*1024)
|
||||||
|
Useful if serialisation size can be correctly estimated,
|
||||||
|
avoid unnecessary reallocations.
|
||||||
"""
|
"""
|
||||||
cdef msgpack_packer pk
|
cdef msgpack_packer pk
|
||||||
cdef object _default
|
cdef object _default
|
||||||
|
@ -107,8 +112,7 @@ cdef class Packer(object):
|
||||||
cdef bint autoreset
|
cdef bint autoreset
|
||||||
cdef bint datetime
|
cdef bint datetime
|
||||||
|
|
||||||
def __cinit__(self):
|
def __cinit__(self, buf_size=256*1024, **_kwargs):
|
||||||
cdef int buf_size = 1024*1024
|
|
||||||
self.pk.buf = <char*> PyMem_Malloc(buf_size)
|
self.pk.buf = <char*> PyMem_Malloc(buf_size)
|
||||||
if self.pk.buf == NULL:
|
if self.pk.buf == NULL:
|
||||||
raise MemoryError("Unable to allocate internal buffer.")
|
raise MemoryError("Unable to allocate internal buffer.")
|
||||||
|
@ -117,7 +121,8 @@ cdef class Packer(object):
|
||||||
|
|
||||||
def __init__(self, *, default=None,
|
def __init__(self, *, default=None,
|
||||||
bint use_single_float=False, bint autoreset=True, bint use_bin_type=True,
|
bint use_single_float=False, bint autoreset=True, bint use_bin_type=True,
|
||||||
bint strict_types=False, bint datetime=False, unicode_errors=None):
|
bint strict_types=False, bint datetime=False, unicode_errors=None,
|
||||||
|
buf_size=256*1024):
|
||||||
self.use_float = use_single_float
|
self.use_float = use_single_float
|
||||||
self.strict_types = strict_types
|
self.strict_types = strict_types
|
||||||
self.autoreset = autoreset
|
self.autoreset = autoreset
|
||||||
|
|
|
@ -210,7 +210,7 @@ def unpackb(object packed, *, object object_hook=None, object list_hook=None,
|
||||||
raise ValueError("Unpack failed: error = %d" % (ret,))
|
raise ValueError("Unpack failed: error = %d" % (ret,))
|
||||||
|
|
||||||
|
|
||||||
cdef class Unpacker(object):
|
cdef class Unpacker:
|
||||||
"""Streaming unpacker.
|
"""Streaming unpacker.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
|
|
|
@ -232,6 +232,7 @@ class Unpacker:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
file_like=None,
|
file_like=None,
|
||||||
|
*,
|
||||||
read_size=0,
|
read_size=0,
|
||||||
use_list=True,
|
use_list=True,
|
||||||
raw=False,
|
raw=False,
|
||||||
|
@ -650,32 +651,13 @@ class Packer:
|
||||||
The error handler for encoding unicode. (default: 'strict')
|
The error handler for encoding unicode. (default: 'strict')
|
||||||
DO NOT USE THIS!! This option is kept for very specific usage.
|
DO NOT USE THIS!! This option is kept for very specific usage.
|
||||||
|
|
||||||
Example of streaming deserialize from file-like object::
|
:param int buf_size:
|
||||||
|
Internal buffer size. This option is used only for C implementation.
|
||||||
unpacker = Unpacker(file_like)
|
|
||||||
for o in unpacker:
|
|
||||||
process(o)
|
|
||||||
|
|
||||||
Example of streaming deserialize from socket::
|
|
||||||
|
|
||||||
unpacker = Unpacker()
|
|
||||||
while True:
|
|
||||||
buf = sock.recv(1024**2)
|
|
||||||
if not buf:
|
|
||||||
break
|
|
||||||
unpacker.feed(buf)
|
|
||||||
for o in unpacker:
|
|
||||||
process(o)
|
|
||||||
|
|
||||||
Raises ``ExtraData`` when *packed* contains extra bytes.
|
|
||||||
Raises ``OutOfData`` when *packed* is incomplete.
|
|
||||||
Raises ``FormatError`` when *packed* is not valid msgpack.
|
|
||||||
Raises ``StackError`` when *packed* contains too nested.
|
|
||||||
Other exceptions can be raised during unpacking.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
*,
|
||||||
default=None,
|
default=None,
|
||||||
use_single_float=False,
|
use_single_float=False,
|
||||||
autoreset=True,
|
autoreset=True,
|
||||||
|
@ -683,6 +665,7 @@ class Packer:
|
||||||
strict_types=False,
|
strict_types=False,
|
||||||
datetime=False,
|
datetime=False,
|
||||||
unicode_errors=None,
|
unicode_errors=None,
|
||||||
|
buf_size=None,
|
||||||
):
|
):
|
||||||
self._strict_types = strict_types
|
self._strict_types = strict_types
|
||||||
self._use_float = use_single_float
|
self._use_float = use_single_float
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue