Skip to content

Commit 271aaf4

Browse files
committed
add a non-mutating probe_value method for fast paths
1 parent bde9953 commit 271aaf4

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/unify/mod.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ impl<S: UnificationStoreBase> UnificationTable<S> {
295295
pub fn len(&self) -> usize {
296296
self.values.len()
297297
}
298+
299+
/// Obtains the current value for a particular key.
300+
/// Not for end-users; they can use `probe_value`.
301+
fn value(&self, key: S::Key) -> &VarValue<S::Key> {
302+
&self.values[key.index() as usize]
303+
}
298304
}
299305

300306
impl<S: UnificationStoreMut> UnificationTable<S> {
@@ -325,12 +331,6 @@ impl<S: UnificationStoreMut> UnificationTable<S> {
325331
});
326332
}
327333

328-
/// Obtains the current value for a particular key.
329-
/// Not for end-users; they can use `probe_value`.
330-
fn value(&self, key: S::Key) -> &VarValue<S::Key> {
331-
&self.values[key.index() as usize]
332-
}
333-
334334
/// Find the root node for `vid`. This uses the standard
335335
/// union-find algorithm with path compression:
336336
/// <http://en.wikipedia.org/wiki/Disjoint-set_data_structure>.
@@ -447,6 +447,31 @@ impl<S: UnificationStoreMut> UnificationTable<S> {
447447
/// ////////////////////////////////////////////////////////////////////////
448448
/// Public API
449449
450+
impl<S, K, V> UnificationTable<S>
451+
where
452+
S: UnificationStoreBase<Key = K, Value = V>,
453+
K: UnifyKey<Value = V>,
454+
V: UnifyValue,
455+
{
456+
/// Attempts to return the current value for the given key. If the key has
457+
/// been union'd, this may give the current value from the current root or `None`.
458+
///
459+
/// This is useful for fast-paths that want to avoid pointer-chasing.
460+
#[inline]
461+
pub fn try_probe_value<'a, K1>(&'a self, id: K1) -> Option<&'a V>
462+
where
463+
K1: Into<K>,
464+
K: 'a,
465+
{
466+
let id = id.into();
467+
let v = self.value(id);
468+
if v.parent == id {
469+
return Some(&v.value);
470+
}
471+
None
472+
}
473+
}
474+
450475
impl<S, K, V> UnificationTable<S>
451476
where
452477
S: UnificationStoreMut<Key = K, Value = V>,

0 commit comments

Comments
 (0)