A while ago I blogged about why the CSharp var keyword cannot be used with local ILNumerics arrays (ILArray<T>, ILCell, ILLogical). This post is about the other one of the two main limitations on C# language features in ILNumerics: the use of compound operators in conjunction with ILArray<T>. In the online documentation we state the rule as follows:
The following features of the C# language are not compatible with the memory management of ILNumerics and its use is not supported:
- The C# var keyword in conjunction with any ILNumerics array types, and
- Any compound operator, like +=, -=, /=, *= a.s.o. Exactly spoken, these operators are not allowed in conjunction with the indexer on arrays. So A += 1; is allowed. A[0] += 1; is not!
Let’s take a closer look at the second rule. Most developers think of compound operators as being just syntactic sugar for some common expressions:
int i = 1;
i += 2;
… would simply expand to:
int i = 1;
i = i + 2;
For such simple types like an integer variable the actual effect will be indistinguishable from that expectation. However, compound operators introduce a lot more than that. Back in his times at Microsoft, Eric Lippert blogged about those subtleties. The article is worth reading for a deep understanding of all side effects. In the following, we will focus on the single fact, which becomes important in conjunction with ILNumerics arrays: when used with a compound operator, i
in the example above is only evaluated once! In difference to that, in i = i + 2
, i
is evaluated twice.
Evaluating an int
does not cause any side effects. However, if used on more complex types, the evaluation may does cause side effects. An expression like the following:
ILArray<double> A = 1;
A += 2;
… evaluates to something similiar to this:
ILArray<double> A = 1;
A = (ILArray<double>)(A + 2);
There is nothing wrong with that! A += 2 will work as expected. Problems arise, if we include indexers on A:
ILArray<double> A = ILMath.rand(1,10);
A[0] += 2;
// this transforms to something similar to the following:
var receiver = A;
var index = (ILRetArray<double>)0;
receiver[index] = receiver[index] + 2;
In order to understand what exactly is going on here, we need to take a look at the definition of indexers on ILArray
:
public ILRetArray<ElementType> this[params ILBaseArray[] range] { ...
The indexer expects a variable length array of ILBaseArray
. This gives most flexibility for defining subarrays in ILNumerics. Indexers allow not only scalars of builtin system types as in our example, but arbitrary ILArray and string definitions. In the expression A[0]
, 0
is implicitly converted to a scalar ILNumerics array before the indexer is invoked. Thus, a temporary array is created as argument. Keep in mind, due to the memory management of ILNumerics, all such implicitly created temporary arrays are immediately disposed off after the first use.
Since both, the indexing expression 0
and the object where the indexer is defined for (i.e.: A) are evaluated only once, we run into a problem: index
is needed twice. At first, it is used to acquire the subarray at receiver[index]
. The indexer get { ...}
function is used for that. Once it returns, all input arguments are disposed – an important foundation of ILNumerics memory efficency! Therefore, if we invoke the index setter function with the same index
variable, it will find the array being disposed already – and throws an exception.
It would certainly be possible to circumvent that behavior by converting scalar system types to ILArray
instead of ILRetArray
:
ILArray A = ...;
A[(ILArray)0] += 2;
However, the much less expressive syntax aside, this would not solve our problem in general either. The reason lies in the flexibility required for the indexer arguments. The user must manually ensure, all arguments in the indexer argument list are of some non-volatile array type. Casting to ILArray<T>
might be an option in some situations. However, in general, compound operators require much more attention due to the efficient memory management in ILNumerics. We considered the risk of failing to provide only non-volatile arguments too high. So we decided not to support compound operators at all.
See: General Rules for ILNumerics, Function Rules, Subarrays