tag:blogger.com,1999:blog-319847463078075113.post5023969625952776313..comments2023-02-22T08:21:50.843-08:00Comments on Ybeer's .NET Blog: Techniques of calling unmanaged code from .NET and their speedHubert Kindermannhttp://www.blogger.com/profile/15165308885501948845noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-319847463078075113.post-4380472813729081192016-06-09T03:00:34.434-07:002016-06-09T03:00:34.434-07:00Is the code for this post not available anywhere?Is the code for this post not available anywhere?Harry Singletonhttps://www.blogger.com/profile/13486919608384100430noreply@blogger.comtag:blogger.com,1999:blog-319847463078075113.post-49802766872699879902013-05-02T20:29:52.492-07:002013-05-02T20:29:52.492-07:00Hi, Very nice post.
From my own experience. Expli...Hi, Very nice post.<br /><br />From my own experience. Explict PInvoke is the best performer. When I wrote my C# Wrapper Generator for C++ DLL, I thought about using calli to call the native C++ methods, but the performance was not always good and it thrown exceptions some time. So I choose to stick to Explict PInvoke, pure and clean, it is the king among all different kinds of ways of calling native methods.Anonymoushttps://www.blogger.com/profile/16934444454747657901noreply@blogger.comtag:blogger.com,1999:blog-319847463078075113.post-6727724214649131732011-07-14T05:23:04.048-07:002011-07-14T05:23:04.048-07:00My results:
MCall - it is calli standard call (an...My results:<br /><br />MCall - it is calli standard call (and fastcall at unmanaged side)<br />SCall - it is unmanaged stdcall<br /><br />SCall Calli - it is same technique as mcall, but use 'calli unmanaged stdcall ...' construction.<br /><br />OpMinus2 - two int32 args<br />OpMinus4 - four int32 args<br /><br />this results on AMD64, x64 but with x86 assemblies.<br /><br />all tests do for loop count 100 000 000.<br /><br />OpMinus2:<br />C# (informative): 332ms 1,00x<br /> MCall: 448ms 0,74x<br /> SCall Calli: 1309ms 0,25x<br /> SCall PInvoke: 658ms 0,50x<br /><br />OpMinus4:<br />C# (informative): 474ms 1,00x<br /> MCall: 736ms 0,64x<br /> SCall Calli: 1516ms 0,31x<br /> SCall PInvoke: 777ms 0,61xDmitry Azaraevhttps://www.blogger.com/profile/11316800710684850180noreply@blogger.comtag:blogger.com,1999:blog-319847463078075113.post-30086091140499525952011-07-07T02:34:02.216-07:002011-07-07T02:34:02.216-07:00Oh. When i use Reflection.Emit - i specified unman...Oh. When i use Reflection.Emit - i specified unmanaged fastcall which is not supported. In fact we need Emit managed calli with standard callconv, which internally fastcall. So great post. Thanks. :)Dmitry Azaraevhttps://www.blogger.com/profile/11316800710684850180noreply@blogger.comtag:blogger.com,1999:blog-319847463078075113.post-15660774713697890072011-07-05T01:35:11.091-07:002011-07-05T01:35:11.091-07:00I'm trying stdcall, and it works for me.
When ...I'm trying stdcall, and it works for me.<br />When i'm trying fastcall - i got exception, that unmanaged code can be called only by stdcall, cdecl or thiscall.<br />And my timings (all with supressed unmanaged code security) - calli twice slower than p/invoke, and delegates twice slower than calli.<br />So actually i don't know where i'm wrong. :)Dmitry Azaraevhttps://www.blogger.com/profile/11316800710684850180noreply@blogger.comtag:blogger.com,1999:blog-319847463078075113.post-11763721633170833252011-07-04T11:52:57.104-07:002011-07-04T11:52:57.104-07:00Hmm, interesting.. I wasn't using reflection e...Hmm, interesting.. I wasn't using reflection emit (now I see there's an option) but I was using simple library written in MSIL and I didn't realize i can somehow change the convention. Have you tried it with some other convention than fastcall? I'm interested if it works, but don't have time to test it now.Hubert Kindermannhttps://www.blogger.com/profile/15165308885501948845noreply@blogger.comtag:blogger.com,1999:blog-319847463078075113.post-68792769620743946622011-07-04T04:27:10.247-07:002011-07-04T04:27:10.247-07:00Hi, thanks, understood.
And least one question - ...Hi, thanks, understood.<br /><br />And least one question - why you wrote that calli uses fastcall convention? When i'm emiting calli instruction i can choose any calling convention...Dmitry Azaraevhttps://www.blogger.com/profile/11316800710684850180noreply@blogger.comtag:blogger.com,1999:blog-319847463078075113.post-11134412425729525192011-07-02T15:24:50.239-07:002011-07-02T15:24:50.239-07:00Hi, no I create the delagete via GetDelegateForFun...Hi, no I create the delagete via GetDelegateForFunctionPointer just once and than I call this delegate in simple for cycle. Firstly there's no reason to create new instance of delegate for function pointer each time and secondly it would be very inefficient.<br />Thanks for reading.Hubert Kindermannhttps://www.blogger.com/profile/15165308885501948845noreply@blogger.comtag:blogger.com,1999:blog-319847463078075113.post-40321206910919144372011-06-20T07:59:31.244-07:002011-06-20T07:59:31.244-07:00Hi! Good post.
How you do performance tests?
I'...Hi! Good post.<br />How you do performance tests?<br />I'm interested in function delegates method - you obtain delegate via GetDelegateForFunctionPointer and call this delegate, or you obtain delegate at each iteration?<br />May be you have sample code?Dmitry Azaraevhttps://www.blogger.com/profile/11316800710684850180noreply@blogger.com