diff --git a/src/cortex-engine/src/async_utils.rs b/src/cortex-engine/src/async_utils.rs index f7b0490ad..a7735a7a8 100644 --- a/src/cortex-engine/src/async_utils.rs +++ b/src/cortex-engine/src/async_utils.rs @@ -3,6 +3,7 @@ //! Provides utilities for async operations including //! timeouts, retries, and concurrency control. +use std::collections::VecDeque; use std::future::Future; use std::pin::Pin; use std::sync::Arc; @@ -213,8 +214,10 @@ impl Default for AsyncOnce { } /// Async queue. +/// +/// Uses VecDeque internally for O(1) push and pop operations. pub struct AsyncQueue { - items: RwLock>, + items: RwLock>, capacity: Option, } @@ -222,7 +225,7 @@ impl AsyncQueue { /// Create a new queue. pub fn new() -> Self { Self { - items: RwLock::new(Vec::new()), + items: RwLock::new(VecDeque::new()), capacity: None, } } @@ -230,7 +233,7 @@ impl AsyncQueue { /// Create with capacity. pub fn with_capacity(capacity: usize) -> Self { Self { - items: RwLock::new(Vec::with_capacity(capacity)), + items: RwLock::new(VecDeque::with_capacity(capacity)), capacity: Some(capacity), } } @@ -245,18 +248,17 @@ impl AsyncQueue { return false; } - items.push(item); + items.push_back(item); true } /// Pop an item. + /// + /// Uses VecDeque::pop_front() for O(1) complexity instead of + /// Vec::remove(0) which has O(n) complexity. pub async fn pop(&self) -> Option { let mut items = self.items.write().await; - if items.is_empty() { - None - } else { - Some(items.remove(0)) - } + items.pop_front() } /// Get length.