
On Thu, 2022-02-24 at 00:21 +0100, Antonio Cuni wrote:
On Mon, Feb 21, 2022 at 5:18 PM Petr Viktorin <encukou@gmail.com> wrote:
Should we care about hacks/optimizations that rely on having the only
reference (or all references), e.g. mutating a tuple if it has refcount 1? Immortal objects shouldn't break them (the special case simply won't apply), but this wording would make them illegal. AFAIK CPython uses this internally, but I don't know how prevalent/useful it is in third-party code.
FWIW, a real world example of this is numpy.ndarray.resize(..., refcheck=True): https://numpy.org/doc/stable/reference/generated/numpy.ndarray.resize.html#n... https://github.com/numpy/numpy/blob/main/numpy/core/src/multiarray/shape.c#L...
When refcheck=True (the default), numpy raises an error if you try to resize an array inplace whose refcnt > 2 (although I don't understand why > 2 and not > 1, and the docs aren't very clear about this).
That said, relying on the exact value of the refcnt is very bad for alternative implementations and for HPy, and in particular it is impossible to implement ndarray.resize(refcheck=True) correctly on PyPy. So from this point of view, a wording which explicitly restricts the "legal" usage of the refcnt details would be very welcome.
Yeah, NumPy resizing is a bit of an awkward point, I would be on-board for just replacing resize for non NumPy does also have a bit of magic akin to the "string concat" trick for operations like: a + b + c where it will try do magic and use the knowledge that it can mutate/reuse the temporary array, effectively doing: tmp = a + b tmp += c (which requires some stack walking magic additionally to the refcount!) Cheers, Sebastian
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/ACJIER45... Code of Conduct: http://python.org/psf/codeofconduct/