Skip to content

Commit ad4062e

Browse files
committed
Eliminate unecessary extra sigil node from LruCache.
Instead of allocating both head and tail nodes for the ends of the node list, a single node can be allocated and linked circularly instead, making it act as both the head and the tail of the list at the same time.
1 parent 0478454 commit ad4062e

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

src/libcollections/lru_cache.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ pub struct LruCache<K, V> {
5959
map: HashMap<KeyRef<K>, ~LruEntry<K, V>>,
6060
max_size: uint,
6161
head: *mut LruEntry<K, V>,
62-
tail: *mut LruEntry<K, V>,
6362
}
6463

6564
impl<S, K: Hash<S>> Hash<S> for KeyRef<K> {
@@ -103,11 +102,10 @@ impl<K: Hash + TotalEq, V> LruCache<K, V> {
103102
map: HashMap::new(),
104103
max_size: capacity,
105104
head: unsafe{ cast::transmute(~LruEntry::<K, V>::new()) },
106-
tail: unsafe{ cast::transmute(~LruEntry::<K, V>::new()) },
107105
};
108106
unsafe {
109-
(*cache.head).next = cache.tail;
110-
(*cache.tail).prev = cache.head;
107+
(*cache.head).next = cache.head;
108+
(*cache.head).prev = cache.head;
111109
}
112110
return cache;
113111
}
@@ -191,7 +189,7 @@ impl<K: Hash + TotalEq, V> LruCache<K, V> {
191189
#[inline]
192190
fn remove_lru(&mut self) {
193191
if self.len() > 0 {
194-
let lru = unsafe { (*self.tail).prev };
192+
let lru = unsafe { (*self.head).prev };
195193
self.detach(lru);
196194
unsafe {
197195
match (*lru).key {
@@ -269,7 +267,6 @@ impl<K, V> Drop for LruCache<K, V> {
269267
fn drop(&mut self) {
270268
unsafe {
271269
let _: ~LruEntry<K, V> = cast::transmute(self.head);
272-
let _: ~LruEntry<K, V> = cast::transmute(self.tail);
273270
}
274271
}
275272
}

0 commit comments

Comments
 (0)