Monday, July 7, 2014

Vote for "Unmanaged generic type constraint + generic pointers" uservoice idea for .NET

If you care about performance, maybe because you are working with big data, image analysis, computer vision, machine learning etc., please go to Visual Studio user voice and vote for:

Microsoft are taking these user voice ideas serious (very good) and are in fact currently working on C# and SIMD in the form of, at least for now, an out-of-band library Microsoft.Bcl.Simd and the new Just-in-Time compiler RyuJIT.

Getting support for an unmanaged generic type constraint and generic pointers combined with SIMD operations based on unsafe pointers would be absolutely awesome. Although, the System.Numerics.Vector types and methods in the current version 1.0.2-beta of Microsoft.Bcl.Simd are very limited in type support and have no support for creating "vectors" over pointers.

Currently, the lack of unmanaged generic type constraint and generic pointers means that, if you are doing image processing on different primitive types byte, sbyte, short, ushort, int, uint, long, ulong, float, double etc., then you have to repeat code for each of these - if you care about type safety and performance - leading to massive code bloat and combinatorial explosions where I have seen methods with thousands of overloads (using T4 code generation), which Visual Studios intellisense simply can't handle. Getting the above feature would allow a lot of this code to be boiled down to a single generic method e.g. (a draft example using value type trick to get inlining inside execution loop):

    public interface IFunc<in T, out TResult>
        TResult Invoke(T arg);

    public struct Threshold : IFunc<int, byte>
        readonly int m_threshold;

        public Threshold(int threshold)
            m_threshold = threshold;

        // Since this is a value type it will be inlined by JIT in release
        public byte Invoke(int value)
            return value > m_threshold ? byte.MaxValue : byte.MinValue;

    public static class Transforms
        public unsafe static TFunc Transform<T, TResult, TFunc>(
            ArrayPtr2D<T> src, TFunc func, ArrayPtr2D<TResult> dst)
            where T : unmanaged
            where TFunc : struct, IFunc<T, TResult>
            if (src.Size != dst.Size)
            { throw new ArgumentException("Arrays must have same size"); }

            var width = src.Size.Width;
            var height = src.Size.Height;

            var srcStride = src.StrideInBytes;
            T* srcRowPtr = src.DataPtr;
            var srcRowPtrEnd = ((byte*)srcRowPtr) + srcStride * height;

            var dstStride = dst.StrideInBytes;
            TResult* dstRowPtr = dst.DataPtr;

            for (; srcRowPtr != srcRowPtrEnd;
                   srcRowPtr = (T*)(((byte*)srcRowPtr) + srcStride),
                   dstRowPtr = (TResult*)(((byte*)dstRowPtr) + dstStride))
                var srcColPtr = srcRowPtr;
                var srcColPtrEnd = srcColPtr + width;
                var dstColPtr = dstRowPtr;
                for (; srcColPtr != srcColPtrEnd; ++srcColPtr, ++dstColPtr)
                    *dstColPtr = func.Invoke(*srcColPtr);
            return func;

    public class Program
        public static void Main()
            // Initialize from some existing native data i.e. from a bitmap etc.
            ArrayPtr2D<int> src = ...;
            ArrayPtr2D<byte> dst = ...;
            // Unsafe, fast, loop with threshold invoke inlined
            Transforms.Transform(src, new Threshold(1275), dst); 
Note how due to the transform having type 'T' as input and type 'TResult' as output we can handle a lot of combinations with this, without repeating the code. The JIT will, of course, have to generate specific code for each actual type usage etc. However, this is exactly what we want. Optimized code for each specific type combination and value type func. Who wouldn't want that ;)

F# already has the unmanaged generic type constraint as can be seen in Constraints (F#), we just need this in C# and generic pointers to unmanaged types.

So perhaps instead of obsessing about Change All CAPS Menu in VS 2012 to VS Beta format File Edit Instead of FILE EDIT (although I agree ALL CAPS is a terrible design choice) vote for something that we could all enjoy, less code and higher performance ;)


  1. reviseted this by accident (second time (:

    . funny as i re-resarched this issue . back then it was regarding byte[] , now it's to try and outperform List or List or List with testings of custom
    char[] for strings and structs i named NusT made out of blittble .... NusT[] being unsafe vs manged ... so i revsited this issue of copy as i try to track the basics of list internally which is using array.copy()... remove all un needed body to be light weight as possible... many things i need to do. so when i was visiting here back in 2013 it was for image processing period, now it's all started when i realized how much garbage i am loading when working with objects , having freely instantiating all over the code not paying attention to costs of calls etc ... i now redesigning my projects basic structures so ... feel free to reply if you have any insights from your much greater experiance in c# and specially performance issues

  2. Yes, although our Think Tank could not figure out the answer to this challenge, it just shows you how tough our future might be when it comes to secure and safe elections. Please think on this.

  3. Best Real Money Casino Apps in USA 2021 - CasinoWow
    Slots Casino — One of the most recognizable 1xbet login online slots games around. This game's poormansguidetocasinogambling most recent is the Playtech 🏆 Best 1등 사이트 Real Money Casino App: SlotWolf🎁 #1 USA Casino Bonus: Risk Free Spins for $1,000🏆 Best Real Money Casino worrione App: SlotsMillion