Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Type instantiation (.ctor with parameters)
object obj;
var r = 0;
const int cnt = 1000000;
var constructorInfo = typeof(Class).GetConstructor(new[] { typeof(int) })!;
var hCtor = constructorInfo.MethodHandle;
RuntimeHelpers.PrepareMethod(RuntimeMethodHandle.FromIntPtr(hCtor.Value));
var pCtor = (delegate* managed<object, int, void>)hCtor.GetFunctionPointer();
var t1 = Stopwatch.GetTimestamp();
for (int i = 0; i < cnt; i++)
{
obj = RuntimeHelpers.GetUninitializedObject(typeof(Class));
pCtor(obj, i);
var inst = Unsafe.As<object, Class>(ref obj);
r += inst._f;
}
var t2 = Stopwatch.GetTimestamp();
Console.WriteLine($"RuntimeHelpers.GetUninitializedObject + .ctor invocation via function pointer:\t{TimeSpan.FromTicks(t2 - t1).Milliseconds} ms");
t1 = Stopwatch.GetTimestamp();
for (int i = 0; i < cnt; i++)
{
obj = constructorInfo.Invoke(new object?[] { i });
var inst = Unsafe.As<object, Class>(ref obj);
r += inst._f;
}
t2 = Stopwatch.GetTimestamp();
Console.WriteLine($"ConstructorInfo.Invoke:\t{TimeSpan.FromTicks(t2 - t1).Milliseconds} ms");
t1 = Stopwatch.GetTimestamp();
for (int i = 0; i < cnt; i++)
{
var c = Activator.CreateInstance(typeof(Class), i);
var inst = Unsafe.As<object, Class>(ref c!);
r += inst._f;
}
t2 = Stopwatch.GetTimestamp();
Console.WriteLine($"Activator.CreateInstance:\t\t\t\t\t\t\t{TimeSpan.FromTicks(t2 - t1).Milliseconds} ms");
t1 = Stopwatch.GetTimestamp();
for (int i = 0; i < cnt; i++)
{
var inst = new Class(i);
r += inst._f;
}
t2 = Stopwatch.GetTimestamp();
Console.WriteLine($"Implicit call via 'new':\t\t\t\t\t\t\t{TimeSpan.FromTicks(t2 - t1).Milliseconds} ms");
Console.WriteLine();
Console.WriteLine($"Dummy output = {r}");
@dadhi
Copy link

dadhi commented Jul 16, 2022

Thanks. It is very compact and educational example of function pointer machinery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment