all groups > dotnet interop > august 2006 >
You're in the

dotnet interop

group:

Struct vs. Class Performance


Struct vs. Class Performance MLM450 NO[at]SPAM hotmail.com
8/29/2006 11:09:54 AM
dotnet interop: When passing data to an unmanaged DLL, is there much of a performance
difference between passing a structure versus an object that is
marshalled as a structure? Is there another option I have not thought
of? Below is an example...

Thanks!

// Structure option
internal struct MY_RECT
{
public uint left;
public uint top;
public uint right;
public uint bottom;
}

[DllImport(ImageGearLibName)]
public static extern int Foo(
ref MY_RECT rect
);

// Class option
[StructLayout(LayoutKind.Sequential)]
internal class MY_RECT2
{
public uint left;
public uint top;
public uint right;
public uint bottom;
}

[DllImport(ImageGearLibName)]
public static extern int Foo(
[MarshalAs(UnmanagedType.LPStruct)]
MY_RECT2 rect
);
Re: Struct vs. Class Performance MLM450 NO[at]SPAM hotmail.com
8/30/2006 6:50:14 AM
I tried running a test to see how much better using a struct is. I made
a loop that calls the DLL function 2.5 million times. First I ran using
a struct, then changed it to use a class and ran again. I did the test
3 times and each run had very similar results. The struct processing
took about 45 seconds while the class processing took about 41 seconds.
Huh? Is the class approach actually MORE efficient? Any advice would be
appreciated. Thanks!

[quoted text, click to view]
Re: Struct vs. Class Performance Christian_Fröschlin
8/30/2006 4:34:25 PM
[quoted text, click to view]

First of all, you should call both functions at least once before
you start measuring anything, or you might be measuring the time
for just-in-time compilation as well.

I would not expect much of a difference between the two approaches
anyway. While a struct sounds more low-level, it resides in managed
memory just like a class and will be pinned (and possibly copied
and layouted?) before passing a pointer as well.

If the performance is important for your application and you need
to call the native method very often, you could also try to reuse
the same struct for multiple invocations and keep it manually pinned,
passing the IntPtr to your method. Have a look at class GCHandle and
its methods Alloc and AddrOfPinnedObject.

Mind that there may be hidden penalties for keeping objects
pinned when memory is allocated or garbage is collected.

Finally, your struct could actually be represented as an
array with four elements. Passing a blittable array may be
a lot faster than passing a struct. You could even wrap it
in a class to provide friendly property accessors.

Re: Struct vs. Class Performance Mattias Sjögren
8/30/2006 10:26:45 PM
Christian,

[quoted text, click to view]

If it's stack allocated there's no need for pinning.


[quoted text, click to view]

Exactly, that's why I could see a small advantage to using a struct in
this case. But I doubt the difference is of any significance in a real
application.


[quoted text, click to view]

Why woud it be faster to pass a blittable array than a blittable
struct?


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: Struct vs. Class Performance Christian_Fröschlin
8/31/2006 12:00:00 AM
Mattias,

[quoted text, click to view]

Both of these are of course very good points ;)

My reply was a bit biased regarding my last evaluation of
struct vs. array performance in a more specific context,
where the data needed to persist as a class member and
was therefore never stack-based, also, my functions
expected arrays of structs rather than structs.

I don't think I have the code anymore, but there was a
significant difference between passing an array of structs
containing a single blittable member and a simple array of
that blittable type, although I would have expected both
of these to represent the same blittable data. You can
look for an older thread in this newsgroup titled
"P/Invoke efficiency for structs and arrays".

At the time I concluded there were some nifty optimizations
going on when passing arrays of basic types from managed heap,
as this seemed to be much faster even compared to manually
Re: Struct vs. Class Performance MLM450 NO[at]SPAM hotmail.com
8/31/2006 4:24:52 AM
Although the comparison of using an array versus not using one has its
place, it does not impact my situation. The example code in my initial
post was just that, an example. The structure I showed was one of many
I am dealing with. The others are not as simple.

[quoted text, click to view]
Re: Struct vs. Class Performance MLM450 NO[at]SPAM hotmail.com
8/31/2006 4:32:08 AM
Christian and Mattias,
Many thanks to both of you. Your input is very helpful.
AddThis Social Bookmark Button