mgravell/PooledAwait

Don't alloc TCS

Closed this issue · 1 comments

Currently in the Task[<T>] API we use TaskCompletionSource<T> to control the Task<T>. This works, but involves an extra allocation. TCS is just a flat wrapper around a Task<T>, with privileged access to call methods. We should attempt to do this with optimized reflection instead, and drop the TCS alloc.

Requirements:

  • must do a speculative "does this all work as expected" test
  • must assign the result of that test to a static readonly bool (it can be non-readonly in DEBUG for test purposes) as part of a static field initializer (not a static constructor)
  • must test this before allocating, using a direct allocated Task<T> if it works
  • otherwise use the fallback strategy of allocating the TCS
  • tests must cover both scenarios, presumably by flipping the flag in DEBUG

Core logic implemented by ValueTaskCompletionSource<T>. Needs tests etc.