cpython: ff0220c9d213 (original) (raw)

Mercurial > cpython

changeset 68383:ff0220c9d213

Issue #9935: Speed up pickling of instances of user-defined classes. [#9935]

Antoine Pitrou solipsis@pitrou.net
date Fri, 11 Mar 2011 21:30:43 +0100
parents 92b59654e71c
children 8c9fa86a613e 4daad4adaf46
files Lib/test/pickletester.py Misc/NEWS Modules/_pickle.c Objects/typeobject.c
diffstat 4 files changed, 108 insertions(+), 52 deletions(-)[+] [-] Lib/test/pickletester.py 21 Misc/NEWS 2 Modules/_pickle.c 51 Objects/typeobject.c 86

line wrap: on

line diff

--- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -3,6 +3,7 @@ import unittest import pickle import pickletools import copyreg +import weakref from http.cookies import SimpleCookie from test.support import TestFailed, TESTFN, run_with_locale, no_tracing @@ -842,6 +843,25 @@ class AbstractPickleTests(unittest.TestC self.assertEqual(B(x), B(y), detail) self.assertEqual(x.dict, y.dict, detail)

+ # Register a type with copyreg, with extension code extcode. Pickle # an object of that type. Check that the resulting pickle uses opcode # (EXT[124]) under proto 2, and not in proto 1. @@ -1009,7 +1029,6 @@ class AbstractPickleTests(unittest.TestC self.assertRaises(RuntimeError, self.dumps, x, proto) # protocol 2 don't raise a RuntimeError. d = self.dumps(x, 2)

def test_reduce_bad_iterator(self): # Issue4176: crash when 4th and 5th items of reduce()

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -62,6 +62,8 @@ Core and Builtins Library ------- +- Issue #9935: Speed up pickling of instances of user-defined classes. +

--- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -2842,6 +2842,28 @@ save_pers(PicklerObject *self, PyObject return status; } +static PyObject * +get_class(PyObject *obj) +{

+

+} + /* We're saving obj, and args is the 2-thru-5 tuple returned by the

if (newobj_str == NULL) { newobj_str = PyUnicode_InternFromString("newobj");

@@ -2925,9 +2948,9 @@ save_reduce(PicklerObject *self, PyObjec use_newobj = 0; } else {

cls = PyTuple_GET_ITEM(argtup, 0);

if (obj != NULL) {

--- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -3073,14 +3073,19 @@ static PyObject * import_copyreg(void) { static PyObject *copyreg_str;

if (!copyreg_str) { copyreg_str = PyUnicode_InternFromString("copyreg"); if (copyreg_str == NULL) return NULL; } -

+

} static PyObject * @@ -3089,14 +3094,16 @@ slotnames(PyObject *cls) PyObject *clsdict; PyObject *copyreg; PyObject *slotnames; -

+

@@ -3130,12 +3137,20 @@ reduce_2(PyObject *obj) PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; Py_ssize_t i, n; -

-

+

+

+

@@ -3153,7 +3168,7 @@ reduce_2(PyObject *obj) if (args == NULL) goto end;

@@ -3161,17 +3176,18 @@ reduce_2(PyObject *obj) goto end; } else {

@@ -3230,7 +3246,7 @@ reduce_2(PyObject *obj) copyreg = import_copyreg(); if (copyreg == NULL) goto end;

@@ -3238,8 +3254,8 @@ reduce_2(PyObject *obj) args2 = PyTuple_New(n+1); if (args2 == NULL) goto end;

@@ -3249,7 +3265,6 @@ reduce_2(PyObject *obj) res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); end:

+

+