Click here to Skip to main content
15,880,967 members
Articles / Programming Languages / C#

Fast Asynchronous Delegates in .NET

Rate me:
Please Sign up or sign in to vote.
4.31/5 (15 votes)
1 Jul 2009CPOL2 min read 52.2K   357   53   18
Implementation of delegates which are way faster during async operations than the default delegates in .NET

Introduction

This article describes how to implement a delegate which performs much better during BeginInvoke/EndInvoke operations, than the delegates with default implementation of BeginInvoke/EndInvoke injected by CLR. The idea is rather simple, no cheats with IL and function pointers.

Background

In .NET, there is 3 ways to execute an operation or a task asynchronously. The first approach consists in using of System.Threading.ThreadPool, which suits the best in most cases. The second approach is Thread.Start, which fits better than ThreadPool, for the cases of long-running operations. And the last one is APM pattern (pair of methods BeginSomething/EndSomething) provided by all delegates and some major classes, like Stream, WebRequest, etc. APM pattern suits the best when you need to retrieve a result of an asynchronous operation.

Let's consider more closely delegates in .NET. Each delegate in .NET is decorated by C# compiler with such methods as BeginInvoke and EndInvoke. These methods at some degree are abstract - C# compiler does not provide any implementation at compile time. Implementation is injected later by CLR. BeginInvoke/EndInvoke seems to be the perfect choice when we need to retrieve a result or an absence of a result (which is called an exception) of an asynchronous operation. But the "perfect" word here is valid only in the scope of logical design. When it comes to performance, be careful: "Avoid using BeginInvoke/EndInvoke methods as much as possible. The reason is both methods internally use remoting infrastructure." - this statement I've recently encountered in one of the Microsoft's books (I've rephrased it a little bit). Basically, I was interested in the fact, how much slower than ThreadPool, for instance, are those two methods. Therefore I've implemented my own versions of BeginInvoke/EndInvoke.

Using the Code

The usage of my APM pattern implementation is exactly the same, as the default one. Here is an example:

C#
var fastDel = new FastInvokeDelegate<int>(() => MyAsyncTask(100));
var asyncResult = fastDel.BeginInvokeFast(null, null);
//do something else in parallel
int result = fastDel.EndInvokeFast(asyncResult);

Here MyAsyncOperation is some "don't-matter" function which returns an integer. As I mentioned, the usage of BeginInvokeFast/EndInvokeFast is exactly the same as BeginInvoke/EndInvoke. FastInvokeDelegate here is just a simple regular delegate decorated with extension methods BeginInvokeFast/EndInvokeFast. Looks a bit cumbersome. Unfortunately, C# compiler at the moment does not provide any better language constructs to make it more nice-looking.

Benchmarks

I've prepared a small peace of code for testing purposes:

C#
static void Main(string[] args)
{
    Func<int,int > del = (i) => 100 + i;
    var fastDel = new FastInvokeDelegate<int >(() => del(100));

    Stopwatch stopWatch = new Stopwatch();

    var asyncResults = new List<iasyncresult >(10000);

    stopWatch.Start();
    for (int i = 0; i < 10000; i++)
    {
        asyncResults.Add(del.BeginInvoke(i, null, null));
    }
    stopWatch.Stop();
    Console.WriteLine("Delegate.BeginInvoke(): {0}", stopWatch.ElapsedTicks);

    Thread.Sleep(10000);

    stopWatch.Reset();
    stopWatch.Start();
    for (int i = 0; i < 10000; i++)
    {
        del.EndInvoke(asyncResults[i]);
    }
    stopWatch.Stop();
    Console.WriteLine("Delegate.EndInvoke(): {0}", stopWatch.ElapsedTicks);

    asyncResults = new List<iasyncresult >(10000);
    GC.Collect();
    
    stopWatch.Reset();
    stopWatch.Start();
    for (int i = 0; i < 10000; i++)
    {
        asyncResults.Add(fastDel.BeginInvokeFast(null, null));
    }
    stopWatch.Stop();
    Console.WriteLine("FastInvokeDelegate.BeginInvoke(): {0}", stopWatch.ElapsedTicks);
    
    Thread.Sleep(10000);
    
    stopWatch.Reset();
    stopWatch.Start();
    for (int i = 0; i < 10000; i++)
    {
        var res = fastDel.EndInvokeFast(asyncResults[i]);    
    }
    stopWatch.Stop();
    Console.WriteLine("FastInvokeDelegate.EndInvoke(): {0}", stopWatch.ElapsedTicks);
}

Here's the output:

FastAsyncDelegates

Now you can see, why I do call my implementation "fast"...

Implementation

Everything is pretty simple. No cheats with IL, no function pointers. All I did is created a simple regular .NET delegate and decorated it with two extension methods - BeginInvokeFast and EndEnvokeFast. That's the story. The most interesting part here would be IAsyncResult implementation which does not initialize ManualResetEvent until it is needed.

C#
public delegate T FastInvokeDelegate<t>();
     
public static class DelegateExtensions
{
    public static IAsyncResult BeginInvokeFast<t>
	(this FastInvokeDelegate<t> del, object state, AsyncCallback callback)
    {
        return new FastInvokeAsyncResult<t>(del, callback, state);
    }
    
    public static T EndInvokeFast<t>(this FastInvokeDelegate<t> del, 
					IAsyncResult asyncResult)
    {
        var result = asyncResult as FastInvokeAsyncResult<t>;
        if (result == null)
            throw new InvalidOperationException("Wrong async result");
        return result.End();
    }
    
    private class FastInvokeAsyncResult<t> : IAsyncResult
    {
        private volatile int m_isCompleted; // 0==not complete, 1==complete.
        private ManualResetEvent m_asyncWaitHandle;
        private volatile int m_asyncWaitHandleNeeded = 0; //0 - is not needed, 1 - needed
        private readonly AsyncCallback m_callback;
        private readonly object m_asyncState;
        // To hold the results, exceptional or ordinary.
        private Exception m_exception;
        private T m_result;
    
        public FastInvokeAsyncResult(FastInvokeDelegate<t> work, 
				AsyncCallback callback, object state)
        {
            m_callback = callback;
            m_asyncState = state;
     
            Run(work);
        }
    
        public bool IsCompleted
        {
            get { return (m_isCompleted == 1); }
        }
        public bool CompletedSynchronously
        {
            get { return false; }
        }
        public WaitHandle AsyncWaitHandle
        {
            get
            {
                if (m_asyncWaitHandleNeeded == 1)
                {
                    return m_asyncWaitHandle;
                }
                m_asyncWaitHandleNeeded = 1;
                m_asyncWaitHandle = new ManualResetEvent(m_isCompleted == 1);
    
                return m_asyncWaitHandle;
            }
        }

        public object AsyncState
        {
            get { return m_asyncState; }
        }
        
        private void Run(FastInvokeDelegate<t> work)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                try
                {
                    m_result = work();
                }
                catch (Exception e)
                {
                    m_exception = e;
                }
                finally
                {
                    m_isCompleted = 1;
                    if (m_asyncWaitHandleNeeded == 1)
                    {
                        m_asyncWaitHandle.Set();
                    }
                    if (m_callback != null)
                        m_callback(this);
                }
            });
        }
        
        public T End()
        {
            if (m_isCompleted == 0)
            {
                AsyncWaitHandle.WaitOne();
                AsyncWaitHandle.Close();
            }
            
            if (m_exception != null)
                throw m_exception;
            return m_result;
        }
    }
}

Conclusion

Use cases for FastBeginInvoke/FastEndInvoke are exactly the same as for BeginInvoke/EndInvoke. The only difference is performance.

History

  • 1st July, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead bwin Interactive Entertainment AG
Austria Austria
The views expressed in my articles are mine and do not necessarily reflect the views of my employer.

if(youWantToContactMe)
{
SendMessage(string.Format("{0}@{1}.com", "liptchinski_vit", "yahoo"));
}

More info in my LinkedIn profile:
http://www.linkedin.com/in/vitaliyliptchinsky

Comments and Discussions

 
GeneralNice article Pin
gglaze7-Jul-09 2:39
gglaze7-Jul-09 2:39 
GeneralRe: Nice article Pin
Vitaliy Liptchinsky7-Jul-09 2:49
Vitaliy Liptchinsky7-Jul-09 2:49 
GeneralMy vote of 2 Pin
Bad code hunter1-Jul-09 19:54
Bad code hunter1-Jul-09 19:54 
GeneralRe: My vote of 2 Pin
Vitaliy Liptchinsky1-Jul-09 22:14
Vitaliy Liptchinsky1-Jul-09 22:14 
QuestionWhat is the point of callind EndInvoke multiple times? Pin
Henrique C.1-Jul-09 13:02
professionalHenrique C.1-Jul-09 13:02 
AnswerRe: What is the point of callind EndInvoke multiple times? Pin
Vitaliy Liptchinsky1-Jul-09 22:10
Vitaliy Liptchinsky1-Jul-09 22:10 
GeneralDefine fast Pin
Ramon Smits1-Jul-09 5:36
Ramon Smits1-Jul-09 5:36 
GeneralRe: Define fast Pin
Ramon Smits1-Jul-09 5:47
Ramon Smits1-Jul-09 5:47 
GeneralRe: Define fast Pin
Vitaliy Liptchinsky1-Jul-09 5:51
Vitaliy Liptchinsky1-Jul-09 5:51 
GeneralRe: Define fast Pin
Vitaliy Liptchinsky1-Jul-09 5:48
Vitaliy Liptchinsky1-Jul-09 5:48 
GeneralRe: Define fast Pin
Mike Marynowski7-Jul-09 4:08
professionalMike Marynowski7-Jul-09 4:08 
GeneralRe: Define fast Pin
gglaze7-Jul-09 2:36
gglaze7-Jul-09 2:36 
QuestionDesign choices Pin
Timothy Bussmann1-Jul-09 5:28
Timothy Bussmann1-Jul-09 5:28 
AnswerRe: Design choices Pin
Vitaliy Liptchinsky1-Jul-09 5:41
Vitaliy Liptchinsky1-Jul-09 5:41 
GeneralRe: Design choices Pin
Timothy Bussmann1-Jul-09 6:32
Timothy Bussmann1-Jul-09 6:32 
GeneralRe: Design choices Pin
Vitaliy Liptchinsky1-Jul-09 10:07
Vitaliy Liptchinsky1-Jul-09 10:07 
GeneralRe: Design choices Pin
katrash19-Aug-09 18:05
katrash19-Aug-09 18:05 
GeneralRe: Design choices Pin
Vitaliy Liptchinsky21-Aug-09 20:55
Vitaliy Liptchinsky21-Aug-09 20:55 
katrash wrote:
The use of volatile doesn't imply that it is thread safe; it only guarantees that the value is read from the actual memory and not from a cached place (CPU cache or register). Therefore, for being thread safe you must use both volatile and interlocked, or replace volatile with Interlocked.Read(...). Volatile is thread safe for read only but not read/write.


Hello,

You are describing here race conditions. This is a purely design issue and has nothing to do with volatile fields.


katrash wrote:
To get an idea why this is not thread safe, assume a volatile integer value = 1 and two threads start reading its value simultaneously. Both will receive the value of 1. Afterwards, the first thread changes it to 0 and aftes few CPU cycles the other thread sets it to 2. The final value will be 2 which is not as expected by thread 1. Of course if the first thread accesses this value it will be 2 as it will get it directly and not the cached value (0 as this thread set it previously).


Again, this is a design issue. If thread is working with shared field and this thread is not expecting this field to be changed from the outside, probably the thread shouldn't cooperate with shared state.

Vitaliy Liptchinsky

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.