Issue #3116 and #1792: Fix quadratic behavior in marshal.dumps().
diff --git a/Misc/NEWS b/Misc/NEWS
index 6363f80..cdc3f9c 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -48,6 +48,8 @@
Library
-------
+- Issue #3116 and #1792: Fix quadratic behavior in marshal.dumps().
+
- Issue #2682: ctypes callback functions no longer contain a cyclic
reference to themselves.
diff --git a/Python/marshal.c b/Python/marshal.c
index 897c15e..c305bbe 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -65,7 +65,10 @@
if (p->str == NULL)
return; /* An error already occurred */
size = PyString_Size(p->str);
- newsize = size + 1024;
+ newsize = size + size + 1024;
+ if (newsize > 32*1024*1024) {
+ newsize = size + (size >> 3); /* 12.5% overallocation */
+ }
if (_PyString_Resize(&p->str, newsize) != 0) {
p->ptr = p->end = NULL;
}