"The total size of a static array cannot exceed 16Mb."

Vladimir Panteleev thecybershadow at gmail.com
Tue Oct 2 15:48:46 PDT 2007


On Wed, 03 Oct 2007 00:51:46 +0300, BCS <ao at pathlink.com> wrote:

> Unless I'm smoking something, the code to access any array will look the
> same regardless of where the array is.

Well, with how I understand this terminology, "dereferencing" means reading an offset from a memory location, other than an immediate instruction operand. Consider:

int i;
i++;

This assembles as:

i dd ?
...
inc i

Assuming i is in the data segment (it has a fixed address), I do not consider this dereferencing, nor would I think anyone should.

Then we have:

int* i;
...
(*i)++;

Assuming i is in the data segment (it has a fixed address), this is a "single" dereference. To get the address of the integer we need to read it from a fixed memory location.

The same with arrays - except, for the first case, it's a static array, and for the second - it is either a dynamic array, a new-allocated array, etc.

So, how does this boil down in performance? Let me show you:

int[0x4000000] bigArr;
...
int readValue(int index)
{
	return bigArr[index];
}

With -release -O, this assembles to:

readValue proc near
        mov     eax, ds:bigArr[eax*4]
        retn
readValue endp

Now, if bigArr would be a dynamic array or referenced through a pointer, the resulting code would look like this:

readValue proc near
        mov     ecx, ds:bigArr
        mov     eax, [ecx+eax*4]
        retn
readValue endp

It gets worse if there are further nests, e.g. when using multidimensional arrays.

That doesn't sound like a big deal, however:

1) when the array is in the data segment, we know the address of every one of its elements - so random access is faster
2) when using multi-dimensional dynamic arrays, there are more casts
3) rectangular static arrays are much faster when you want to access it by column - you just add (width*element_size) to the pointer, and go to the element on the next row
4) when you want to access an address inside the element (you have an array of structs), you must perform an addition after the multiplication (I think it can be part of the instruction in the newer instruction sets though) - while, when the array base address is predetermined, you can just use base_address + element_offset as the base address, then add index*element_size to that
etc.

-- 
Best regards,
 Vladimir                          mailto:thecybershadow at gmail.com



More information about the Digitalmars-d mailing list