diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2013-02-16 12:08:14 -0800 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2013-02-16 12:08:14 -0800 |
commit | 3f12846d40e035573993cc9c5195bf3607ee98bb (patch) | |
tree | 04da2863d58da9ba384f5e4e92bbe310cb02c639 | |
parent | 626ae51017cdbf62a2c7b00b338747d1b2070513 (diff) | |
download | msgpack-python-3f12846d40e035573993cc9c5195bf3607ee98bb.tar.gz |
On PyPy, preallocate lists
When deserealizing arrays, preallocate the resulting list at the correct size.
-rw-r--r-- | msgpack/fallback.py | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/msgpack/fallback.py b/msgpack/fallback.py index a834229..0016bd1 100644 --- a/msgpack/fallback.py +++ b/msgpack/fallback.py @@ -22,6 +22,7 @@ else: if hasattr(sys, 'pypy_version_info'): # cStringIO is slow on PyPy, StringIO is faster. However: PyPy's own # StringBuilder is fastest. + from __pypy__ import newlist_hint from __pypy__.builders import StringBuilder USING_STRINGBUILDER = True class StringIO(object): @@ -38,6 +39,7 @@ if hasattr(sys, 'pypy_version_info'): else: USING_STRINGBUILDER = False from io import BytesIO as StringIO + newlist_hint = lambda size: [] from msgpack.exceptions import ( BufferFull, @@ -346,7 +348,7 @@ class Unpacker(object): # TODO check whether we need to call `list_hook` self._fb_unpack(EX_SKIP, write_bytes) return - ret = [] + ret = newlist_hint(n) for i in xrange(n): ret.append(self._fb_unpack(EX_CONSTRUCT, write_bytes)) if self._list_hook is not None: |