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

Julian Taylor jtaylor.debian at googlemail.com
Fri Jun 6 19:21:40 CEST 2014


On 06.06.2014 04:26, Greg Ewing wrote:

Nathaniel Smith wrote:

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. I can think of at least one plausible scenario where this could occur: the operand is a view object that wraps another object, and its iadd method updates that other object. In fact, now that I think about it, exactly this kind of thing happens in numpy when you slice an array! So the opt-in indicator would need to be dynamic, on a per-object basis, rather than a type flag.

yes an opt-in indicator would need to receive both operand objects so it would need to be a slot in the object or number type object. Would the addition of a tp_can_elide slot to the object types be acceptable for this rather specialized case?

tp_can_elide receives two objects and returns one of three values:

Implementation could e.g. look about like this:

TARGET(BINARY_SUBTRACT) { fl = left->obj_type->tp_can_elide fr = right->obj_type->tp_can_elide elide = 0 if (unlikely(fl)) { elide = fl(left, right) } else if (unlikely(fr)) { elide = fr(left, right) } if (unlikely(elide == YES) && left->refcnt == 1) { PyNumber_InPlaceSubtract(left, right) } else if (unlikely(elide == SWAPPABLE) && right->refcnt == 1) { PyNumber_InPlaceSubtract(right, left) } else { PyNumber_Subtract(left, right) } }



More information about the Python-Dev mailing list