.NET › Diagnostics & Performance
How does async/await performance compare to synchronous code, and what overhead does the async state machine introduce?
AdvancedAnswer
Each async method is compiled into a state machine (a class or struct implementing IAsyncStateMachine) that captures local variables and tracks execution position across await points, introducing some overhead (allocation for the state machine if it can't be a struct, plus continuation scheduling) compared to purely synchronous code -- but this overhead is typically negligible compared to the benefit of not blocking a thread during genuinely asynchronous I/O operations (network calls, file access, database queries), where the alternative (blocking a thread pool thread) would be far more costly at scale under concurrent load.
// Async state machine overhead is real but usually negligible relative to the I/O wait time
public async Task<Order> GetOrderAsync(int id) {
return await _db.Orders.FindAsync(id); // thread NOT blocked during the DB roundtrip
}
// vs blocking equivalent (bad under load):
public Order GetOrder(int id) {
return _db.Orders.Find(id); // blocks a thread pool thread for the entire DB roundtrip
}Real-world example A benchmark comparing sync and async versions of a database-heavy endpoint under load shows the async version sustaining several times higher throughput under concurrency, since it doesn't tie up thread pool threads waiting on I/O, despite each individual async call having marginally higher per-call overhead.