nuno-faria commented on code in PR #17031: URL: https://github.com/apache/datafusion/pull/17031#discussion_r2255095258
########## datafusion/execution/src/cache/lru_queue.rs: ########## @@ -0,0 +1,490 @@ +use std::{ + collections::HashMap, + hash::Hash, + sync::{Arc, Mutex, Weak}, +}; + +#[derive(Default)] +/// Provides a Least Recently Used queue with unbounded capacity. +/// +/// # Examples +/// +/// ``` +/// use datafusion_execution::cache::lru_queue::LruQueue; +/// +/// let mut lru_queue: LruQueue<i32, i32> = LruQueue::new(); +/// lru_queue.put(1, 10); +/// lru_queue.put(2, 20); +/// lru_queue.put(3, 30); +/// assert_eq!(lru_queue.get(&2), Some(&20)); +/// assert_eq!(lru_queue.pop(), Some((1, 10))); +/// assert_eq!(lru_queue.pop(), Some((3, 30))); +/// assert_eq!(lru_queue.pop(), Some((2, 20))); +/// assert_eq!(lru_queue.pop(), None); +/// ``` +pub struct LruQueue<K: Eq + Hash + Clone, V> { Review Comment: This implementation uses no unsafe blocks. The "unsafest" part is when we upgrade the `Weak` pointers in the doubly-linked list and then unwrap them, however we guarantee that the strong reference is always in the `data` map. While I believe this implementation could be more efficient, I tried to keep it as simple as possible (e.g., `get` does a `remove` and a `put`, instead of something more complex). A quick bench shows that it reaches >1M puts/gets/pops per second, which should be enough. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org