Fine tuning register use with C# ?

Fine tuning register use with C# ?

Post by fd12345 » Wed, 28 Jul 2004 23:08:15


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
VB's "With" in C#.
 
 
 

Fine tuning register use with C# ?

Post by paulhill3 » Thu, 29 Jul 2004 18:30:47

When you attach the de *** to a process, the JIT generates the
following code:

00000016 mov dword ptr [ebp-4],esi
00000019 mov eax,dword ptr [ebp-4]
0000001c mov byte ptr [eax],1
0000001f mov byte ptr [eax+1],2
00000023 mov byte ptr [eax+2],3
00000027 mov dword ptr [ebp-4],0


But you HAVE to attach the process with debugging to see this - normal
"Start with debugging" disables JIT optimisations so you see less than
optimal code in assembly form.

 
 
 

Fine tuning register use with C# ?

Post by fd12345 » Fri, 30 Jul 2004 00:49:05

Hi Paul,

I have been reading blogs (mainly from MS gurus) all day, about how
smart the JITer is (optimizing the switch statements, the incredible
foreach optimization on arrays...), and I was beginning to think I'd
better check my code once compiled... Thanks for doing it and
confirming my suspicions. I love that compiler a bit more every day.

Michel.
 
 
 

Fine tuning register use with C# ?

Post by David Nota » Fri, 06 Aug 2004 21:32:31

Plus, it will be probably more efficient to pack the RGB values of a pixel
into an uint and do the write in only one instruction (you still have 3
writes per pixel)

--
David Notario
Software Design Engineer, CLR JIT Compiler
http://www.yqcomputer.com/
 
 
 

Fine tuning register use with C# ?

Post by fd12345 » Sun, 08 Aug 2004 01:26:03

Hi David,

Thanks for the suggestion, but unfortunately these are 24-bit bitmaps,
which means I can't use full dwords. Actually, come to think of it, it
might be faster to use 32 bits internally. I'll do some benching.
Thanks !

Michel