[Python-Dev] [numpy wishlist] Interpreter support for temporary elision in third-party classes

Nikolaus Rath Nikolaus at rath.org
Fri Jun 6 04:27:20 CEST 2014


Nathaniel Smith <njs at pobox.com> writes:
>> >     tmp1 = a + b
>> >     tmp1 += c
>> >     tmp1 /= c
>> >     result = tmp1
>>
>> Could this transformation be done in the ast? And would that help?
>
> I don't think it could be done in the ast because I don't think you can
> work with anonymous temporaries there. But, now that you mention it, it
> could be done on the fly in the implementation of the relevant opcodes.
> I.e., BIN_ADD could do
>
> if (Py_REFCNT(left) == 1)
>     result = PyNumber_InPlaceAdd(left, right);
> else
>     result = PyNumber_Add(left, right)
>
> Upside: all packages automagically benefit!
>
> Potential downsides to consider:
> - Subtle but real and user-visible change in Python semantics. I'd be a
> little nervous about whether anyone has implemented, say, an iadd with side
> effects such that you can tell whether a copy was made, even if the object
> being copied is immediately destroyed. Maybe this doesn't make sense
> though.

Hmm. I don't think this is as unlikely as it may sound. Consider eg the
h5py module:

with h5py.File('database.h5') as fh:
     result = fh['key'] + np.ones(42)

if this were transformed to

with h5py.File('database.h5') as fh:
    tmp = fh['key']
    tmp += np.ones(42)
    result = tmp

then the database.h5 file would get modified, *and* result would be of
type h5py.Dataset rather than np.array.


Best,
-Nikolaus

-- 
GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F
Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F

             »Time flies like an arrow, fruit flies like a Banana.«


More information about the Python-Dev mailing list