I'd like to use async/await in the context of a game engine where high framerate is important and garbage collection halts should be avoided. Idiomatic async/await appears to rely heavily on heap allocation. How can I reduce this?
Task<T>s allocate on the heap, await appears to cause allocations, and task cancellation through CancellationToken.ThrowIfCancellationRequested generate a large stacktrace. In fact, it appears that there is no way to cancel a task without allocating an exception and stack trace.
I could implement task cancellation like so but it seems counter intuitive to not use IsCancelled.
public async Task<T> Method<T>(CancellationToken cancellationToken)
{
// Do not do this as it could cause a TaskCanceledException
//await Task.Delay(1000, cancellationToken);
await Task.Delay(1000);
if (cancellationToken.IsCancellationRequested)
{
//perhaps return null to show a cancellation?
return default;
}
//uncancelled work...
}
Is there a more idiomatic way to do this?