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

Implement search_for! using functions #491

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
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
57 changes: 36 additions & 21 deletions src/function/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,6 @@ pub(crate) struct Table {
pub(crate) vals: Vec<(Input, TupleOutput)>,
}

/// Used for the HashTable probe sequence.
macro_rules! search_for {
($slf:expr, $hash:expr, $inp:expr) => {
|to| {
// Test that hashes match.
if to.hash != $hash {
return false;
}
// If the hash matches, the value should not be stale, and the data
// should match.
let inp = &$slf.vals[to.off as usize].0;
inp.live() && inp.data() == $inp
}
};
}

impl Debug for Table {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Table")
Expand Down Expand Up @@ -119,14 +103,14 @@ impl Table {
/// table.
pub(crate) fn get(&self, inputs: &[Value]) -> Option<&TupleOutput> {
let hash = hash_values(inputs);
let &TableOffset { off, .. } = self.table.find(hash, search_for!(self, hash, inputs))?;
let &TableOffset { off, .. } = self.table.find(hash, self.search_for(hash, inputs))?;
debug_assert!(self.vals[off].0.live());
Some(&self.vals[off].1)
}

pub(crate) fn get_mut(&mut self, inputs: &[Value]) -> Option<&mut TupleOutput> {
let hash: u64 = hash_values(inputs);
let &TableOffset { off, .. } = self.table.find(hash, search_for!(self, hash, inputs))?;
let &TableOffset { off, .. } = self.table.find(hash, self.search_for(hash, inputs))?;
debug_assert!(self.vals[off].0.live());
Some(&mut self.vals[off].1)
}
Expand Down Expand Up @@ -159,8 +143,9 @@ impl Table {
assert!(ts >= self.max_ts);
self.max_ts = ts;
let hash = hash_values(inputs);
if let Some(TableOffset { off, .. }) =
self.table.find_mut(hash, search_for!(self, hash, inputs))
if let Some(TableOffset { off, .. }) = self
.table
.find_mut(hash, search_for(&self.vals, hash, inputs))
{
let (inp, prev) = &mut self.vals[*off];
let prev_subsumed = prev.subsumed;
Expand Down Expand Up @@ -235,7 +220,10 @@ impl Table {
/// removed.
pub(crate) fn remove(&mut self, inp: &[Value], ts: u32) -> bool {
let hash = hash_values(inp);
let Ok(entry) = self.table.find_entry(hash, search_for!(self, hash, inp)) else {
let Ok(entry) = self
.table
.find_entry(hash, search_for(&self.vals, hash, inp))
Copy link
Collaborator

Choose a reason for hiding this comment

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

this can just use self.search_for? (similarly above)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would cause a double borrow error

else {
return false;
};
let (TableOffset { off, .. }, _) = entry.remove();
Expand Down Expand Up @@ -324,6 +312,33 @@ impl Table {
0..0
}
}

/// Used for the HashTable probe sequence.
fn search_for<'a>(
&'a self,
hash: u64,
input: &'a [Value],
) -> impl Fn(&TableOffset) -> bool + 'a {
search_for(&self.vals, hash, input)
}
}

/// Used for the HashTable probe sequence.
fn search_for<'a>(
vals: &'a [(Input, TupleOutput)],
hash: u64,
input: &'a [Value],
) -> impl Fn(&TableOffset) -> bool + 'a {
move |to| {
// Test that hashes match.
if to.hash != hash {
return false;
}
// If the hash matches, the value should not be stale, and the data
// should match.
let (inp, _) = &vals[to.off];
inp.live() && inp.data() == input
}
}

/// Returns whether the given value is live and not subsume (if the include_subsumed flag is false).
Expand Down
Loading