FacticiusVir / SharpVk

C# Bindings for the Vulkan API & SPIR-V
MIT License
147 stars 18 forks source link

Improve performance of umanaged functions call #41

Closed KeKl closed 6 years ago

KeKl commented 6 years ago

I have some performance improvments:

  1. Use IL injection instead of GetDelegateForFunctionPointer

The function GetDelegateForFunctionPointer is really slow compared to a PInvoke or a Calli instruction injection into the IL .dll.

    public static unsafe void Variant(Int32 id, Int16* addr)
    {
        Silk.Cil.Ldarg(0);
        Silk.Cil.Ldarg(1);
        Silk.Cil.Load(EntryPoints);
        Silk.Cil.Ldc_I4(2366);
        Silk.Cil.Ldelem_I();
        Silk.Cil.Calli(System.Runtime.InteropServices.CallingConvention.StdCall, typeof(void), typeof(UInt32), typeof(Int16*));
        Silk.Cil.Ret();
    }

In OpenTK there is an il rewriter (example above). It injects the calli instruction into the IL. A similiar Approach was done in SharpDx. There is also a proposal https://github.com/dotnet/roslyn/issues/11475

  1. Use SuppressUnmanagedCodeSecurity Attribute for pinvoke

Use the SuppressUnmanagedCodeSecurity Attribute for Releases increases the performance and should be not a problem in the use case.

discosultan commented 6 years ago

I tested PInvoke vs delegate vs calli for vkCreateInstance. With RyuJIT (tested for x64), their perf was on par. I assume the jitter optimizes the delegate approach. With the old jitter (tested for x86), delegate was noticeably slower. Didn't delve any deeper than that, though.

KeKl commented 6 years ago

Ok, i tested this some years ago. Didn´t know that the new jitter do this better.

FacticiusVir commented 6 years ago

Hey, thanks for the feedback! I'm going to reopen this issue as the SuppressUnmanagedCodeSecurity attribute does need adding.

FacticiusVir commented 6 years ago

As 0.4.0 has moved away from using PInvoke except during function initialisation, this optimisation attribute is no longer required. I'll check if there's anything comparable required for delegate calls, but the performance recommendations above should be addressed.