Fastest way to IPC from a DLL to an EXE
Re: inter-process callback possible?
That would be a blunder on Microsoft. Every APC's dream
--Iconic
--Iconic
Re: inter-process callback possible?
Hehe, yes! I was a bit scared because I know that SendMessage() internally handles messages. So calling SendMessage() inside of a hook callback function can be a bad idea.
Re: inter-process callback possible?
No worries Only certain (very few) APIs can interrupt a thread, causing an alertable state, where pending APCs enqueued are executed.
--Iconic
--Iconic
Re: inter-process callback possible?
@iconic, You are correct as far as my rewording is concerned..and I have updated the title Your thoughts, nevertheless were interesting.
Last edited by abalonge on Wed Mar 31, 2021 4:11 pm, edited 1 time in total.
Re: Fastest way to IPC from a DLL to an EXE
I will not using this in a service or a thread.
I have added PostMessage to my testing application and it came in slower than SendMessage.
Results:
Calling PostMessage X 5000000
9.199 seconds for PostMessage
Calling SendMessage X 5000000
1.594 seconds for SendMessage
Calling callback X 5000000
0.250 seconds for Callback
This is between my DLL and EXE, the DLL is notifing the EXE by using many calls to the EXE's main form handle via PostMessage, SendMessage... I also did a Callback for comparison. Of course this test is not inter-process, but now you can see why I wanted to use a Callback in IPC
Maybe my test is not accounting for async vs sync (PostMessage vs SendMessage) calling methods?
Here is the meat of how I tested:
In the DLL:
In the EXE:
I have added PostMessage to my testing application and it came in slower than SendMessage.
Results:
Calling PostMessage X 5000000
9.199 seconds for PostMessage
Calling SendMessage X 5000000
1.594 seconds for SendMessage
Calling callback X 5000000
0.250 seconds for Callback
This is between my DLL and EXE, the DLL is notifing the EXE by using many calls to the EXE's main form handle via PostMessage, SendMessage... I also did a Callback for comparison. Of course this test is not inter-process, but now you can see why I wanted to use a Callback in IPC
Maybe my test is not accounting for async vs sync (PostMessage vs SendMessage) calling methods?
Here is the meat of how I tested:
In the DLL:
Code: Select all
procedure Send_PostMessage(mode, opt: integer); stdcall;
begin
PostMessage(CB_Handle, WM_USER + 10, mode, opt);
end;
Code: Select all
procedure SetCallBackFunc(myCBFunction: TCallBackFunction); stdcall; external 'messageDLL.dll';
procedure TestCallBack(myCBFunction: TCallBackFunction); stdcall; external 'messageDLL.dll';
procedure Send_PostMessage(mode, opt: integer); stdcall; external 'messageDLL.dll';
procedure Send_SendMessage(mode, opt: integer); stdcall; external 'messageDLL.dll';
...
// for each I am doing...
SW := TStopwatch.StartNew;
for ci := 1 to NumMessages do begin
case which of
1: Send_PostMessage(1, 1);
2: Send_SendMessage(1, 1);
3: TestCallBack(CallMeTest);
end;
end;
SW.Stop();
Display(Format('%.3f seconds for '+ss, [SW.Elapsed.TotalSeconds]));
Last edited by abalonge on Wed Mar 31, 2021 4:08 pm, edited 1 time in total.
Re: Fastest way to IPC from a DLL to an EXE
I'm not sure why SendMessage is faster for you, that seems weird to me. However, I think you're testing all this within the same thread? It might make sense to use a separate thread to do the Post/SendMessage calls. That should be a better simulation about how this would perform across process boundaries than running both message sending and message receiving in the same thread.
Callbacks are ultra fast within the same process. But simple callbacks like that don't work across process boundaries.
In any case, of course you can measure SendMessage vs PostMessage across process boundaries and measure which is faster. I would assume PostMessage should be faster. Speed can be measured in different ways, though:
1) How long does the Post/SendMessage call take in the other process?
2) How high is the average delay between sending the message and receiving it in your Delphi process.
It's quite possible that some methods are better for 1) and others for 2). So in order to get a full picture, you should probably measure both.
Callbacks are ultra fast within the same process. But simple callbacks like that don't work across process boundaries.
In any case, of course you can measure SendMessage vs PostMessage across process boundaries and measure which is faster. I would assume PostMessage should be faster. Speed can be measured in different ways, though:
1) How long does the Post/SendMessage call take in the other process?
2) How high is the average delay between sending the message and receiving it in your Delphi process.
It's quite possible that some methods are better for 1) and others for 2). So in order to get a full picture, you should probably measure both.
Re: Fastest way to IPC from a DLL to an EXE
Yes, both are in the same thread. I will test in different ways, that makes sense.
Re: Fastest way to IPC from a DLL to an EXE
Thanks for all your help, madshi & iconic
Re: Fastest way to IPC from a DLL to an EXE
No problem, good luck
--Iconic
--Iconic