Message Dispatcher - Your thoughts/comments

Started by SubEvil, November 11, 2005, 09:49:35 AM

Previous topic - Next topic

SubEvil

In 2002 I think, hutch created a thread about a high speed message dispatcher.

I did something similar I believe, I did this in C:


LRESULT (CALLBACK *window_messages[WM_USER])(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

__declspec( naked ) void WindowProc( void )
{
__asm mov eax, [esp + 8]
__asm cmp eax, WM_USER
__asm jnb GotoDefWindowProc
__asm jmp dword ptr [window_messages + eax * 4]
GotoDefWindowProc:
__asm jmp dword ptr [DefWindowProc]
}


The extra jump was because IE was broadcasting a message with the value of 44000 to my app. The array of function pointers has 1024 (WM_USER) elements, since I don't have any custom messages!

I would like to know/get your comments on how this could compare to the traditional switch...case statement, taking into consideration newer architecture etc. The reason I wrote this in assembler was because the compiler was creating terrible code for the following:


LRESULT CALLBACK WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
if( uMsg < WM_USER )
return window_messages[uMsg](hWnd, uMsg, wParam, lParam);
else
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}


It was pushing all values back on the stack and calling the function, then returning etc.

My basic profiling found it to be only 5% faster than the old case statement (17 cases).

What are your thoughts/opinion on this?