Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to specify Packer's internal buf_size #547

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions msgpack/_packer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ cdef class Packer(object):
:param str unicode_errors:
The error handler for encoding unicode. (default: 'strict')
DO NOT USE THIS!! This option is kept for very specific usage.

:param int buf_size:
The size of the internal buffer. (default: 1024 * 1024)
Useful if serialisation size can be correctly estimated,
avoid unnecessary reallocations.
"""
cdef msgpack_packer pk
cdef object _default
Expand All @@ -112,8 +117,7 @@ cdef class Packer(object):
cdef bint autoreset
cdef bint datetime

def __cinit__(self):
cdef int buf_size = 1024*1024
def __cinit__(self, buf_size=1024*1024, **_kwargs):
self.pk.buf = <char*> PyMem_Malloc(buf_size)
if self.pk.buf == NULL:
raise MemoryError("Unable to allocate internal buffer.")
Expand All @@ -122,7 +126,7 @@ cdef class Packer(object):

def __init__(self, *, default=None,
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=1024*1024):
self.use_float = use_single_float
self.strict_types = strict_types
self.autoreset = autoreset
Expand Down