VizOne
February 28th, 2003, 12:14 PM
Hi!
I am working on a function that returns an object initilized with some
specific values, like
__value struct MyStruct
{
public:
MyStruct(int a, int b, int c)
: a(in_a), b(in_b), c(in_c) {}
static MyStruct One() { return MyStruct(1,1,1); }
private:
int a, b, c;
};
This is compiled to the following MSIL-code:
.method public static valuetype Cpp.MyStruct
One() cil managed
{
// Code size 26 (0x1a)
.maxstack 4
.locals (valuetype Cpp.MyStruct V_0)
IL_0000: ldloca.s V_0
IL_0002: initobj Cpp.MyStruct
IL_0008: ldloca.s V_0
IL_000a: ldc.i4.1
IL_000b: ldc.i4.1
IL_000c: ldc.i4.1
IL_000d: call instance void Cpp.MyStruct::.ctor(int32,
int32,
int32)
IL_0012: ldloca.s V_0
IL_0014: ldobj Cpp.MyStruct
IL_0019: ret
} // end of method MyStruct::One
I looked at
IL_0002 and found a initobj. I tried to avoid initializing of
the members, so I defined an empty default c'tor:
MyStruct() {}
However, now my IL code expanded to:
.method public static valuetype Cpp.MyStruct
One() cil managed
{
// Code size 35 (0x23)
.maxstack 4
.locals (valuetype Cpp.MyStruct V_0,
valuetype Cpp.MyStruct V_1)
IL_0000: ldloca.s V_1
IL_0002: initobj Cpp.MyStruct
IL_0008: ldloca.s V_1
IL_000a: ldc.i4.1
IL_000b: ldc.i4.1
IL_000c: ldc.i4.1
IL_000d: call instance void Cpp.MyStruct::.ctor(int32,
int32,
int32)
IL_0012: ldloca.s V_0
IL_0014: ldloca.s V_1
IL_0016: cpobj Cpp.MyStruct
IL_001b: ldloca.s V_0
IL_001d: ldobj Cpp.MyStruct
IL_0022: ret
} // end of method MyStruct::One
Which contains a cpobj and two local instances of MyStruct and therefore
seems to be even worse.
I wrote a similar struct in C# to compare compilation output:
public struct MyStruct
{
public MyStruct(int in_a, int in_b, int in_c)
{
a = in_a;
b = in_b;
c = in_c;
}
public static MyStruct One() { return new MyStruct(1, 1, 1); }
int a, b, c;
};
This compiled to the following code:
.method public hidebysig static valuetype CSharp.MyStruct
One() cil managed
{
// Code size 9 (0x9)
.maxstack 8
IL_0000: ldc.i4.1
IL_0001: ldc.i4.1
IL_0002: ldc.i4.1
IL_0003: newobj instance void CSharp.MyStruct::.ctor(int32,
int32,
int32)
IL_0008: ret
} // end of method MyStruct::One
Now I tested execution time and I am not really suprised: the the C++ struct
without default c'tor was about 30% slower than the C# version. The C++
Version with default c'tor even was 70% slower than the C# version.
Now I have two questions: first, is the C# version creating the object on
the heap or the stack?
And: how can I make the C++ version compile to the faster version?
Thanks in advance!
- Andre
I am working on a function that returns an object initilized with some
specific values, like
__value struct MyStruct
{
public:
MyStruct(int a, int b, int c)
: a(in_a), b(in_b), c(in_c) {}
static MyStruct One() { return MyStruct(1,1,1); }
private:
int a, b, c;
};
This is compiled to the following MSIL-code:
.method public static valuetype Cpp.MyStruct
One() cil managed
{
// Code size 26 (0x1a)
.maxstack 4
.locals (valuetype Cpp.MyStruct V_0)
IL_0000: ldloca.s V_0
IL_0002: initobj Cpp.MyStruct
IL_0008: ldloca.s V_0
IL_000a: ldc.i4.1
IL_000b: ldc.i4.1
IL_000c: ldc.i4.1
IL_000d: call instance void Cpp.MyStruct::.ctor(int32,
int32,
int32)
IL_0012: ldloca.s V_0
IL_0014: ldobj Cpp.MyStruct
IL_0019: ret
} // end of method MyStruct::One
I looked at
IL_0002 and found a initobj. I tried to avoid initializing of
the members, so I defined an empty default c'tor:
MyStruct() {}
However, now my IL code expanded to:
.method public static valuetype Cpp.MyStruct
One() cil managed
{
// Code size 35 (0x23)
.maxstack 4
.locals (valuetype Cpp.MyStruct V_0,
valuetype Cpp.MyStruct V_1)
IL_0000: ldloca.s V_1
IL_0002: initobj Cpp.MyStruct
IL_0008: ldloca.s V_1
IL_000a: ldc.i4.1
IL_000b: ldc.i4.1
IL_000c: ldc.i4.1
IL_000d: call instance void Cpp.MyStruct::.ctor(int32,
int32,
int32)
IL_0012: ldloca.s V_0
IL_0014: ldloca.s V_1
IL_0016: cpobj Cpp.MyStruct
IL_001b: ldloca.s V_0
IL_001d: ldobj Cpp.MyStruct
IL_0022: ret
} // end of method MyStruct::One
Which contains a cpobj and two local instances of MyStruct and therefore
seems to be even worse.
I wrote a similar struct in C# to compare compilation output:
public struct MyStruct
{
public MyStruct(int in_a, int in_b, int in_c)
{
a = in_a;
b = in_b;
c = in_c;
}
public static MyStruct One() { return new MyStruct(1, 1, 1); }
int a, b, c;
};
This compiled to the following code:
.method public hidebysig static valuetype CSharp.MyStruct
One() cil managed
{
// Code size 9 (0x9)
.maxstack 8
IL_0000: ldc.i4.1
IL_0001: ldc.i4.1
IL_0002: ldc.i4.1
IL_0003: newobj instance void CSharp.MyStruct::.ctor(int32,
int32,
int32)
IL_0008: ret
} // end of method MyStruct::One
Now I tested execution time and I am not really suprised: the the C++ struct
without default c'tor was about 30% slower than the C# version. The C++
Version with default c'tor even was 70% slower than the C# version.
Now I have two questions: first, is the C# version creating the object on
the heap or the stack?
And: how can I make the C++ version compile to the faster version?
Thanks in advance!
- Andre