classification
Title: Correct __sizeof__ support for StringIO
Type: behavior Stage: patch review
Components: IO, Library (Lib) Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: aliles, benjamin.peterson, jcea, loewis, pitrou, serhiy.storchaka, stutzbach
Priority: normal Keywords: needs review, patch

Created on 2012-07-29 19:53 by serhiy.storchaka, last changed 2013-11-30 20:16 by alexandre.vassalotti.

Files
File name Uploaded Description Edit
stringio_sizeof-3.3_3.patch serhiy.storchaka, 2012-09-21 06:51 Patch for 3.3 review
stringio_sizeof-2.7_3.patch serhiy.storchaka, 2012-09-21 06:52 Patch for 2.7 review
Messages (16)
msg166807 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-07-29 19:53
Here is a patch that implements correct __sizeof__ for C implementation of io.StringIO.

I haven't tested the patch on narrow build.
msg166808 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-29 19:58
> Here is a patch that implements correct __sizeof__ for C implementation of io.StringIO.

For some value of "correct", since the internal accumulator could hold
alive some unicode strings.

> I haven't tested the patch on narrow build.

There's no narrow build anymore on 3.3.
msg166812 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-07-29 20:54
> For some value of "correct", since the internal accumulator could hold
> alive some unicode strings.

This is not a concern of __sizeof__, because these lists and strings are 
managed by GC.

> There's no narrow build anymore on 3.3.

I mean 2.7 and 3.2. I have written test for such case, but can't check if it 
correct.
msg166813 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-29 21:04
> > For some value of "correct", since the internal accumulator could hold
> > alive some unicode strings.
> 
> This is not a concern of __sizeof__, because these lists and strings are 
> managed by GC.

It is, since they are private and not known by the user.
msg166814 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-07-29 21:11
The question really is what memory blocks can "leak out" of the object, and those don't really belong to the container. Those shouldn't be accounted for, since somebody else may get hold of them, and pass them to sys.sizeof.

For the PyAccu, AFAICT, objects cannot leak out of it (except for gc.getobjects in debug mode). So I think the memory allocated by the accu really belongs to the StringIO, and needs to be accounted there. The same goes for readnl, writenl, and weakreflist.

On a related note, I wonder why the tp_clear and tp_traverse functions for stringio are so short. Shouldn't it also visit "decoder"?
msg166865 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-07-30 08:14
> For the PyAccu, AFAICT, objects cannot leak out of it (except for gc.getobjects in debug mode).

Not only in debug mode.

>>> import io, gc
>>> s=io.StringIO()
>>> s.write('12345')
5
>>> s.write('qwerty')
6
>>> for o in gc.get_objects():
...     if "'123" in repr(o) and len(repr(o)) < 1000:
...         print(type(o), repr(o))
... 
<class 'list'> ['12345', 'qwerty']
<class 'list'> ['o', 'gc', 'get_objects', 'repr', 'o', "'123", 1000, 'len', 'repr', 'o', 'print', 'type', 'o', 'repr', 'o']
<class 'tuple'> ("'123", 1000, None)

Someone can summarize sys.getsizeof() for all managed by GC objects and therefore count internal objects twice.

I think the standard library should provide a method for recursive calculation of memory (in some sense, perhaps using different strategies), but that should wait up to 3.4. __sizeof__ should count non-object memory that is not available for GC. As I understand it.
msg166868 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-07-30 09:01
>> For the PyAccu, AFAICT, objects cannot leak out of it (except for  
>> gc.getobjects in debug mode).
>
> Not only in debug mode.

I see. I meant sys.getobjects, which is in debug mode only, but
I now see that gc.get_objects will get the list (but not the strings)
of the Accu.

That still leaves readnl and writenl.

> I think the standard library should provide a method for recursive  
> calculation of memory (in some sense, perhaps using different  
> strategies), but that should wait up to 3.4.

Actually, this is (and should be) a separate project:

http://guppy-pe.sourceforge.net/

> __sizeof__ should count non-object memory that is not available for  
> GC. As I understand it.

I think you misunderstand slightly; the GC relevance is only a side
effect. __sizeof__ should only account for memory that isn't separately
accessible. The notion of "separately accessible" is somewhat weak, since
it may depend on the container.

Non-PyObject blocks clearly need to be accounted for.

PyObject blocks normally don't need to be accounted for, as they are typically
accessible separately, by the following means:
- gc.get_objects (for GC objects)
- gc.get_referents (for contained non-GC objects)

There are also irregular ways to get objects:
- in debug mode only: sys.getobjects
- customer access functions or attributes

For memory accounting, it would really be best if the latter two categories
wouldn't exist, i.e. if all non-GC objects were still visible through  
tp_traverse.
msg167010 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-07-31 15:52
Do I understand correctly that for each Python subobject must be one of the following:
1) or it is visited in tp_traverse;
2) or it is accounted in __sizeof__ together with all its subobjects (if they cannot leak out)?

PyAccu can contains not only Unicode objects, but at least an instance of a Unicode subtype, which can has a reference to StringIO object, creating a cycle. Does this mean that PyAccu should be visited in tp_traverse and not be accounted in __sizeof__? Or should we tighten the control and ensure that PyAccu contains only exact Unicode objects?
msg167034 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-31 19:34
> Does this mean that PyAccu should be visited in tp_traverse and not be
> accounted in __sizeof__? Or should we tighten the control and ensure
> that PyAccu contains only exact Unicode objects?

IMO, the latter.
msg168242 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-08-14 20:57
Patch updated. Now private pyobjects (readnl, accu) counted.

Note all three patches rather different.
msg170565 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-09-16 18:40
It would be good if someone looked at the patches. I'm not sure that they are good enough. Perhaps they are too complicated and we shouldn't worry about duplicates?
msg170569 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-09-16 18:52
Well, I do think that it's not very important to make __sizeof__ strictly exact according to some specification. I'm also not sure we want to complicate maintenance when the calculation becomes tedious or difficult.

To me __sizeof__ is mostly an educational tool which gives a hint at the memory impact of an object, but cannot really convey a precise information except for trivial atomic types.
msg170594 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-09-17 11:18
I disagree that sizeof cannot work well for variable-sized types. It works very well for strings, lists, tuple, dicts, and other "regular" containers. I agree that it is not important that it is absolutely correct (in some sense) for every object, but it shouldn't lose "big" chunks of data. A bug where it misses four bytes is much less important than a bug where it misses N bytes (for an object-specific value N that can grow indefinitely).

As for the specific patch, I don't think any action should be taken before the 3.3 release. I would personally prefer if the computations where done in Py_ssize_t, not PyObject* (i.e. the result of the recursive call should be unwrapped).
msg170599 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-09-17 12:26
It can't work well if we count internal Python objects that can be shared. This is because the "work well" concept is not well defined. And because the implementation of a certain defined calculation algorithm can be completely unmaintainable, that is not well.

If we wrote in the StringIO the same 1 MB string twice, should the __sizeof__() return about 1) 2 MB, 2) 1 MB or 3) size of empty stream if there are external links to this shared string? Patch implements the second strategy, it can be simplified to the first one or complicated even more to a third one. Even more complication will need to take into account the sharing of eol string ('\r' and '\n' always shared, '\r\n' may be).

> I would personally prefer if the computations where done in Py_ssize_t, not PyObject*

I too. But on platforms with 64-bit pointers and 32-bit sizes we can allocate total more than PY_SIZE_MAX bytes (hey, I remember the DOS memory models with 16-bit size_t and 32-bit pointers). Even faster we get an overflow if allow the repeated counting of shared objects. What to do with overflow? Return PY_SIZE_MAX or ignore the possibility of errors?
msg170604 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-09-17 12:51
Am 17.09.2012 14:26, schrieb Serhiy Storchaka:
>> I would personally prefer if the computations where done in
>> Py_ssize_t, not PyObject*
> 
> I too. But on platforms with 64-bit pointers and 32-bit sizes we can
> allocate total more than PY_SIZE_MAX bytes (hey, I remember the DOS
> memory models with 16-bit size_t and 32-bit pointers). Even faster we
> get an overflow if allow the repeated counting of shared objects.
> What to do with overflow? Return PY_SIZE_MAX or ignore the
> possibility of errors?

It can never overflow. We cannot allocate more memory than SIZE_MAX;
this is (mostly) guaranteed by the C standard. I don't know whether
you deliberately brought up the obscure case of 64-bit pointers and
32-bit sizes. If there are such systems, we don't support them.
msg170863 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-09-21 06:51
Patches updated. Now the computations done in size_t.

Note that the patches for the different versions differ.
History
Date User Action Args
2013-11-30 20:16:31alexandre.vassalottisetfiles: - stringio_sizeof-3.2_3.patch
2013-11-30 20:16:22alexandre.vassalottisetfiles: - stringio_sizeof-2.7_2.patch
2013-11-30 20:16:10alexandre.vassalottisetfiles: - stringio_sizeof-3.2_2.patch
2013-11-30 20:15:57alexandre.vassalottisetfiles: - stringio_sizeof-3.3_2.patch
2013-11-30 20:15:49alexandre.vassalottisetfiles: - stringio_sizeof-2.7.patch
2013-11-30 20:15:42alexandre.vassalottisetfiles: - stringio_sizeof-3.2.patch
2013-11-30 20:15:36alexandre.vassalottisetfiles: - stringio_sizeof-3.3.patch
2012-12-29 22:16:39serhiy.storchakasetassignee: serhiy.storchaka
2012-11-02 07:29:11hyneksetnosy: - hynek
2012-10-02 16:45:23serhiy.storchakasetversions: + Python 3.4
2012-09-21 06:52:22serhiy.storchakasetfiles: + stringio_sizeof-2.7_3.patch
2012-09-21 06:51:51serhiy.storchakasetfiles: + stringio_sizeof-3.2_3.patch
2012-09-21 06:51:18serhiy.storchakasetfiles: + stringio_sizeof-3.3_3.patch

messages: + msg170863
2012-09-17 12:51:15loewissetmessages: + msg170604
2012-09-17 12:26:17serhiy.storchakasetmessages: + msg170599
2012-09-17 11:18:29loewissetmessages: + msg170594
2012-09-16 18:52:08pitrousetmessages: + msg170569
2012-09-16 18:40:01serhiy.storchakasetmessages: + msg170565
2012-08-29 12:02:50alilessetnosy: + aliles
2012-08-15 20:57:59serhiy.storchakasetkeywords: + needs review
assignee: serhiy.storchaka -> (no value)
stage: patch review
2012-08-14 20:59:10serhiy.storchakasetfiles: + stringio_sizeof-2.7_2.patch
2012-08-14 20:58:12serhiy.storchakasetfiles: + stringio_sizeof-3.2_2.patch
2012-08-14 20:57:32serhiy.storchakasetfiles: + stringio_sizeof-3.3_2.patch

messages: + msg168242
2012-08-05 11:01:52serhiy.storchakasetassignee: serhiy.storchaka
2012-08-01 00:56:12jceasetnosy: + jcea
2012-07-31 19:34:54pitrousetmessages: + msg167034
2012-07-31 15:52:33serhiy.storchakasetmessages: + msg167010
2012-07-30 09:01:17loewissetmessages: + msg166868
2012-07-30 08:14:11serhiy.storchakasetmessages: + msg166865
2012-07-29 21:11:22loewissetnosy: + loewis
messages: + msg166814
2012-07-29 21:04:07pitrousetmessages: + msg166813
2012-07-29 20:54:41serhiy.storchakasetmessages: + msg166812
2012-07-29 19:58:28pitrousetmessages: + msg166808
2012-07-29 19:54:18serhiy.storchakasetfiles: + stringio_sizeof-2.7.patch
2012-07-29 19:53:55serhiy.storchakasetfiles: + stringio_sizeof-3.2.patch
2012-07-29 19:53:36serhiy.storchakacreate