Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel comemo & optimizations #5

Merged
merged 28 commits into from
Dec 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Atomic age (faster)
  • Loading branch information
Dherse committed Dec 9, 2023
commit 47ca59be1c600baa43843b26dc6554a12fa0c5fe
27 changes: 14 additions & 13 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 1,6 @@
use std::borrow::Cow;
use std::hash::Hash;
use std::sync::atomic::Ordering;
use std::{borrow::Cow, sync::atomic::AtomicUsize};
Dherse marked this conversation as resolved.
Show resolved Hide resolved

use hashbrown::HashMap;
use parking_lot::{Mutex, RwLock};
Expand Down Expand Up @@ -43,7 44,7 @@ where
};

// Check if there is a cached output.
let mut borrow = cache.write();
let borrow = cache.read();
if let Some((constrained, value)) = borrow.lookup::<In>(key, &input) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if let Some((constrained, value)) = borrow.lookup::<In>(key, &input) {
if let Some((constraint, value)) = borrow.lookup::<In>(key, &input) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this because there is another value of type In::Constraint that is already called constraint.

// Replay the mutations.
input.replay(constrained);
Expand All @@ -68,7 69,7 @@ where
outer.join(constraint);

// Insert the result into the cache.
borrow = cache.write();
let mut borrow = cache.write();
borrow.insert::<In>(key, constraint.take(), output.clone());
#[cfg(feature = "last_was_hit")]
Dherse marked this conversation as resolved.
Show resolved Hide resolved
LAST_WAS_HIT.with(|cell| cell.set(false));
Expand Down Expand Up @@ -116,22 117,22 @@ impl<C, Out: 'static> Cache<C, Out> {
/// Evict all entries whose age is larger than or equal to `max_age`.
pub fn evict(&mut self, max_age: usize) {
self.entries.retain(|_, entries| {
entries.retain_mut(|entry| {
entry.age = 1;
entry.age <= max_age
entries.retain(|entry| {
let age = entry.age.fetch_add(1, Ordering::Acquire);
Dherse marked this conversation as resolved.
Show resolved Hide resolved
(age 1) <= max_age
Dherse marked this conversation as resolved.
Show resolved Hide resolved
});
!entries.is_empty()
});
}

/// Look for a matching entry in the cache.
fn lookup<In>(&mut self, key: u128, input: &In) -> Option<(&In::Constraint, &Out)>
fn lookup<In>(&self, key: u128, input: &In) -> Option<(&In::Constraint, &Out)>
where
In: Input<Constraint = C>,
{
self.entries
.get_mut(&key)?
.iter_mut()
.get(&key)?
.iter()
.rev()
.find_map(|entry| entry.lookup::<In>(input))
}
Expand All @@ -155,7 156,7 @@ struct CacheEntry<C, Out> {
/// The memoized function's output.
output: Out,
/// How many evictions have passed since the entry has been last used.
age: usize,
age: AtomicUsize,
}

impl<C, Out: 'static> CacheEntry<C, Out> {
Expand All @@ -164,16 165,16 @@ impl<C, Out: 'static> CacheEntry<C, Out> {
where
In: Input<Constraint = C>,
{
Self { constraint, output, age: 0 }
Self { constraint, output, age: AtomicUsize::new(0) }
}

/// Return the entry's output if it is valid for the given input.
fn lookup<In>(&mut self, input: &In) -> Option<(&In::Constraint, &Out)>
fn lookup<In>(&self, input: &In) -> Option<(&In::Constraint, &Out)>
where
In: Input<Constraint = C>,
{
input.validate(&self.constraint).then(|| {
self.age = 0;
self.age.store(0, Ordering::Release);
(&self.constraint, &self.output)
})
}
Expand Down