locked
Await Overhead Approximately 50 + (n * 15) ms

    General discussion

  • Considering the state gymnastics being performed, the Await operator is very efficient.  The performance penalty on a fast machine appears to be approximately:

    50 + (n * 15) milliseconds

    Where 50 ms is a one time setup delay deferred until runtime the first time an asynchronous method encounters an Await operator.  The penalty for each Await operation (including the first one) is approximately 15 ms. 

    I arrived at these rough estimates by comparing the times required to load images synchronously vs. asynchronously.  Obviously your results will vary depending on the speed of your machine, the size of the state being saved and reloaded, and the number of competing threads that might delay scheduling the continuance. 

    However this little exploration suggests to me that:

    1) It doesn't make sense to Await tasks which RELIABLY consume <= 20 ms.  Obviously an unreliable task, even a very short one say 1 or 2 ms could still be a very good candidate for Awaiting.  For example if an IO task fails 1 out of 100 times it should be considered to be very unreliable and Awaiting the completion of this task would be one way to manage this uncertainty.

    2) There is zero penalty for coding an Async method if the Await operator is never encountered.  This makes it painless to write methods which can run synchronously or asynchronously depending on an IsAsync parameter passed to the method.  In pseudo code something like this:

     

    Private Async Sub DoSomething(ByVal IsAsync as Boolean) 
    
     Dim Result as T
     If IsAsync Then
     Result = Await SomethingTask
     Else
     Result = Something
     End if
    
    
     UseResult(Result)
    End Sub Private Function Something() as T Dim Result as T ... Return Result End Function Private Function SomethingTask() as Task(of T) Dim f = New Func(of T)(AddressOf Something) Dim s = TaskScheduler.FromCurrentSynchronizationContext Dim tf = New TaskFactory(of T)(s) Return tf.StartNew(f) End Function
    (Note the CurrentSynchronizationContext used above is only required if your Async task needs to run on the same thread as the Async method was invoked on. In Silverlight ANYTHING that touches the UI must be 
    on the UI thread for example.)


    BTW: CONSIDERING THAT THIS IS A MICROSOFT FORUM WITH PEOPLE WANTING TO INCLUDE C# OR VB CODE COULDN'T SOMEONE DO A LITTLE BETTER JOB OF CREATING AN EDITOR THAT PROPERLY FORMATS POSTS CONTAINING CODE???  I'M TIRED OF WRESTLING WITH THIS EVERY TIME I MAKE A POST!!

     

    Monday, August 01, 2011 12:42 PM

All replies

  • Very nice knowledge you just gave there !... I've considdered only heavy calls as candidates for Async, but it looks like people are using Async/Await for 'mathematic' calls as well !? 

    I also considder writing async methods as more difficult / slower then normal calls... so i only use async as a refactor / optimization of my existing code. 

    Regarding posting Code... have you tried the 'Insert Code Block' button ?... an easy trick to know, is always insert 3-5 newlines and then paste the code into #2 line so that you have a free line to write on after the codeblock... 

    Wednesday, August 03, 2011 9:26 AM
  • Very nice knowledge you just gave there !... I've considdered only heavy calls as candidates for Async, but it looks like people are using Async/Await for 'mathematic' calls as well !? 

    I also considder writing async methods as more difficult / slower then normal calls... so i only use async as a refactor / optimization of my existing code. 

    Regarding posting Code... have you tried the 'Insert Code Block' button ?... an easy trick to know, is always insert 3-5 newlines and then paste the code into #2 line so that you have a free line to write on after the codeblock... 


    Thanks All,

    I'm currently developing a thumbnail image browser which demonstrates several facets of using the Async CTP.  I'll post the control and source code on my blog soon.

    I consider Async to be a major breakthrough, on the order of LINQ, because it enables complex asynchronous scenarios to be coded in a way that can be read and understood. 

    State management has been an area that has been somewhat neglected until now.  For example we ought to be able to write transactional methods such that if everything completes successfully the state changes brought about are committed, whereas exceptions would rollback any state changes as if the method had never been invoked.  Central to this approach would be a new type of function that would return either a result or a report on the status of the call. 

    This IS essentially what we have  with an Async task. 

    I'll be able to talk more intelligently about this after I've got more experience with the nuances of Async, but I think something much more important is happening here than just making asynchronous code more readable ... more when I'm smart enough to articulate it.

    Thanks for the tip on workiing around the frailties of our forum software.  Yes I've used the code block button and selected VB.NET but the results are terrible and very difficult to edit. 

    Come on!  This is a MICROSOFT forum.  Any company with enough brainpower to deliver a tool like VS2010 should certainly be able to set the bar for forum software!  Instead of a hokey HTML formatted "code block" , a forum post should be able to include a "LIVE" code window operating in "code lab" mode (like a debugger). Readers could run or step through the samples varying inputs or making changes to their instance.  A single button click would download the samples to a designated folder.  And how about a VB <-> C# translator so that the C# guys could get that warm fuzzy semi colon/curly bracket feeling.

     

    Wednesday, August 03, 2011 2:10 PM
  • try http://coderun.com/ide/ and get your mind blown !

    but yeah... Code Pasting should be awesome... the problem is that it takes a hell lot of Javascript --
    i know, coz i wrote this : http://home.mdk-photo.com/Editor 

    Wednesday, August 03, 2011 2:19 PM
  • Montago,

    Yes CodeRun is very impressive!  Who's behind it? I wish it supported VB.

    YES if something like CodeRun could be integrated with Forum software it would be awesome.

    I couldn't get your mdk-photo.com/Editor link to open.  I'm in Thailand and sometimes my Internet connection is poor.

    Mark

     

    Found this:

    http://weblogs.asp.net/hajan/archive/2010/09/19/using-coderun-browser-based-ide-to-create-asp-net-web-applications.aspx

     

     


    Wednesday, August 03, 2011 2:57 PM
  • Hello, Mark -

    OT, but I wanted to say that I think the app you're developing is cool.

    I spent a month in Thailand in 2006, shortly before the military coup. I taught English and Math in an orphanage. It was such a great time, and I'm planning to return with my family... someday when we can afford it. :)

    I barely learned any Thai, though; do you speak it?

           -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
    Thursday, August 04, 2011 9:15 PM
  • Hello, Mark -

    OT, but I wanted to say that I think the app you're developing is cool.

    I spent a month in Thailand in 2006, shortly before the military coup. I taught English and Math in an orphanage. It was such a great time, and I'm planning to return with my family... someday when we can afford it. :)

    I barely learned any Thai, though; do you speak it?

           -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible

    Thanks Steve!

    Yes I speak and read Thai.  I've got high hopes for the software I'm creating and a powerful way to market it once I'm ready.  Why don't you help me create it?  Then you'll have plenty of $ to come visit here or move here if you want to.  Good time to have lots of options for your family.

    Mark

     

    Sunday, August 07, 2011 8:09 AM