You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pickletester.diff: add a unit test to verify attribute names are interned
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/pitrou'closed_at=<Date2009-05-02.21:42:49.807>created_at=<Date2009-01-27.21:52:19.380>labels= ['library', 'performance']
title='unpickling does not intern attribute names'updated_at=<Date2009-05-02.21:42:49.789>user='https://bugs.python.org/jakemcguire'
Instance attribute names are normally interned - this is done in
PyObject_SetAttr (among other places). Unpickling (in pickle and
cPickle) directly updates __dict__ on the instance object. This
bypasses the interning so you end up with many copies of the strings
representing your attribute names, which wastes a lot of space, both in
RAM and in pickles of sequences of objects created from pickles. Note
that the native python memcached client uses pickle to serialize
objects.
>>> import pickle
>>> classC(object):
... def__init__(self, x):
... self.long_attribute_name = x
...
>>> len(pickle.dumps([pickle.loads(pickle.dumps(C(None),
pickle.HIGHEST_PROTOCOL)) for i in range(100)],
pickle.HIGHEST_PROTOCOL))
3658
>>> len(pickle.dumps([C(None) for i inrange(100)],
pickle.HIGHEST_PROTOCOL))
1441
>>>
Interning the strings on unpickling makes the pickles smaller, and at
least for cPickle actually makes unpickling sequences of many objects
slightly faster. I have included proposed patches to cPickle.c and
pickle.py, and would appreciate any feedback.
The patch for cPickle doesn't do the same thing as the pickle one. In
the cPickle one, you are only interning slot attributes, which, I
believe, is not what you intended. :-)
Are you sure? I may not have enough context in my diff, which I
should have done against an anonymous svn checkout, but it looks like
the slot attributes get set several lines after my diff. "while
(PyDict_Next(slotstate, ...))" as opposed to the "while
(PyDict_Next(state, ...))" in my change...
-jake
On Jan 27, 2009, at 6:54 PM, Alexandre Vassalotti wrote:
The patch for cPickle doesn't do the same thing as the pickle one. In
the cPickle one, you are only interning slot attributes, which, I
believe, is not what you intended. :-)
Why do you call PyString_AsString() followed by PyString_FromString()?
Strings are immutable so you shouldn't neek to take a copy.
Besides, it would be nice to add a test.
jakemcguire commented on Feb 24, 2009
jakemcguiremannequin
MannequinAuthor
More actions
The fromstring/asstring dance was due to my incomplete understanding of
refcounting. PyDict_Next returns a borrowed reference but
PyString_InternInPlace expects an owned reference. Thanks to Kirk
McDonald, I have a new patch that does the refcounting correctly.
The sorted(map(id, d.__dict__)) should have been the same before and
after pickling.
jakemcguire commented on Feb 25, 2009
jakemcguiremannequin
MannequinAuthor
More actions
Ugh. Clearly I didn't check to see if it worked or not, as it didn't even
compile. A new diff, tested and verified to work, is attached. I'll work
on creating a test.
Activity
jakemcguire commentedon Jan 27, 2009
Instance attribute names are normally interned - this is done in
PyObject_SetAttr (among other places). Unpickling (in pickle and
cPickle) directly updates __dict__ on the instance object. This
bypasses the interning so you end up with many copies of the strings
representing your attribute names, which wastes a lot of space, both in
RAM and in pickles of sequences of objects created from pickles. Note
that the native python memcached client uses pickle to serialize
objects.
Interning the strings on unpickling makes the pickles smaller, and at
least for cPickle actually makes unpickling sequences of many objects
slightly faster. I have included proposed patches to cPickle.c and
pickle.py, and would appreciate any feedback.
ggenellina commentedon Jan 27, 2009
Either my browser got crazy, or you uploaded the same patch (.py) twice.
avassalotti commentedon Jan 28, 2009
The patch for cPickle doesn't do the same thing as the pickle one. In
the cPickle one, you are only interning slot attributes, which, I
believe, is not what you intended. :-)
jakemcguire commentedon Jan 28, 2009
Are you sure? I may not have enough context in my diff, which I
should have done against an anonymous svn checkout, but it looks like
the slot attributes get set several lines after my diff. "while
(PyDict_Next(slotstate, ...))" as opposed to the "while
(PyDict_Next(state, ...))" in my change...
-jake
On Jan 27, 2009, at 6:54 PM, Alexandre Vassalotti wrote:
avassalotti commentedon Jan 28, 2009
Oh, you are right. I was looking at py3k's version of pickle, which uses
PyDict_Update instead of a while loop; that is why I got confused.
pitrou commentedon Feb 1, 2009
Why do you call PyString_AsString() followed by PyString_FromString()?
Strings are immutable so you shouldn't neek to take a copy.
Besides, it would be nice to add a test.
jakemcguire commentedon Feb 24, 2009
The fromstring/asstring dance was due to my incomplete understanding of
refcounting. PyDict_Next returns a borrowed reference but
PyString_InternInPlace expects an owned reference. Thanks to Kirk
McDonald, I have a new patch that does the refcounting correctly.
What sort of test did you have in mind?
pitrou commentedon Feb 25, 2009
The test should check that the unpickled strings have been interned.
pitrou commentedon Feb 25, 2009
In your patch, I'm not sure where the
namevariable is coming from.Have you checked it works?
pitrou commentedon Feb 25, 2009
To give an example of what the test could check:
The
sorted(map(id, d.__dict__))should have been the same before andafter pickling.
jakemcguire commentedon Feb 25, 2009
Ugh. Clearly I didn't check to see if it worked or not, as it didn't even
compile. A new diff, tested and verified to work, is attached. I'll work
on creating a test.
pitrou commentedon May 1, 2009
Jake, are you string working on a test?
pitrou commentedon May 1, 2009
(s/string/still/, of course...)
jakemcguire commentedon May 2, 2009
This fell through the cracks. But the following unit test seems like it
does the right thing (fails with the old module, works with the new ones).
pitrou commentedon May 2, 2009
Thanks, I'll take a look very soon.
pitrou commentedon May 2, 2009
Committed in r72223, r72224. Thanks!