mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-25 11:15:34 +00:00
8ee6cafb05
We move LRUCache from using VecDeque to ArrayDeque to avoid using heap allocations. This relies on the fix in goandylok/arraydeque#4. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #17054 (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [X] These changes do not require tests because the use cases are the same, only minimal implementation changes have been made <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> --- I additionally ran test-unit, because I'm paranoid. Source-Repo: https://github.com/servo/servo Source-Revision: 2dd67a94971bf562e2eed90582f28ef78a70cf70 --HG-- extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear extra : subtree_revision : 3bbb1feb0ae4fb0919edb4f32252594911e1cbe4
74 lines
2.1 KiB
Rust
74 lines
2.1 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
//! A simple LRU cache.
|
|
|
|
#![deny(missing_docs)]
|
|
|
|
extern crate arraydeque;
|
|
use self::arraydeque::Array;
|
|
use self::arraydeque::ArrayDeque;
|
|
|
|
/// A LRU cache used to store a set of at most `n` elements at the same time.
|
|
///
|
|
/// The most-recently-used entry is at index zero.
|
|
pub struct LRUCache <K: Array>{
|
|
entries: ArrayDeque<K>,
|
|
}
|
|
|
|
/// A iterator over the items of the LRU cache.
|
|
pub type LRUCacheIterator<'a, K> = arraydeque::Iter<'a, K>;
|
|
|
|
/// A iterator over the mutable items of the LRU cache.
|
|
pub type LRUCacheMutIterator<'a, K> = arraydeque::IterMut<'a, K>;
|
|
|
|
impl<K: Array> LRUCache<K> {
|
|
/// Create a new LRU cache with `size` elements at most.
|
|
pub fn new() -> Self {
|
|
LRUCache {
|
|
entries: ArrayDeque::new(),
|
|
}
|
|
}
|
|
|
|
/// Returns the number of elements in the cache.
|
|
pub fn num_entries(&self) -> usize {
|
|
self.entries.len()
|
|
}
|
|
|
|
#[inline]
|
|
/// Touch a given entry, putting it first in the list.
|
|
pub fn touch(&mut self, pos: usize) {
|
|
let last_index = self.entries.len() - 1;
|
|
if pos != last_index {
|
|
let entry = self.entries.remove(pos).unwrap();
|
|
self.entries.push_front(entry);
|
|
}
|
|
}
|
|
|
|
/// Iterate over the contents of this cache, from more to less recently
|
|
/// used.
|
|
pub fn iter(&self) -> arraydeque::Iter<K::Item> {
|
|
self.entries.iter()
|
|
}
|
|
|
|
/// Iterate mutably over the contents of this cache.
|
|
pub fn iter_mut(&mut self) -> arraydeque::IterMut<K::Item> {
|
|
self.entries.iter_mut()
|
|
}
|
|
|
|
/// Insert a given key in the cache.
|
|
pub fn insert(&mut self, key: K::Item) {
|
|
if self.entries.len() == self.entries.capacity() {
|
|
self.entries.pop_back();
|
|
}
|
|
self.entries.push_front(key);
|
|
debug_assert!(self.entries.len() <= self.entries.capacity());
|
|
}
|
|
|
|
/// Evict all elements from the cache.
|
|
pub fn evict_all(&mut self) {
|
|
self.entries.clear();
|
|
}
|
|
}
|