When writing concurrent code, minimize blocking operations to maintain responsiveness and prevent performance bottlenecks. This applies especially to async contexts and lock usage.
When writing concurrent code, minimize blocking operations to maintain responsiveness and prevent performance bottlenecks. This applies especially to async contexts and lock usage.
For asynchronous operations:
tokio::spawn
over thread creation for concurrent tasks, as it’s more lightweightFor lock handling:
Example - Before:
{
let mut running_requests_id_to_codex_uuid = running_requests_id_to_codex_uuid.lock().await;
running_requests_id_to_codex_uuid.insert(request_id.clone(), session_id);
}
After:
running_requests_id_to_codex_uuid.lock().await.insert(request_id.clone(), session_id);
For mutex usage:
// Bad: Lock held longer than necessary
let mut flag = self.pending_redraw.lock().unwrap();
if *flag {
return;
}
*flag = true;
// Lock still held here when it's no longer needed
// Good: Lock released as soon as possible
{
let mut flag = self.pending_redraw.lock().unwrap();
if *flag {
return;
}
*flag = true;
} // Lock released here
// Continue with operations that don't need the lock
Enter the URL of a public GitHub repository