Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / August 2006

Tip: Looking for answers? Try searching our database.

Struct vs. Class Performance

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MLM450@hotmail.com - 29 Aug 2006 19:09 GMT
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
  );
MLM450@hotmail.com - 30 Aug 2006 14:50 GMT
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!

> When passing data to an unmanaged DLL, is there much of a performance
> difference between passing a structure versus an object that is
[quoted text clipped - 32 lines]
>    MY_RECT2 rect
>    );
Christian Fröschlin - 30 Aug 2006 15:34 GMT
> 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
[quoted text clipped - 3 lines]
> Huh? Is the class approach actually MORE efficient? Any advice would be
> appreciated. Thanks!

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.

Let us know what you find.
Mattias Sjögren - 30 Aug 2006 21:26 GMT
Christian,

>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 it's stack allocated there's no need for pinning.

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

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.

>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.

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

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Christian Fröschlin - 31 Aug 2006 09:28 GMT
Mattias,

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

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

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
pinning the array and passing an IntPtr.
MLM450@hotmail.com - 31 Aug 2006 12:24 GMT
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.

> Mattias,
>
[quoted text clipped - 23 lines]
> as this seemed to be much faster even compared to manually
> pinning the array and passing an IntPtr.
MLM450@hotmail.com - 31 Aug 2006 12:32 GMT
Christian and Mattias,
Many thanks to both of you. Your input is very helpful.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.