Skip to content

Commit c3942c3

Browse files
author
andrew.kuchling
committed
#1792: Improve performance of marshal.dumps() on large objects by increasing
the size of the buffer more quickly. git-svn-id: http://svn.python.org/projects/python/trunk@63059 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 5ce4468 commit c3942c3

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lib/test/test_marshal.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ def test_exact_type_match(self):
255255
subtyp = type('subtyp', (typ,), {})
256256
self.assertRaises(ValueError, marshal.dumps, subtyp())
257257

258+
# Issue #1792 introduced a change in how marshal increases the size of its
259+
# internal buffer; this test ensures that the new code is exercised.
260+
def test_large_marshal(self):
261+
size = int(1e6)
262+
testString = 'abc' * size
263+
marshal.dumps(testString)
264+
265+
258266
def test_main():
259267
test_support.run_unittest(IntTestCase,
260268
FloatTestCase,

Python/marshal.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ w_more(int c, WFILE *p)
6565
if (p->str == NULL)
6666
return; /* An error already occurred */
6767
size = PyString_Size(p->str);
68-
newsize = size + 1024;
68+
newsize = size + size + 1024;
69+
if (newsize > 32*1024*1024) {
70+
newsize = size + 1024*1024;
71+
}
6972
if (_PyString_Resize(&p->str, newsize) != 0) {
7073
p->ptr = p->end = NULL;
7174
}

0 commit comments

Comments
 (0)