[Matrix-SIG] Numeric Nits

David Ascher da@ski.org
Tue, 22 Jun 1999 11:18:10 -0700 (Pacific Daylight Time)


On Tue, 22 Jun 1999, Rick White wrote:

> It appears to me it might be a lot simpler than this, though my
> understanding of the Python/Numeric code is not very deep so I certainly
> could be wrong.  For example, here is the Python C code to do a multiply
> operation:
> 
>    case BINARY_MULTIPLY:
>       w = POP();
>       v = POP();
>       x = PyNumber_Multiply(v, w);
>       Py_DECREF(v);
>       Py_DECREF(w);
>       PUSH(x);
>       if (x != NULL) continue;
>       break;
> 
> Presumably if v and/or w are Numeric arrays, PyNumber_Multiply
> eventually calls the Numeric multiply routine.  In the current Numeric
> implementation, a new Numeric array is allocated, filled with the
> product, and returned for assignment to x.  Every operation results in
> the creation of another temporary array.
> 
> Suppose v is the intermediate result of another expression.  Then its
> reference count will be 1, so that after returning from the multiply
> routine its memory will be released by the DECREF.  Couldn't the
> Numeric multiply routine check the reference count of v and, if it is 1
> and if the datatype and size of v are appropriate, put the result into
> v instead of into a new array?  Then the ref count for v would be
> incremented and v would be returned as the function result.

It's an interesting approach.  However, I don't think that the "Suppose
..." statement above corresponds to an "if and only if", which I believe
is needed.  Consider:

   a = arange(10)
   b = a * 3
   return a, b

At the point of the PyArray_Multiply, the refcount of its first argument
(a) is 1. However, it is not a temporary variable.  Or did I miss
something?

--david ascher