Hi all.
I'm trying to optimize a tight loop which is inside a drawing routine.
It's written in C#, and the loop itself is in an unsafe block. As I'm
working on pixels, I have declared a struct:
public struct RGB
{
public byte B;
public byte G;
public byte R;
}
....And I'm working through the pixels of a bitmap through the use of a
pointer, like this (I'll skip the stride details for clarity) :
RGB* myPointer = myBitmapData.Scan0;
The following instructions :
myPointer->B = 1;
myPointer->G = 2;
myPointer->R = 3;
....are compiled as :
mov eax,dword ptr [ebp-10h]
mov byte ptr [eax],01h
mov eax,dword ptr [ebp-10h]
mov byte ptr [eax+1],02h
mov eax,dword ptr [ebp-10h]
mov byte ptr [eax+2],03h
EAX is loaded with the pointer everytime, whereas on second and third
times, it is not necessary. Is there a way to tell the compiler to
just do this ?
mov eax,dword ptr [ebp-10h]
mov byte ptr [eax],01h
mov byte ptr [eax+1],02h
mov byte ptr [eax+2],03h
In pseudo-code, it would be :
with (myPointer)
{
->B = 1;
->G = 2;
->R = 3;
}
Of course, I could write it in asm, but I'm quite sure there is a very
obvious and very elegant solution that I'm missing completely. Any
clues?
Michel
PS : the use of "with" in the pseudo-code example is just to clarify
my meaning. It's only a question about compiler behaviour for the JIT
gurus, not an Nth debate about "using" and "aliases", or incorporating