mirror of
https://github.com/servo/servo.git
synced 2025-07-31 19:20:22 +01:00
Hoist lookup() into lru_cache.
MozReview-Commit-ID: F5FbKFqpXEh
This commit is contained in:
parent
13a3cf27a8
commit
05c03d5104
2 changed files with 27 additions and 25 deletions
|
@ -89,6 +89,31 @@ impl<T, A: Array<Item=Entry<T>>> LRUCache<T, A> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Performs a lookup on the cache with the given test routine. Touches
|
||||||
|
/// the result on a hit.
|
||||||
|
pub fn lookup<F, R>(&mut self, mut test_one: F) -> Option<R>
|
||||||
|
where
|
||||||
|
F: FnMut(&mut T) -> Option<R>
|
||||||
|
{
|
||||||
|
let mut result = None;
|
||||||
|
for (i, candidate) in self.iter_mut() {
|
||||||
|
if let Some(r) = test_one(candidate) {
|
||||||
|
result = Some((i, r));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match result {
|
||||||
|
None => None,
|
||||||
|
Some((i, r)) => {
|
||||||
|
self.touch(i);
|
||||||
|
let front = self.front_mut().unwrap();
|
||||||
|
debug_assert!(test_one(front).is_some());
|
||||||
|
Some(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Insert a given key in the cache.
|
/// Insert a given key in the cache.
|
||||||
pub fn insert(&mut self, val: T) {
|
pub fn insert(&mut self, val: T) {
|
||||||
let entry = Entry { val, prev: 0, next: 0 };
|
let entry = Entry { val, prev: 0, next: 0 };
|
||||||
|
|
|
@ -442,29 +442,6 @@ impl<E: TElement> SharingCache<E> {
|
||||||
};
|
};
|
||||||
self.entries.insert(StyleSharingCandidate { element, validation_data });
|
self.entries.insert(StyleSharingCandidate { element, validation_data });
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lookup<F, R>(&mut self, mut test_one: F) -> Option<R>
|
|
||||||
where
|
|
||||||
F: FnMut(&mut StyleSharingCandidate<E>) -> Option<R>
|
|
||||||
{
|
|
||||||
let mut result = None;
|
|
||||||
for (i, candidate) in self.entries.iter_mut() {
|
|
||||||
if let Some(r) = test_one(candidate) {
|
|
||||||
result = Some((i, r));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
match result {
|
|
||||||
None => None,
|
|
||||||
Some((i, r)) => {
|
|
||||||
self.entries.touch(i);
|
|
||||||
let front = self.entries.front_mut().unwrap();
|
|
||||||
debug_assert!(test_one(front).is_some());
|
|
||||||
Some(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Style sharing caches are are large allocations, so we store them in thread-local
|
/// Style sharing caches are are large allocations, so we store them in thread-local
|
||||||
|
@ -638,7 +615,7 @@ impl<E: TElement> StyleSharingCache<E> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.cache_mut().lookup(|candidate| {
|
self.cache_mut().entries.lookup(|candidate| {
|
||||||
Self::test_candidate(
|
Self::test_candidate(
|
||||||
target,
|
target,
|
||||||
candidate,
|
candidate,
|
||||||
|
@ -755,7 +732,7 @@ impl<E: TElement> StyleSharingCache<E> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.cache_mut().lookup(|candidate| {
|
self.cache_mut().entries.lookup(|candidate| {
|
||||||
debug_assert_ne!(candidate.element, target);
|
debug_assert_ne!(candidate.element, target);
|
||||||
if !candidate.parent_style_identity().eq(inherited) {
|
if !candidate.parent_style_identity().eq(inherited) {
|
||||||
return None;
|
return None;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue