Array Operations: a[] + b[] etc.

John Colvin john.loughran.colvin at gmail.com
Thu Nov 22 18:06:44 PST 2012


On Thursday, 22 November 2012 at 21:37:19 UTC, Dmitry Olshansky 
wrote:
> Array ops supposed to be overhead-free loops transparently 
> leveraging SIMD parallelism of modern CPUs. No more and no 
> less. It's like auto-vectorization but it's guaranteed and 
> obvious in the form.

I disagree that array ops are only for speed.
I would argue that their primary significance lies in their 
ability to make code significantly more readable, and more 
importantly, writeable. For example, the vector distance between 
2 position vectors can be written as:
dv[] = v2[] - v1[]
or
dv = v2[] - v1[]
anyone with an understanding of mathematical vectors instantly 
understands the general intent of the code.
With documentation something vaguely like this:
"An array is a reference to a chunk of memory that contains a 
list of data, all of the same type. v[] means the set of elements 
in the array, while v on it's own refers to just the reference. 
Operations on sets of elements e.g. dv[] = v2[] - v1[] work 
element-wise along the arrays {insert mathematical notation and 
picture of 3 arrays as columns next to each other etc.}.
Array operations can be very fast, as they are sometimes lowered 
directly to cpu vector instructions. However, be aware of 
situations where a new array has to be created implicitly, e.g. 
dv = v2[] - v1[]; Let's look at what this really means: we are 
asking for dv to be set to refer to the vector difference between 
v2 and v1. Note we said nothing about the current elements of dv, 
it might not even have any! This means we need to put the result 
of v2[] - v1] in a new chunk of memory, which we then set dv to 
refer to. Allocating new memory takes time, potentially taking a 
lot longer than the array operation itself, so if you can, avoid 
it!",
anyone with the most basic programming and mathematical knowledge 
can write concise code operating on arrays, taking advantage of 
the potential speedups while being aware of the pitfalls.

In short:
Vector syntax/array ops is/are great. Concise code that's easy to 
read and write. They fulfill one of the guiding principles of D: 
the most obvious code is fast and safe (or if not 100% safe, at 
least not too error-prone).
More vector syntax capabilities please!


More information about the Digitalmars-d mailing list