git.delta.rocks / jrsonnet / refs/commits / d710b4fe6639

difftreelog

perf use weak objvalue as cache key

Yaroslav Bolyukin2022-03-21parent: #7efefe2.patch.diff
in: master

4 files changed

modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -15,9 +15,6 @@
 # Allows library authors to throw custom errors
 anyhow-error = ["anyhow"]
 
-# Unlocks extra features, but works only on unstable
-unstable = []
-
 [dependencies]
 jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }
 jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.2" }
modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -133,10 +133,6 @@
 		}
 		Ok(self.extend(new, new_dollar, this, super_obj))
 	}
-	#[cfg(feature = "unstable")]
-	pub fn into_weak(self) -> WeakContext {
-		WeakContext(Rc::downgrade(&self.0))
-	}
 }
 
 impl Default for Context {
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
1#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]
2#![warn(clippy::all, clippy::nursery)]1#![warn(clippy::all, clippy::nursery)]
3#![allow(2#![allow(
4 macro_expanded_macro_exports_accessed_by_absolute_paths,3 macro_expanded_macro_exports_accessed_by_absolute_paths,
31pub use evaluate::*;30pub use evaluate::*;
32use function::{Builtin, TlaArg};31use function::{Builtin, TlaArg};
33use gc::{GcHashMap, TraceBox};32use gc::{GcHashMap, TraceBox};
34use gcmodule::{Cc, Trace};33use gcmodule::{Cc, Trace, Weak};
35pub use import::*;34pub use import::*;
36pub use jrsonnet_interner::IStr;35pub use jrsonnet_interner::IStr;
37use jrsonnet_parser::*;36use jrsonnet_parser::*;
653 std::ptr::eq(a, b)652 std::ptr::eq(a, b)
654}653}
654
655fn weak_raw<T>(a: Weak<T>) -> *const () {
656 unsafe { std::mem::transmute(a) }
657}
658fn weak_ptr_eq<T>(a: Weak<T>, b: Weak<T>) -> bool {
659 std::ptr::eq(weak_raw(a), weak_raw(b))
660}
661
662#[test]
663fn weak_unsafe() {
664 let a = Cc::new(1);
665 let b = Cc::new(2);
666
667 let aw1 = a.clone().downgrade();
668 let aw2 = a.clone().downgrade();
669 let aw3 = a.clone().downgrade();
670
671 let bw = b.clone().downgrade();
672
673 assert!(weak_ptr_eq(aw1, aw2));
674 assert!(!weak_ptr_eq(aw3, bw));
675}
655676
656#[cfg(test)]677#[cfg(test)]
657pub mod tests {678pub mod tests {
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -1,7 +1,7 @@
 use crate::gc::{GcHashMap, GcHashSet, TraceBox};
 use crate::operator::evaluate_add_op;
-use crate::{cc_ptr_eq, Bindable, LazyBinding, LazyVal, Result, Val};
-use gcmodule::{Cc, Trace};
+use crate::{cc_ptr_eq, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal, Result, Val};
+use gcmodule::{Cc, Trace, Weak};
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{ExprLocation, Visibility};
 use rustc_hash::FxHashMap;
@@ -22,7 +22,7 @@
 }
 
 // Field => This
-type CacheKey = (IStr, ObjValue);
+type CacheKey = (IStr, WeakObjValue);
 #[derive(Trace)]
 #[force_tracking]
 pub struct ObjValueInternals {
@@ -35,6 +35,22 @@
 }
 
 #[derive(Clone, Trace)]
+pub struct WeakObjValue(#[skip_trace] pub(crate) Weak<ObjValueInternals>);
+
+impl PartialEq for WeakObjValue {
+	fn eq(&self, other: &Self) -> bool {
+		weak_ptr_eq(self.0.clone(), other.0.clone())
+	}
+}
+
+impl Eq for WeakObjValue {}
+impl Hash for WeakObjValue {
+	fn hash<H: Hasher>(&self, hasher: &mut H) {
+		hasher.write_usize(weak_raw(self.0.clone()) as usize)
+	}
+}
+
+#[derive(Clone, Trace)]
 pub struct ObjValue(pub(crate) Cc<ObjValueInternals>);
 impl Debug for ObjValue {
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -50,14 +66,7 @@
 		for (name, member) in self.0.this_entries.iter() {
 			debug.field(name, member);
 		}
-		#[cfg(feature = "unstable")]
-		{
-			debug.finish_non_exhaustive()
-		}
-		#[cfg(not(feature = "unstable"))]
-		{
-			debug.finish()
-		}
+		debug.finish_non_exhaustive()
 	}
 }
 
@@ -217,7 +226,7 @@
 
 	fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
 		let real_this = real_this.unwrap_or(self);
-		let cache_key = (key.clone(), real_this.clone());
+		let cache_key = (key.clone(), WeakObjValue(real_this.0.downgrade()));
 
 		if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {
 			return Ok(v.clone());