classification
Title: Implement __sizeof__ for etree Element
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, eli.bendersky, flox, loewis, ncoghlan, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2012-02-19 20:28 by loewis, last changed 2012-06-17 08:43 by loewis. This issue is now closed.

Files
File name Uploaded Description Edit
sizeof.diff loewis, 2012-02-19 20:28 review
Messages (6)
msg153726 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-02-19 20:28
The cElementTree Element currently returns an incorrect response to sys.getsizeof, as it doesn't account for the extra structure, and for the child pointers array. This patch corrects the computation.
msg162980 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2012-06-16 17:21
Out of curiosity, why don't the constituents of Element (i.e. the tag, the attributes dict, etc.) count for its size?
msg162985 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-06-16 18:43
__sizeof__ is supposed to account for just the object, not for the size of any object it refers to. If you want to compute total memory consumption, you should get a list of all objects, and then sum up __sizeof__. If some object would also include some of its referents, there would be a danger of accounting some memory twice.
msg163008 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2012-06-17 03:04
Martin, thanks for the explanation. The patch LGTM, then.

Could it be useful to document this a bit more explicitly in the description of sys.getsizeof? The most intuitive thing to expect from it is to compute the *total* size including contained objects. So this is somewhat surprising:

>>> import sys
>>> sys.getsizeof([1, 2, 3])
96
>>> sys.getsizeof([1, 2, [10] * 500])
96

The last sentence in the doc of sys.getsizeof says: "See recursive sizeof recipe for an example of using getsizeof() recursively to find the size of containers and all their contents.", which can be taken as a hint, but maybe it could be just said straightforwardly. I.e before it, add: "Note that getsizeof returns just the memory occupied by the object itself, not any contained objects it holds references to".
msg163024 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-06-17 07:37
Given how C's sizeof works, it may be debatable whether it's natural to expect that sys.getsizeof should be recursive. If you have a struct with pointers (say, char*) in C, and you do sizeof, the string length isn't considered, either.

But I'm certainly fine with making this more clear.
msg163035 - (view) Author: Roundup Robot (python-dev) Date: 2012-06-17 08:42
New changeset 093dec81ea1f by Martin v. Löwis in branch 'default':
Issue #14055: Add __sizeof__ support to _elementtree.
http://hg.python.org/cpython/rev/093dec81ea1f
History
Date User Action Args
2012-06-17 08:43:47loewissetstatus: open -> closed
resolution: fixed
2012-06-17 08:42:08python-devsetnosy: + python-dev
messages: + msg163035
2012-06-17 07:37:25loewissetmessages: + msg163024
2012-06-17 03:04:18eli.benderskysetnosy: + ncoghlan
messages: + msg163008
2012-06-16 18:43:45loewissetmessages: + msg162985
2012-06-16 17:21:23eli.benderskysetmessages: + msg162980
2012-06-16 15:38:13pitrousetnosy: + eli.bendersky
2012-06-16 15:34:13serhiy.storchakasetnosy: + serhiy.storchaka
2012-02-20 01:09:06Arfreversetnosy: + Arfrever
2012-02-19 20:32:22pitrousetversions: + Python 3.3
nosy: + flox

components: + Library (Lib)
type: enhancement
stage: patch review
2012-02-19 20:28:45loewiscreate