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 / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Question about Value Types

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Zachary  Turner - 11 Jul 2006 05:02 GMT
Suppose I have a Value Type, implemented as a struct.  Example:

struct MyValueType
{
 int Field1;
 int Field2;
}

Now, this Value Type is a member variable of a reference type with a
property get accessor.  Example:

class MyClass
{
  private MyValueType m_ValueType;

  public MyValueType Info { get m_ValueType;}
}

Now, in some other function somewhere I do somethign liek this:

MyClass class = new MyClass();
int i = class.Info.Field1;
i = class.Info.Field2;

What are the implications of these 2 lines of code from a performance
standpoint?  Does it copy the entire value type onto the stack twice?
Or is it smart enough to only do it once?  Or maybe it uses a different
paradigm altogether, since I come from a C++ background I guess there
are many things that seem "intuitive" about how unmanaged code would do
things that no longer apply in the managed world.  My concern is that I
might have a severe performance penalty when repeatedly accessing a
value type like this.

At the lowest level, what I am doing is calling unmanaged code to
populate the fields of a struct.  So naturally this needs to be a value
type.  What is the "general" way to handle this?  An obvious
alternative is to create two versions of this object - a reference type
version and an value type version.  Use the value type for the interop
only, and marshal data between value/reference types before and after
calling the unmanaged code.  Then I would work with reference types
only, perhaps making more efficient code.  Or the way I am doing it
now, which is to use the value types throughout my code.

Suggestions?  Any insight is appreciated.

Zach
Göran Andersson - 11 Jul 2006 12:55 GMT
Zachary Turner wrote:
> Suppose I have a Value Type, implemented as a struct.  Example:
>
[quoted text clipped - 12 lines]
>
>    public MyValueType Info { get m_ValueType;}

I suppose that is:

public MyValueType Info { get { return m_ValueType; } }

:)

> }
>
[quoted text clipped - 7 lines]
> standpoint?  Does it copy the entire value type onto the stack twice?
> Or is it smart enough to only do it once?

That depends on how complex the code of the property is, and how much
work the compiler will put into trying to optimize things like that.

It's plausible that the compiler might be able to recognise that the
value returned from the Info property would be the same both times. On
the other hand, it might not want to do this assumption, as
optimizations like that can potentially break code.

If you for an example have a loop that is waiting for a value to be
changed by a different thread:

while (watcher.CheckStatus) { Thread.Sleep(100); }

(Yes, not very good code, but it's just an example.)

If the compiler would make the assumption that the value can't change,
as there is no code in the loop to change it, it could optimize the code to:

bool temp = watcher.CheckStatus;
while (temp) { Thread.Sleep(100); }

That would of course send your application into an eternal slumber.

> Or maybe it uses a different
> paradigm altogether, since I come from a C++ background I guess there
> are many things that seem "intuitive" about how unmanaged code would do
> things that no longer apply in the managed world.  My concern is that I
> might have a severe performance penalty when repeatedly accessing a
> value type like this.

Copying a 16 byte structure performs about the same as copying a
reference (according to Microsoft). As long as the structure isn't
substantially larger than that, you don't have to worry unless you are
making some extreme optimization of a tight loop.

> At the lowest level, what I am doing is calling unmanaged code to
> populate the fields of a struct.  So naturally this needs to be a value
[quoted text clipped - 9 lines]
>
> Zach
Zachary  Turner - 11 Jul 2006 21:46 GMT
> Copying a 16 byte structure performs about the same as copying a
> reference (according to Microsoft). As long as the structure isn't
> substantially larger than that, you don't have to worry unless you are
> making some extreme optimization of a tight loop.

Actually my structure is more like a few hundred bytes.  Even then I
realize that optimizing it will provide little to no performance gain,
but I feel dirty when I know that I haven't written the best possible
code.  I think I'll do some kind of optimization, my choices are
mirroring the structure in a reference type, or simply returning an
object and letting CLR box and unbox my value type.  The first has the
advantage that it makes clean code and avoids unnecessary casting, the
second has the advantage that it doesn't require me to maintain the
definitions for two separate UDTs.
Göran Andersson - 11 Jul 2006 22:42 GMT
Zachary Turner wrote:
>> Copying a 16 byte structure performs about the same as copying a
>> reference (according to Microsoft). As long as the structure isn't
[quoted text clipped - 10 lines]
> second has the advantage that it doesn't require me to maintain the
> definitions for two separate UDTs.

If it's that big you should consider making it a class instead.

It should be possible to marshal the data area of the object so that it
appears to be a struct to the unmanaged code.
Zachary  Turner - 11 Jul 2006 23:18 GMT
> If it's that big you should consider making it a class instead.
>
> It should be possible to marshal the data area of the object so that it
> appears to be a struct to the unmanaged code.

If this is actually possible then that's clearly the best solution.  Do
you have any advice how I might go about doing this?  I'm a little new
to .NET in general, so I haven't seen all the backdoors and things lke
that yet.  I assume this makes some use of attributes similar to the
MarshalAs attribute?
Göran Andersson - 12 Jul 2006 09:15 GMT
Zachary Turner wrote:
>> If it's that big you should consider making it a class instead.
>>
[quoted text clipped - 6 lines]
> that yet.  I assume this makes some use of attributes similar to the
> MarshalAs attribute?

I have nothing specific, it's only based on my knowledge on how
references and pointers work.

As you are populating the struct, you have to send a pointer to the
struct to the method that populates it. I don't think that it should be
more complicated than replacing that pointer with a pointer to the
object you want to populate.

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.