difftreelog
perf use weak objvalue as cache key
in: master
4 files changed
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth15# Allows library authors to throw custom errors15# Allows library authors to throw custom errors16anyhow-error = ["anyhow"]16anyhow-error = ["anyhow"]1718# Unlocks extra features, but works only on unstable19unstable = []201721[dependencies]18[dependencies]22jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }19jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth133 }133 }134 Ok(self.extend(new, new_dollar, this, super_obj))134 Ok(self.extend(new, new_dollar, this, super_obj))135 }135 }136 #[cfg(feature = "unstable")]137 pub fn into_weak(self) -> WeakContext {138 WeakContext(Rc::downgrade(&self.0))139 }140}136}141137142impl Default for Context {138impl Default for Context {crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1#![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}654655fn 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}661662#[test]663fn weak_unsafe() {664 let a = Cc::new(1);665 let b = Cc::new(2);666667 let aw1 = a.clone().downgrade();668 let aw2 = a.clone().downgrade();669 let aw3 = a.clone().downgrade();670671 let bw = b.clone().downgrade();672673 assert!(weak_ptr_eq(aw1, aw2));674 assert!(!weak_ptr_eq(aw3, bw));675}655676656#[cfg(test)]677#[cfg(test)]657pub mod tests {678pub mod tests {crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth1use crate::gc::{GcHashMap, GcHashSet, TraceBox};1use crate::gc::{GcHashMap, GcHashSet, TraceBox};2use crate::operator::evaluate_add_op;2use crate::operator::evaluate_add_op;3use crate::{cc_ptr_eq, Bindable, LazyBinding, LazyVal, Result, Val};3use crate::{cc_ptr_eq, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal, Result, Val};4use gcmodule::{Cc, Trace};4use gcmodule::{Cc, Trace, Weak};5use jrsonnet_interner::IStr;5use jrsonnet_interner::IStr;6use jrsonnet_parser::{ExprLocation, Visibility};6use jrsonnet_parser::{ExprLocation, Visibility};7use rustc_hash::FxHashMap;7use rustc_hash::FxHashMap;22}22}232324// Field => This24// Field => This25type CacheKey = (IStr, ObjValue);25type CacheKey = (IStr, WeakObjValue);26#[derive(Trace)]26#[derive(Trace)]27#[force_tracking]27#[force_tracking]28pub struct ObjValueInternals {28pub struct ObjValueInternals {34 value_cache: RefCell<GcHashMap<CacheKey, Option<Val>>>,34 value_cache: RefCell<GcHashMap<CacheKey, Option<Val>>>,35}35}3637#[derive(Clone, Trace)]38pub struct WeakObjValue(#[skip_trace] pub(crate) Weak<ObjValueInternals>);3940impl PartialEq for WeakObjValue {41 fn eq(&self, other: &Self) -> bool {42 weak_ptr_eq(self.0.clone(), other.0.clone())43 }44}4546impl Eq for WeakObjValue {}47impl Hash for WeakObjValue {48 fn hash<H: Hasher>(&self, hasher: &mut H) {49 hasher.write_usize(weak_raw(self.0.clone()) as usize)50 }51}365237#[derive(Clone, Trace)]53#[derive(Clone, Trace)]38pub struct ObjValue(pub(crate) Cc<ObjValueInternals>);54pub struct ObjValue(pub(crate) Cc<ObjValueInternals>);50 for (name, member) in self.0.this_entries.iter() {66 for (name, member) in self.0.this_entries.iter() {51 debug.field(name, member);67 debug.field(name, member);52 }68 }53 #[cfg(feature = "unstable")]54 {55 debug.finish_non_exhaustive()69 debug.finish_non_exhaustive()56 }57 #[cfg(not(feature = "unstable"))]58 {59 debug.finish()60 }61 }70 }62}71}6372217226218 fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {227 fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {219 let real_this = real_this.unwrap_or(self);228 let real_this = real_this.unwrap_or(self);220 let cache_key = (key.clone(), real_this.clone());229 let cache_key = (key.clone(), WeakObjValue(real_this.0.downgrade()));221230222 if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {231 if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {223 return Ok(v.clone());232 return Ok(v.clone());