title: async and fast path optimization
body: <p>i was doing some refactoring on an OWIN project and i came across some issue about
  c# fast path optimization.</p>
  
  <p>So i read about best practises regarding pre-computed Tasks, make a method async or not and stuff like that.</p>
  
  <p>More precisely, on the fast path optimization:</p>
  
  <p>Task-based Asynchronous Pattern best practices (fast path p9) <a href="http://www.microsoft.com/en-us/download/details.aspx?id=19957" rel="nofollow">http://www.microsoft.com/en-us/download/details.aspx?id=19957</a></p>
  
  <p>same guidance as for iterator, wich was as first more understandable for me as i'm quite new to TAP
  <a href="http://social.msdn.microsoft.com/Forums/en-US/d9146792-1b9a-4807-a42e-29107c281cc4/can-awaiting-taskfromresult-introduce-concurrency?forum=async" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/d9146792-1b9a-4807-a42e-29107c281cc4/can-awaiting-taskfromresult-introduce-concurrency?forum=async</a></p>
  
  <p>So here is the code i came across:</p>
  
  <pre><code>public class CustomMiddleware2 : OwinMiddleware
  {
      public override async Task Invoke(IOwinContext context)
      {
          await InvokeInternal(context);
      }
  
      private Task InvokeInternal(IOwinContext context)
      {           
          if (Wacko.WhatDOuKnow)
          {
              context.Response.Redirect("/awesomeurl");
  
              //fast path
              return Task.FromResult&lt;object&gt;(null);
          }
          return Next.Invoke(context);
      }
  
      public CustomMiddleware2(OwinMiddleware next)
          : base(next)
      {
  
      }
  
  }
  </code></pre>
  
  <p>and here is the replacement i actually plan to do:</p>
  
  <pre><code>public class BetterOrNotMiddleware : OwinMiddleware
  {
      public override Task Invoke(IOwinContext context)
      {
          if (Wacko.WhatDOuKnow)
          {
              context.Response.Redirect("/awesomeurl");
  
              //fast path
              return Task.FromResult&lt;object&gt;(null);
  
          }
          else
              return InvokeInternal(context);
      }
  
      private async Task InvokeInternal(IOwinContext context)
      {
          await Next.Invoke(context);
      }
  
      public BetterOrNotMiddleware(OwinMiddleware next)
          : base(next)
      {
  
      }
  
  }
  </code></pre>
  
  <p>The thing is, there is not so much differences. I'm not sure the BetterOrNotMiddleware class is more optimized or proper?
  it feel more adequate to me to await on the Next.Invoke call, and the code may feel more understandable because
  it's the same pattern for exception handling with iterators(link above)</p>
  
  <p>i may be missing something about async state machine or call stack wich would validate the second approach, but i'm too new to async to get it.</p>
  
  <p>but if all this is just a micro optimization i'm not sure i want to bother to change all the middlewares in the application. Well, i may nonetheless</p>