use std::hash::{BuildHasher, Hash, Hasher};
use crate::canon::{FixedCanon, VarCanon};
use crate::containers::DenseId;
use crate::containers::IndexLike; use crate::containers::Tagged;
use crate::containers::{ShrinkPolicy, VecI, VecToken};
use crate::node_types::{FixedArityNode, LitNode, VariableArityNode};
#[derive(Default, Clone)]
struct PassthroughHasher(u64);
impl Hasher for PassthroughHasher {
fn write(&mut self, _bytes: &[u8]) {}
fn write_u64(&mut self, i: u64) {
self.0 = i;
}
fn finish(&self) -> u64 {
self.0
}
}
#[derive(Default, Clone)]
struct PassthroughBuildHasher;
impl BuildHasher for PassthroughBuildHasher {
type Hasher = PassthroughHasher;
fn build_hasher(&self) -> PassthroughHasher {
PassthroughHasher(0)
}
}
type Fingerprint = u32;
#[inline]
fn fold32(h: u64) -> Fingerprint {
(h ^ (h >> 32)) as Fingerprint
}
#[inline]
fn spread(fp: Fingerprint) -> u64 {
(fp as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15)
}
#[derive(Clone, Copy, Debug)]
struct StoredKey<L> {
fp: Fingerprint,
local_id: L,
}
impl<L> Hash for StoredKey<L> {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u64(spread(self.fp));
}
}
impl<L: PartialEq> PartialEq for StoredKey<L> {
fn eq(&self, other: &Self) -> bool {
self.local_id == other.local_id
}
}
impl<L: Eq> Eq for StoredKey<L> {}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum InsertResult<G, L> {
Hit { global_id: G },
Inserted { local_id: L },
}
#[derive(Clone, Copy, Debug)]
struct CacheFrame {
saved_len: usize,
dirty_start: usize,
dirty_overflow: bool,
}
pub(crate) const REBUILD_RATIO: usize = 4;
#[inline]
fn dirty_budget(saved_len: usize) -> usize {
saved_len / REBUILD_RATIO
}
#[inline]
pub(crate) fn restore_incrementally(suffix_len: usize, dirty_len: usize, saved_len: usize) -> bool {
REBUILD_RATIO * (suffix_len + dirty_len) <= saved_len
}
#[derive(Clone, Copy, Debug)]
pub struct CacheToken {
nodes: VecToken,
history: Option<VecToken>,
frame_index: usize,
}
#[derive(Clone, Copy, Debug)]
pub struct PoolCacheToken {
nodes: VecToken,
children: VecToken,
history_nodes: Option<VecToken>,
history_children: Option<VecToken>,
frame_index: usize,
}
pub struct FixedArityCache<
G: DenseId,
O: DenseId,
L: DenseId,
const K: usize,
const TRACK: bool = true,
const PROOFS: bool = false,
> {
nodes: VecI<FixedArityNode<G, O, K>, L, TRACK>,
index: hashbrown::HashMap<StoredKey<L>, (), PassthroughBuildHasher>,
history: Option<VecI<FixedArityNode<G, O, K>, usize, TRACK>>,
dirty: Vec<L>,
frames: Vec<CacheFrame>,
}
impl<
G: DenseId + Hash,
O: DenseId + Hash,
L: DenseId,
const K: usize,
const TRACK: bool,
const PROOFS: bool,
> Default for FixedArityCache<G, O, L, K, TRACK, PROOFS>
{
fn default() -> Self {
Self::new()
}
}
impl<
G: DenseId + Hash,
O: DenseId + Hash,
L: DenseId,
const K: usize,
const TRACK: bool,
const PROOFS: bool,
> FixedArityCache<G, O, L, K, TRACK, PROOFS>
{
pub fn new() -> Self {
Self {
nodes: VecI::new(),
index: hashbrown::HashMap::with_hasher(PassthroughBuildHasher),
history: if PROOFS { Some(VecI::new()) } else { None },
dirty: Vec::new(),
frames: Vec::new(),
}
}
pub fn len(&self) -> L {
self.nodes.len()
}
pub fn is_empty(&self) -> bool {
self.nodes.len() == <L as IndexLike>::min()
}
pub fn get(&self, id: L) -> FixedArityNode<G, O, K> {
self.nodes.get(id)
}
pub fn set(&mut self, id: L, node: FixedArityNode<G, O, K>) {
self.nodes.set(id, node);
}
pub fn probe(&self, op: &O, children: &[G; K]) -> Option<G> {
let fp = self.fingerprint(op, children);
self.index
.raw_entry()
.from_hash(spread(fp), |sk| {
sk.fp == fp && {
let n = self.nodes.get(sk.local_id);
n.op() == *op && n.children == *children
}
})
.map(|(sk, _)| self.nodes.get(sk.local_id).global_id())
}
pub fn insert(&mut self, global_id: G, op: O, children: [G; K]) -> L {
let node = FixedArityNode::new(global_id, op, children);
let fp = self.fingerprint(&op, &children);
let lid = self.nodes.len();
self.nodes.try_push(node).expect("push: within index word");
self.index.insert(StoredKey { fp, local_id: lid }, ());
lid
}
pub fn probe_or_insert(&mut self, global_id: G, op: O, children: [G; K]) -> InsertResult<G, L> {
if let Some(gid) = self.probe(&op, &children) {
return InsertResult::Hit { global_id: gid };
}
let lid = self.insert(global_id, op, children);
InsertResult::Inserted { local_id: lid }
}
pub fn node_get(&self, id: L) -> FixedArityNode<G, O, K> {
self.nodes.get(id)
}
pub fn node_set(&mut self, id: L, node: FixedArityNode<G, O, K>) {
self.nodes.set(id, node);
}
pub fn recanonize_node<F: FixedCanon<G, K>>(
&mut self,
local_id: L,
find: impl Fn(G) -> G,
collisions: &mut Vec<(G, G)>,
touched: &mut Vec<G>,
) {
let mut node = self.nodes.get(local_id);
let old_fp = self.fingerprint(&node.op(), &node.children);
F::canonize(&mut node.children, &find);
let new_fp = self.fingerprint(&node.op(), &node.children);
if new_fp == old_fp {
let old = self.nodes.get(local_id);
if old.children == node.children {
return;
}
}
touched.push(node.global_id());
self.note_dirty(local_id);
if let Some(hist) = &mut self.history
&& !node.has_history()
{
hist.try_push(self.nodes.get(local_id))
.expect("push: within index word");
}
self.index.remove(&StoredKey {
fp: old_fp,
local_id,
});
let gid = node.global_id();
let mut new_node = FixedArityNode::new(gid, node.op(), node.children);
if PROOFS {
new_node.set_history();
}
self.nodes.set(local_id, new_node);
if let Some(existing_gid) = self.probe(&node.op(), &node.children) {
collisions.push((gid, existing_gid));
}
self.index.insert(
StoredKey {
fp: new_fp,
local_id,
},
(),
);
}
pub fn original_children(&self, global_id: G) -> Option<[G; K]> {
let hist = self.history.as_ref()?;
let len = hist.len();
for i in 0..len {
let node = hist.get(i);
if node.global_id() == global_id {
return Some(node.children);
}
}
None
}
#[inline]
fn note_dirty(&mut self, local_id: L) {
if !TRACK {
return;
}
let Some(frame) = self.frames.last_mut() else {
return;
};
if local_id.as_usize() >= frame.saved_len {
return;
}
if self.dirty.len() - frame.dirty_start > dirty_budget(frame.saved_len) {
frame.dirty_overflow = true;
return;
}
self.dirty.push(local_id);
}
pub fn mark(&mut self, shrink: ShrinkPolicy) -> CacheToken {
let token = CacheToken {
nodes: self
.nodes
.try_mark(shrink)
.expect("mark: frame depth is bounded by the saturation driver"),
history: self.history.as_mut().map(|h| {
h.try_mark(shrink)
.expect("mark: frame depth is bounded by the saturation driver")
}),
frame_index: self.frames.len(),
};
self.frames.push(CacheFrame {
saved_len: self.nodes.len().as_usize(),
dirty_start: self.dirty.len(),
dirty_overflow: false,
});
token
}
pub fn restore(&mut self, token: CacheToken) {
let frame = *self
.frames
.get(token.frame_index)
.expect("restore: token minted by this cache's own mark, and not already spent");
assert!(
self.nodes.is_valid_token(&token.nodes),
"restore: node-arena token is not restorable"
);
if let (Some(h), Some(tok)) = (&self.history, token.history.as_ref()) {
assert!(
h.is_valid_token(tok),
"restore: history token is not restorable"
);
}
let live_len = self.nodes.len().as_usize();
let incremental = !frame.dirty_overflow
&& restore_incrementally(
live_len - frame.saved_len,
self.dirty.len() - frame.dirty_start,
frame.saved_len,
);
if incremental {
for i in frame.saved_len..live_len {
self.remove_entry(L::from_usize(i));
}
for k in frame.dirty_start..self.dirty.len() {
self.remove_entry(self.dirty[k]);
}
}
self.nodes
.try_restore(token.nodes)
.expect("restore: token minted by this container's own mark");
if let (Some(h), Some(tok)) = (&mut self.history, token.history) {
h.try_restore(tok)
.expect("restore: token minted by this container's own mark");
}
if incremental {
for k in frame.dirty_start..self.dirty.len() {
self.insert_entry(self.dirty[k]);
}
} else {
self.rebuild_index();
}
self.dirty.truncate(frame.dirty_start);
self.frames.truncate(token.frame_index);
#[cfg(debug_assertions)]
debug_assert!(
self.index_matches_rebuild(),
"restore left the hashcons index out of step with the node arena"
);
}
#[inline]
fn remove_entry(&mut self, local_id: L) {
let fp = fold32(self.nodes.get(local_id).content_hash());
self.index.remove(&StoredKey { fp, local_id });
}
#[inline]
fn insert_entry(&mut self, local_id: L) {
let fp = fold32(self.nodes.get(local_id).content_hash());
self.index.insert(StoredKey { fp, local_id }, ());
}
#[cfg(debug_assertions)]
fn index_matches_rebuild(&self) -> bool {
let count = self.nodes.len().as_usize();
if self.index.len() != count {
return false;
}
let mut seen = vec![false; count];
for (sk, ()) in self.index.iter() {
let i = sk.local_id.as_usize();
if i >= count || seen[i] || sk.fp != fold32(self.nodes.get(sk.local_id).content_hash())
{
return false;
}
seen[i] = true;
}
true
}
fn rebuild_index(&mut self) {
self.index.clear();
let count = self.nodes.len().as_usize();
for i in 0..count {
let lid = L::from_usize(i);
let n = self.nodes.get(lid);
self.index.insert(
StoredKey {
fp: fold32(n.content_hash()),
local_id: lid,
},
(),
);
}
}
fn fingerprint(&self, op: &O, children: &[G; K]) -> Fingerprint {
let mut h = rapidhash::fast::RapidHasher::default();
op.hash(&mut h);
children.hash(&mut h);
fold32(h.finish())
}
}
pub struct VariableArityCache<
G: DenseId,
O: DenseId,
C: Tagged + Clone + Copy + Hash + Eq,
L: DenseId,
const TRACK: bool = true,
const PROOFS: bool = false,
> {
nodes: VecI<VariableArityNode<G, O>, L, TRACK>,
children: VecI<C, usize, TRACK>,
index: hashbrown::HashMap<StoredKey<L>, (), PassthroughBuildHasher>,
history_nodes: Option<VecI<VariableArityNode<G, O>, usize, TRACK>>,
history_children: Option<VecI<C, usize, TRACK>>,
dirty: Vec<L>,
frames: Vec<CacheFrame>,
}
impl<
G: DenseId + Hash,
O: DenseId + Hash,
C: Tagged + Clone + Copy + Hash + Eq + core::fmt::Debug,
L: DenseId,
const TRACK: bool,
const PROOFS: bool,
> Default for VariableArityCache<G, O, C, L, TRACK, PROOFS>
{
fn default() -> Self {
Self::new()
}
}
impl<
G: DenseId + Hash,
O: DenseId + Hash,
C: Tagged + Clone + Copy + Hash + Eq + core::fmt::Debug,
L: DenseId,
const TRACK: bool,
const PROOFS: bool,
> VariableArityCache<G, O, C, L, TRACK, PROOFS>
{
pub fn new() -> Self {
Self {
nodes: VecI::new(),
children: VecI::new(),
index: hashbrown::HashMap::with_hasher(PassthroughBuildHasher),
history_nodes: if PROOFS { Some(VecI::new()) } else { None },
history_children: if PROOFS { Some(VecI::new()) } else { None },
dirty: Vec::new(),
frames: Vec::new(),
}
}
pub fn len(&self) -> L {
self.nodes.len()
}
pub fn is_empty(&self) -> bool {
self.nodes.len() == <L as IndexLike>::min()
}
pub fn get(&self, id: L) -> VariableArityNode<G, O> {
self.nodes.get(id)
}
pub fn set(&mut self, id: L, node: VariableArityNode<G, O>) {
self.nodes.set(id, node);
}
pub fn children_vec(&self, node: &VariableArityNode<G, O>) -> Vec<C> {
let (start, end) = node.span();
(start..end).map(|i| self.children.get(i)).collect()
}
pub fn pool_get(&self, i: usize) -> C {
self.children.get(i)
}
pub fn pool_set(&mut self, i: usize, value: C) {
self.children.set(i, value);
}
pub fn probe(&self, op: O, elems: &[C]) -> Option<G> {
let fp = self.fingerprint(&op, elems);
self.index
.raw_entry()
.from_hash(spread(fp), |sk| {
sk.fp == fp && {
let n = self.nodes.get(sk.local_id);
n.op() == op && self.children_eq(&n, elems)
}
})
.map(|(sk, _)| self.nodes.get(sk.local_id).global_id())
}
pub fn insert(&mut self, global_id: G, op: O, elems: &[C]) -> L {
let start = self.children.len();
for &e in elems {
self.children.try_push(e).expect("push: within index word");
}
let end = self.children.len();
let node = VariableArityNode::make(global_id, op, start, end);
let fp = self.fingerprint(&op, elems);
let lid = self.nodes.len();
self.nodes.try_push(node).expect("push: within index word");
self.index.insert(StoredKey { fp, local_id: lid }, ());
lid
}
pub fn probe_or_insert(&mut self, global_id: G, op: O, elems: &[C]) -> InsertResult<G, L> {
if let Some(gid) = self.probe(op, elems) {
return InsertResult::Hit { global_id: gid };
}
let lid = self.insert(global_id, op, elems);
InsertResult::Inserted { local_id: lid }
}
pub fn node_get(&self, id: L) -> VariableArityNode<G, O> {
self.nodes.get(id)
}
pub fn node_set(&mut self, id: L, node: VariableArityNode<G, O>) {
self.nodes.set(id, node);
}
pub fn recanonize_node<V: VarCanon<G, C>>(
&mut self,
local_id: L,
find: impl Fn(G) -> G,
buf: &mut Vec<C>,
collisions: &mut Vec<(G, G)>,
touched: &mut Vec<G>,
mode: crate::canon::CanonMode<G>,
) {
let node = self.nodes.get(local_id);
let (start, end) = node.span();
let old_fp = self.children_fingerprint(&node);
buf.clear();
V::canonize(buf, start, end, |i| self.children.get(i), &find, mode);
let new_len = buf.len();
if new_len == end - start {
let mut same = true;
for i in 0..new_len {
if buf[i] != self.children.get(start + i) {
same = false;
break;
}
}
if same {
return;
}
}
touched.push(node.global_id());
self.note_dirty(local_id);
if let (Some(hn), Some(hc)) = (&mut self.history_nodes, &mut self.history_children)
&& !node.has_history()
{
let hist_start = hc.len();
for i in start..end {
hc.try_push(self.children.get(i))
.expect("push: within index word");
}
let hist_end = hc.len();
hn.try_push(VariableArityNode::make(
node.global_id(),
node.op(),
hist_start,
hist_end,
))
.expect("push: within index word");
}
self.index.remove(&StoredKey {
fp: old_fp,
local_id,
});
for i in 0..new_len {
self.children.set(start + i, buf[i]);
}
let new_end = start + new_len;
let gid = node.global_id();
let mut updated = VariableArityNode::make(gid, node.op(), start, new_end);
if PROOFS {
updated.set_history();
}
if new_end != end || PROOFS {
self.nodes.set(local_id, updated);
}
let new_fp = self.fingerprint(&node.op(), &buf[..new_len]);
if let Some(existing_gid) = self.probe(node.op(), &buf[..new_len]) {
collisions.push((gid, existing_gid));
}
self.index.insert(
StoredKey {
fp: new_fp,
local_id,
},
(),
);
}
pub fn original_children(&self, global_id: G, out: &mut Vec<C>) -> bool {
let (hn, hc) = match (&self.history_nodes, &self.history_children) {
(Some(hn), Some(hc)) => (hn, hc),
_ => return false,
};
let len = hn.len();
for i in 0..len {
let node = hn.get(i);
if node.global_id() == global_id {
let (s, e) = node.span();
for j in s..e {
out.push(hc.get(j));
}
return true;
}
}
false
}
#[inline]
fn note_dirty(&mut self, local_id: L) {
if !TRACK {
return;
}
let Some(frame) = self.frames.last_mut() else {
return;
};
if local_id.as_usize() >= frame.saved_len {
return;
}
if self.dirty.len() - frame.dirty_start > dirty_budget(frame.saved_len) {
frame.dirty_overflow = true;
return;
}
self.dirty.push(local_id);
}
pub fn mark(&mut self, shrink: ShrinkPolicy) -> PoolCacheToken {
let token = PoolCacheToken {
nodes: self
.nodes
.try_mark(shrink)
.expect("mark: frame depth is bounded by the saturation driver"),
children: self
.children
.try_mark(shrink)
.expect("mark: frame depth is bounded by the saturation driver"),
history_nodes: self.history_nodes.as_mut().map(|h| {
h.try_mark(shrink)
.expect("mark: frame depth is bounded by the saturation driver")
}),
history_children: self.history_children.as_mut().map(|h| {
h.try_mark(shrink)
.expect("mark: frame depth is bounded by the saturation driver")
}),
frame_index: self.frames.len(),
};
self.frames.push(CacheFrame {
saved_len: self.nodes.len().as_usize(),
dirty_start: self.dirty.len(),
dirty_overflow: false,
});
token
}
pub fn restore(&mut self, token: PoolCacheToken) {
let frame = *self
.frames
.get(token.frame_index)
.expect("restore: token minted by this cache's own mark, and not already spent");
assert!(
self.nodes.is_valid_token(&token.nodes),
"restore: node-arena token is not restorable"
);
assert!(
self.children.is_valid_token(&token.children),
"restore: child-pool token is not restorable"
);
if let (Some(h), Some(tok)) = (&self.history_nodes, token.history_nodes.as_ref()) {
assert!(
h.is_valid_token(tok),
"restore: history token is not restorable"
);
}
if let (Some(h), Some(tok)) = (&self.history_children, token.history_children.as_ref()) {
assert!(
h.is_valid_token(tok),
"restore: history token is not restorable"
);
}
let live_len = self.nodes.len().as_usize();
let incremental = !frame.dirty_overflow
&& restore_incrementally(
live_len - frame.saved_len,
self.dirty.len() - frame.dirty_start,
frame.saved_len,
);
if incremental {
for i in frame.saved_len..live_len {
self.remove_entry(L::from_usize(i));
}
for k in frame.dirty_start..self.dirty.len() {
self.remove_entry(self.dirty[k]);
}
}
self.nodes
.try_restore(token.nodes)
.expect("restore: token minted by this container's own mark");
self.children
.try_restore(token.children)
.expect("restore: token minted by this container's own mark");
if let (Some(h), Some(tok)) = (&mut self.history_nodes, token.history_nodes) {
h.try_restore(tok)
.expect("restore: token minted by this container's own mark");
}
if let (Some(h), Some(tok)) = (&mut self.history_children, token.history_children) {
h.try_restore(tok)
.expect("restore: token minted by this container's own mark");
}
if incremental {
for k in frame.dirty_start..self.dirty.len() {
self.insert_entry(self.dirty[k]);
}
} else {
self.rebuild_index();
}
self.dirty.truncate(frame.dirty_start);
self.frames.truncate(token.frame_index);
#[cfg(debug_assertions)]
debug_assert!(
self.index_matches_rebuild(),
"restore left the hashcons index out of step with the node arena"
);
}
#[inline]
fn remove_entry(&mut self, local_id: L) {
let fp = self.children_fingerprint(&self.nodes.get(local_id));
self.index.remove(&StoredKey { fp, local_id });
}
#[inline]
fn insert_entry(&mut self, local_id: L) {
let fp = self.children_fingerprint(&self.nodes.get(local_id));
self.index.insert(StoredKey { fp, local_id }, ());
}
#[cfg(debug_assertions)]
fn index_matches_rebuild(&self) -> bool {
let count = self.nodes.len().as_usize();
if self.index.len() != count {
return false;
}
let mut seen = vec![false; count];
for (sk, ()) in self.index.iter() {
let i = sk.local_id.as_usize();
if i >= count
|| seen[i]
|| sk.fp != self.children_fingerprint(&self.nodes.get(sk.local_id))
{
return false;
}
seen[i] = true;
}
true
}
fn children_eq(&self, node: &VariableArityNode<G, O>, elems: &[C]) -> bool {
let (start, end) = node.span();
if end - start != elems.len() {
return false;
}
(0..elems.len()).all(|i| self.children.get(start + i) == elems[i])
}
fn rebuild_index(&mut self) {
self.index.clear();
let count = self.nodes.len().as_usize();
for i in 0..count {
let lid = L::from_usize(i);
let n = self.nodes.get(lid);
let fp = self.children_fingerprint(&n);
self.index.insert(StoredKey { fp, local_id: lid }, ());
}
}
fn fingerprint(&self, op: &O, elems: &[C]) -> Fingerprint {
let mut h = rapidhash::fast::RapidHasher::default();
op.hash(&mut h);
elems.hash(&mut h);
fold32(h.finish())
}
fn children_fingerprint(&self, node: &VariableArityNode<G, O>) -> Fingerprint {
let mut h = rapidhash::fast::RapidHasher::default();
node.op().hash(&mut h);
let (start, end) = node.span();
(end - start).hash(&mut h);
for i in start..end {
self.children.get(i).hash(&mut h);
}
fold32(h.finish())
}
}
pub struct LitCache<G: DenseId, O: DenseId, V: DenseId, L: DenseId, const TRACK: bool = true> {
nodes: VecI<LitNode<G, O, V>, L, TRACK>,
index: hashbrown::HashMap<StoredKey<L>, (), PassthroughBuildHasher>,
frames: Vec<CacheFrame>,
}
impl<G: DenseId + Hash, O: DenseId + Hash, V: DenseId + Hash, L: DenseId, const TRACK: bool> Default
for LitCache<G, O, V, L, TRACK>
{
fn default() -> Self {
Self::new()
}
}
impl<G: DenseId + Hash, O: DenseId + Hash, V: DenseId + Hash, L: DenseId, const TRACK: bool>
LitCache<G, O, V, L, TRACK>
{
pub fn new() -> Self {
Self {
nodes: VecI::new(),
index: hashbrown::HashMap::with_hasher(PassthroughBuildHasher),
frames: Vec::new(),
}
}
pub fn len(&self) -> L {
self.nodes.len()
}
pub fn is_empty(&self) -> bool {
self.nodes.len() == <L as IndexLike>::min()
}
pub fn get(&self, id: L) -> LitNode<G, O, V> {
self.nodes.get(id)
}
pub fn set(&mut self, id: L, node: LitNode<G, O, V>) {
self.nodes.set(id, node);
}
pub fn probe(&self, op: O, lit: V) -> Option<G> {
let fp = self.fingerprint(&op, &lit);
self.index
.raw_entry()
.from_hash(spread(fp), |sk| {
sk.fp == fp && {
let n = self.nodes.get(sk.local_id);
n.op() == op && n.lit == lit
}
})
.map(|(sk, _)| self.nodes.get(sk.local_id).global_id())
}
pub fn insert(&mut self, global_id: G, op: O, lit: V) -> L {
let node = LitNode::new(global_id, op, lit);
let fp = self.fingerprint(&op, &lit);
let lid = self.nodes.len();
self.nodes.try_push(node).expect("push: within index word");
self.index.insert(StoredKey { fp, local_id: lid }, ());
lid
}
pub fn probe_or_insert(&mut self, global_id: G, op: O, lit: V) -> InsertResult<G, L> {
if let Some(gid) = self.probe(op, lit) {
return InsertResult::Hit { global_id: gid };
}
let lid = self.insert(global_id, op, lit);
InsertResult::Inserted { local_id: lid }
}
pub fn mark(&mut self, shrink: ShrinkPolicy) -> CacheToken {
let token = CacheToken {
nodes: self
.nodes
.try_mark(shrink)
.expect("mark: frame depth is bounded by the saturation driver"),
history: None,
frame_index: self.frames.len(),
};
self.frames.push(CacheFrame {
saved_len: self.nodes.len().as_usize(),
dirty_start: 0,
dirty_overflow: false,
});
token
}
pub fn restore(&mut self, token: CacheToken) {
let frame = *self
.frames
.get(token.frame_index)
.expect("restore: token minted by this cache's own mark, and not already spent");
let live_len = self.nodes.len().as_usize();
let incremental = restore_incrementally(live_len - frame.saved_len, 0, frame.saved_len);
if incremental {
for i in frame.saved_len..live_len {
let local_id = L::from_usize(i);
let fp = fold32(self.nodes.get(local_id).content_hash());
self.index.remove(&StoredKey { fp, local_id });
}
}
self.nodes
.try_restore(token.nodes)
.expect("restore: token minted by this container's own mark");
if !incremental {
self.rebuild_index();
}
self.frames.truncate(token.frame_index);
#[cfg(debug_assertions)]
debug_assert!(
self.index_matches_rebuild(),
"restore left the hashcons index out of step with the node arena"
);
}
#[cfg(debug_assertions)]
fn index_matches_rebuild(&self) -> bool {
let count = self.nodes.len().as_usize();
if self.index.len() != count {
return false;
}
let mut seen = vec![false; count];
for (sk, ()) in self.index.iter() {
let i = sk.local_id.as_usize();
if i >= count || seen[i] || sk.fp != fold32(self.nodes.get(sk.local_id).content_hash())
{
return false;
}
seen[i] = true;
}
true
}
fn rebuild_index(&mut self) {
self.index.clear();
let count = self.nodes.len().as_usize();
for i in 0..count {
let lid = L::from_usize(i);
let n = self.nodes.get(lid);
self.index.insert(
StoredKey {
fp: fold32(n.content_hash()),
local_id: lid,
},
(),
);
}
}
fn fingerprint(&self, op: &O, lit: &V) -> Fingerprint {
let mut h = rapidhash::fast::RapidHasher::default();
op.hash(&mut h);
lit.hash(&mut h);
fold32(h.finish())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::canon::{CCanon, MSetCanon, OrderedCanon, PlainCanon, SetCanon};
use crate::id::{ENodeId, OpId};
use crate::multiplicity::Multiplicity;
use crate::nodes::{
LitNodeId, LitValId, MSetNodeId, Plain0Id, Plain2Id, PlainNId, SPairNodeId, SetNodeId,
};
#[test]
fn fixed_arity_probe_insert() {
let mut c = FixedArityCache::<ENodeId, OpId, Plain0Id, 0, false>::new();
let op = OpId::new(0);
let r = c.probe_or_insert(ENodeId::new(0), op, []);
assert!(matches!(r, InsertResult::Inserted { .. }));
let r2 = c.probe_or_insert(ENodeId::new(99), op, []);
assert!(matches!(r2, InsertResult::Hit { .. }));
}
#[test]
fn fixed_arity_binary() {
let mut c = FixedArityCache::<ENodeId, OpId, Plain2Id, 2, false>::new();
let op = OpId::new(0);
let ch = [ENodeId::new(1), ENodeId::new(2)];
let r = c.probe_or_insert(ENodeId::new(0), op, ch);
assert!(matches!(r, InsertResult::Inserted { .. }));
assert!(c.probe(&op, &ch).is_some());
assert!(c.probe(&op, &[ENodeId::new(2), ENodeId::new(1)]).is_none());
}
#[test]
fn fingerprint_collision_is_disambiguated_by_content() {
const LIMIT: u32 = 4_000_000;
let mut fixed = FixedArityCache::<ENodeId, OpId, Plain2Id, 2, false>::new();
let op = OpId::new(0);
let mut seen: std::collections::HashMap<Fingerprint, u32> =
std::collections::HashMap::new();
let (a, b) = (0..LIMIT)
.find_map(|n| {
let fp = fixed.fingerprint(&op, &[ENodeId::new(n), ENodeId::new(0)]);
seen.insert(fp, n).map(|prev| (prev, n))
})
.expect("no 32-bit fingerprint collision within the search bound");
assert_ne!(a, b);
let (ca, cb) = (
[ENodeId::new(a), ENodeId::new(0)],
[ENodeId::new(b), ENodeId::new(0)],
);
assert_eq!(fixed.fingerprint(&op, &ca), fixed.fingerprint(&op, &cb));
assert_ne!(ca, cb);
let ga = ENodeId::new(LIMIT + 1);
let gb = ENodeId::new(LIMIT + 2);
assert!(matches!(
fixed.probe_or_insert(ga, op, ca),
InsertResult::Inserted { .. }
));
assert!(matches!(
fixed.probe_or_insert(gb, op, cb),
InsertResult::Inserted { .. }
));
assert_eq!(fixed.probe(&op, &ca), Some(ga));
assert_eq!(fixed.probe(&op, &cb), Some(gb));
let mut var = VariableArityCache::<ENodeId, OpId, ENodeId, PlainNId, false>::new();
let mut seen: std::collections::HashMap<Fingerprint, u32> =
std::collections::HashMap::new();
let (a, b) = (0..LIMIT)
.find_map(|n| {
let fp = var.fingerprint(&op, &[ENodeId::new(n), ENodeId::new(0)]);
seen.insert(fp, n).map(|prev| (prev, n))
})
.expect("no 32-bit fingerprint collision within the search bound");
let (va, vb) = (
[ENodeId::new(a), ENodeId::new(0)],
[ENodeId::new(b), ENodeId::new(0)],
);
assert_eq!(var.fingerprint(&op, &va), var.fingerprint(&op, &vb));
assert_ne!(va, vb);
assert!(matches!(
var.probe_or_insert(ga, op, &va),
InsertResult::Inserted { .. }
));
assert!(matches!(
var.probe_or_insert(gb, op, &vb),
InsertResult::Inserted { .. }
));
assert_eq!(var.probe(op, &va), Some(ga));
assert_eq!(var.probe(op, &vb), Some(gb));
}
#[test]
fn variable_arity_probe_insert() {
let mut c = VariableArityCache::<ENodeId, OpId, ENodeId, PlainNId, false>::new();
let op = OpId::new(0);
let ch = &[ENodeId::new(1), ENodeId::new(2), ENodeId::new(3)];
let r = c.probe_or_insert(ENodeId::new(0), op, ch);
assert!(matches!(r, InsertResult::Inserted { .. }));
assert!(c.probe(op, ch).is_some());
}
#[test]
fn lit_cache_probe_insert() {
let mut c = LitCache::<ENodeId, OpId, LitValId, LitNodeId, false>::new();
let op = OpId::new(0);
let lit = LitValId::new(42);
let r = c.probe_or_insert(ENodeId::new(0), op, lit);
assert!(matches!(r, InsertResult::Inserted { .. }));
assert!(c.probe(op, lit).is_some());
assert!(c.probe(op, LitValId::new(99)).is_none());
}
fn id(n: u32) -> ENodeId {
ENodeId::new(n)
}
#[test]
fn recanonize_fixed_no_change() {
let mut c = FixedArityCache::<ENodeId, OpId, Plain2Id, 2, false>::new();
let op = OpId::new(0);
c.probe_or_insert(id(0), op, [id(1), id(2)]);
let mut collisions = Vec::new();
c.recanonize_node::<PlainCanon>(Plain2Id::new(0), |g| g, &mut collisions, &mut Vec::new());
assert!(collisions.is_empty());
assert!(c.probe(&op, &[id(1), id(2)]).is_some());
}
#[test]
fn recanonize_fixed_plain_updates_children() {
let mut c = FixedArityCache::<ENodeId, OpId, Plain2Id, 2, false>::new();
let op = OpId::new(0);
c.probe_or_insert(id(0), op, [id(1), id(2)]);
let mut collisions = Vec::new();
c.recanonize_node::<PlainCanon>(
Plain2Id::new(0),
|g| {
if g == id(2) { id(1) } else { g }
},
&mut collisions,
&mut Vec::new(),
);
assert!(collisions.is_empty());
assert!(c.probe(&op, &[id(1), id(2)]).is_none());
assert!(c.probe(&op, &[id(1), id(1)]).is_some());
}
#[test]
fn recanonize_fixed_collision() {
let mut c = FixedArityCache::<ENodeId, OpId, Plain2Id, 2, false>::new();
let op = OpId::new(0);
c.probe_or_insert(id(10), op, [id(1), id(1)]); c.probe_or_insert(id(20), op, [id(1), id(2)]); let mut collisions = Vec::new();
c.recanonize_node::<PlainCanon>(
Plain2Id::new(1),
|g| {
if g == id(2) { id(1) } else { g }
},
&mut collisions,
&mut Vec::new(),
);
assert_eq!(collisions.len(), 1);
assert_eq!(collisions[0], (id(20), id(10)));
}
#[test]
fn recanonize_c_sorts_pair() {
let mut c = FixedArityCache::<ENodeId, OpId, SPairNodeId, 2, false>::new();
let op = OpId::new(0);
c.probe_or_insert(id(10), op, [id(1), id(5)]); let mut collisions = Vec::new();
c.recanonize_node::<CCanon>(
SPairNodeId::new(0),
|g| {
if g == id(1) { id(9) } else { g }
},
&mut collisions,
&mut Vec::new(),
);
assert!(collisions.is_empty());
assert!(c.probe(&op, &[id(5), id(9)]).is_some());
}
#[test]
fn recanonize_var_ordered_no_change() {
let mut c = VariableArityCache::<ENodeId, OpId, ENodeId, PlainNId, false>::new();
let op = OpId::new(0);
c.probe_or_insert(id(0), op, &[id(1), id(2), id(3)]);
let mut buf = Vec::new();
let mut collisions = Vec::new();
c.recanonize_node::<OrderedCanon>(
PlainNId::new(0),
|g| g,
&mut buf,
&mut collisions,
&mut Vec::new(),
crate::canon::CanonMode::PLAIN,
);
assert!(collisions.is_empty());
}
#[test]
fn recanonize_var_ordered_collision() {
let mut c = VariableArityCache::<ENodeId, OpId, ENodeId, PlainNId, false>::new();
let op = OpId::new(0);
c.probe_or_insert(id(10), op, &[id(1), id(1)]);
c.probe_or_insert(id(20), op, &[id(1), id(2)]);
let mut buf = Vec::new();
let mut collisions = Vec::new();
c.recanonize_node::<OrderedCanon>(
PlainNId::new(1),
|g| {
if g == id(2) { id(1) } else { g }
},
&mut buf,
&mut collisions,
&mut Vec::new(),
crate::canon::CanonMode::PLAIN,
);
assert_eq!(collisions, vec![(id(20), id(10))]);
}
#[test]
fn recanonize_aci_shrinks() {
let mut c = VariableArityCache::<ENodeId, OpId, ENodeId, SetNodeId, false>::new();
let op = OpId::new(0);
c.probe_or_insert(id(10), op, &[id(1), id(2), id(3)]);
let mut buf = Vec::new();
let mut collisions = Vec::new();
c.recanonize_node::<SetCanon>(
SetNodeId::new(0),
|g| {
if g == id(2) { id(1) } else { g }
},
&mut buf,
&mut collisions,
&mut Vec::new(),
crate::canon::CanonMode::PLAIN,
);
assert!(collisions.is_empty());
assert!(c.probe(op, &[id(1), id(3)]).is_some());
assert!(c.probe(op, &[id(1), id(2), id(3)]).is_none());
}
const SHRINK: ShrinkPolicy = ShrinkPolicy::Never;
#[test]
fn restore_drops_the_post_mark_suffix() {
let mut c = FixedArityCache::<ENodeId, OpId, Plain2Id, 2>::new();
let op = OpId::new(0);
c.probe_or_insert(id(10), op, [id(1), id(2)]);
let token = c.mark(SHRINK);
c.probe_or_insert(id(20), op, [id(3), id(4)]);
assert!(c.probe(&op, &[id(3), id(4)]).is_some());
c.restore(token);
assert_eq!(c.len(), Plain2Id::new(1));
assert_eq!(c.probe(&op, &[id(1), id(2)]), Some(id(10)));
assert!(c.probe(&op, &[id(3), id(4)]).is_none());
c.probe_or_insert(id(30), op, [id(5), id(6)]);
assert_eq!(c.probe(&op, &[id(5), id(6)]), Some(id(30)));
}
#[test]
fn restore_rekeys_a_recanonized_pre_mark_node() {
let mut c = FixedArityCache::<ENodeId, OpId, Plain2Id, 2>::new();
let op = OpId::new(0);
c.probe_or_insert(id(10), op, [id(1), id(2)]);
let token = c.mark(SHRINK);
c.recanonize_node::<PlainCanon>(
Plain2Id::new(0),
|g| if g == id(2) { id(1) } else { g },
&mut Vec::new(),
&mut Vec::new(),
);
assert!(c.probe(&op, &[id(1), id(1)]).is_some());
assert!(c.probe(&op, &[id(1), id(2)]).is_none());
c.restore(token);
assert_eq!(c.probe(&op, &[id(1), id(2)]), Some(id(10)));
assert!(c.probe(&op, &[id(1), id(1)]).is_none());
}
#[test]
fn restore_rekeys_a_recanonized_variable_arity_node() {
let mut c = VariableArityCache::<ENodeId, OpId, ENodeId, SetNodeId>::new();
let op = OpId::new(0);
c.probe_or_insert(id(10), op, &[id(1), id(2), id(3)]);
let token = c.mark(SHRINK);
c.recanonize_node::<SetCanon>(
SetNodeId::new(0),
|g| if g == id(2) { id(1) } else { g },
&mut Vec::new(),
&mut Vec::new(),
&mut Vec::new(),
crate::canon::CanonMode::PLAIN,
);
assert!(c.probe(op, &[id(1), id(3)]).is_some());
c.restore(token);
assert_eq!(c.probe(op, &[id(1), id(2), id(3)]), Some(id(10)));
assert!(c.probe(op, &[id(1), id(3)]).is_none());
}
#[test]
fn restore_past_an_open_inner_mark() {
let mut c = FixedArityCache::<ENodeId, OpId, Plain2Id, 2>::new();
let op = OpId::new(0);
c.probe_or_insert(id(10), op, [id(1), id(2)]);
let outer = c.mark(SHRINK);
c.probe_or_insert(id(20), op, [id(3), id(4)]);
let _inner = c.mark(SHRINK);
c.probe_or_insert(id(30), op, [id(5), id(6)]);
c.recanonize_node::<PlainCanon>(
Plain2Id::new(0),
|g| if g == id(2) { id(7) } else { g },
&mut Vec::new(),
&mut Vec::new(),
);
c.restore(outer);
assert_eq!(c.len(), Plain2Id::new(1));
assert_eq!(c.probe(&op, &[id(1), id(2)]), Some(id(10)));
assert!(c.probe(&op, &[id(1), id(7)]).is_none());
assert!(c.probe(&op, &[id(3), id(4)]).is_none());
assert!(c.probe(&op, &[id(5), id(6)]).is_none());
}
#[test]
fn restore_falls_back_to_rebuild_on_a_large_suffix() {
let mut c = FixedArityCache::<ENodeId, OpId, Plain2Id, 2>::new();
let op = OpId::new(0);
c.probe_or_insert(id(10), op, [id(1), id(2)]);
let token = c.mark(SHRINK);
for k in 0..10 {
c.probe_or_insert(id(100 + k), op, [id(200 + k), id(0)]);
}
assert!(!restore_incrementally(10, 0, 1));
c.restore(token);
assert_eq!(c.len(), Plain2Id::new(1));
assert_eq!(c.probe(&op, &[id(1), id(2)]), Some(id(10)));
assert!(c.probe(&op, &[id(200), id(0)]).is_none());
}
#[cfg(debug_assertions)]
#[test]
fn index_check_rejects_a_stale_key() {
let mut c = FixedArityCache::<ENodeId, OpId, Plain2Id, 2>::new();
let op = OpId::new(0);
c.probe_or_insert(id(10), op, [id(1), id(2)]);
assert!(c.index_matches_rebuild());
let local_id = Plain2Id::new(0);
let fp = c.fingerprint(&op, &[id(1), id(2)]);
c.index.remove(&StoredKey { fp, local_id });
assert!(!c.index_matches_rebuild(), "a missing entry is a mismatch");
c.index.insert(
StoredKey {
fp: fp ^ 1,
local_id,
},
(),
);
assert!(!c.index_matches_rebuild(), "a stale key is a mismatch");
}
#[test]
fn recanonize_ac_merges_mult() {
type MSetChild = crate::containers::Pair<ENodeId, Multiplicity>;
let pair = |g, m| crate::containers::Pair {
a: g,
b: Multiplicity(m),
};
let mut c = VariableArityCache::<ENodeId, OpId, MSetChild, MSetNodeId, false>::new();
let op = OpId::new(0);
let elems: &[MSetChild] = &[pair(id(1), 1), pair(id(2), 1), pair(id(3), 1)];
c.probe_or_insert(id(10), op, elems);
let mut buf = Vec::new();
let mut collisions = Vec::new();
c.recanonize_node::<MSetCanon>(
MSetNodeId::new(0),
|g| {
if g == id(2) { id(1) } else { g }
},
&mut buf,
&mut collisions,
&mut Vec::new(),
crate::canon::CanonMode::PLAIN,
);
assert!(collisions.is_empty());
let expected: &[MSetChild] = &[pair(id(1), 2), pair(id(3), 1)];
assert!(c.probe(op, expected).is_some());
}
}