Z
Zachary Turner
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
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