From 271aaf4d1f1da310d8e332005badb736e416796e Mon Sep 17 00:00:00 2001 From: The 8472 Date: Mon, 6 Mar 2023 14:57:00 +0100 Subject: [PATCH 1/2] add a non-mutating probe_value method for fast paths --- src/unify/mod.rs | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/unify/mod.rs b/src/unify/mod.rs index defd7b7..cc3cd83 100644 --- a/src/unify/mod.rs +++ b/src/unify/mod.rs @@ -295,6 +295,12 @@ impl UnificationTable { pub fn len(&self) -> usize { self.values.len() } + + /// Obtains the current value for a particular key. + /// Not for end-users; they can use `probe_value`. + fn value(&self, key: S::Key) -> &VarValue { + &self.values[key.index() as usize] + } } impl UnificationTable { @@ -325,12 +331,6 @@ impl UnificationTable { }); } - /// Obtains the current value for a particular key. - /// Not for end-users; they can use `probe_value`. - fn value(&self, key: S::Key) -> &VarValue { - &self.values[key.index() as usize] - } - /// Find the root node for `vid`. This uses the standard /// union-find algorithm with path compression: /// . @@ -447,6 +447,31 @@ impl UnificationTable { /// //////////////////////////////////////////////////////////////////////// /// Public API +impl UnificationTable +where + S: UnificationStoreBase, + K: UnifyKey, + V: UnifyValue, +{ + /// Attempts to return the current value for the given key. If the key has + /// been union'd, this may give the current value from the current root or `None`. + /// + /// This is useful for fast-paths that want to avoid pointer-chasing. + #[inline] + pub fn try_probe_value<'a, K1>(&'a self, id: K1) -> Option<&'a V> + where + K1: Into, + K: 'a, + { + let id = id.into(); + let v = self.value(id); + if v.parent == id { + return Some(&v.value); + } + None + } +} + impl UnificationTable where S: UnificationStoreMut, From b02546ed398223fe8191d07f797d0483193b2465 Mon Sep 17 00:00:00 2001 From: the8472 Date: Tue, 14 Mar 2023 21:32:00 +0100 Subject: [PATCH 2/2] [review] Improve try_probe_value doccomment Co-authored-by: Niko Matsakis --- src/unify/mod.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/unify/mod.rs b/src/unify/mod.rs index cc3cd83..604f3bd 100644 --- a/src/unify/mod.rs +++ b/src/unify/mod.rs @@ -453,10 +453,7 @@ where K: UnifyKey, V: UnifyValue, { - /// Attempts to return the current value for the given key. If the key has - /// been union'd, this may give the current value from the current root or `None`. - /// - /// This is useful for fast-paths that want to avoid pointer-chasing. + /// Obtains current value for key without any pointer chasing; may return `None` if key has been union'd. #[inline] pub fn try_probe_value<'a, K1>(&'a self, id: K1) -> Option<&'a V> where