Despite the potential speed increase (I've implemented some and it  
can be considerable), SAGE has avoided almost all use of inplace  
operators due to the fact that elements are supposed to be immutable,  
despite the fact that one does not really need the "old" result  
anymore. For example, if I type x^5 - 3*x + 1, the subexpressions  
(x^5), (3*x), and (x^5-3*x) are all created and then discarded. This  
also shows up in places like the operation sum() which doesn't return  
any of its intermediate results, or loops that increment variables in  
certain ways. The worry is that perhaps somewhere something else is  
holding onto a given variable, and we don't want to mess it up.

Just the other day, I realized that Python provides the perfect  
solution--by looking at the reference count of an object we can  
detect whether or not its safe to mutate it (i.e. nothing else is  
holding onto it but the current call). If it is safe to mutate, then  
do so, otherwise create and return a new object. I propose adding  
_iadd_c, _imul_c, etc. to the coercion hierarchy such that the  
default __iadd__/__add__ detects whether or not inplace operations  
are safe and calls the respective operation accordingly. One would  
have bold comments on functions that are not safe to call directly.

The only caveat is that it might make it a tiny bit slower for types  
that do not define inplace operations, and it would take a slight  
(SAGE-specific and optional) tweak to Cython (specifically Cython  
local function variables have a refcount one less than expected due  
to their not being in any kind of a python "scope" container, so we  
would need an extra incref/decref them when performing arithmetic on  
them).

- Robert


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~----------~----~----~----~------~----~------~--~---

Reply via email to