so in c++ it's very easy. you want whatever class/struct to be allocated on the heap, use new. if you want it on the stack, don't use new.
in C# we always use the new keyword, and depending on whether it's a struct or a class it's allocated either on the stack or on the heap (structs go to the stack, classes to the heap) - and in some applications there can be a HUGE performance difference when changing the design such that only those objects go to the heap that really belong there.
What I wonder is - is there a direct way to control where an object is allocated independant of whether it's declared as struct or class? i know that value types (structs) can be boxed to go to the heap (but boxing/unboxing comes at a performance cost). is there a way to allocate classes on the stack?
Also, is there any mechanism to allocate raw memory and use something like placement new in C++? I know that this breaks with the idea of being managed - but it can make a big performance difference if you can use your custom memory management.
I love C# for it's convenience, for it's garbage collector and other things - but sometimes, when working on the bottleneck of an application, it meight be desirable to have more control over what is actually happening.
Any tips/hints welcome :)
edit: performance example:
struct Foo1
{
public int i;
public float f;
public double d;
}
struct Foo2
{
public Foo1[] bar;
public void Init(){
bar = new Foo1[100];
for (int i = 0; i < 100; i++)
bar[i] = new Foo1();
}
}
class Program
{
static void Main(string[] args)
{
DateTime time = DateTime.Now;
Foo2[] arr = new Foo2[1000000];
for (int i = 0; i < 1000000; i++)
{
arr[i] = new Foo2();
arr[i].Init();
}
Console.WriteLine((DateTime.Now - time).TotalMilliseconds);
}
}
This takes 1.8 seconds on my machine to execute (note that there is actually only allocation going on - no parameter passing)
if Foo1 is changed from struct to class, execution takes 8.9 seconds! that's five times slower