git.delta.rocks / jrsonnet / refs/commits / 3d527a7bcaaf

difftreelog

perf use pointer equality in std.equals

Yaroslav Bolyukin2021-01-12parent: #c214d99.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/obj.rs
1use crate::{evaluate_add_op, LazyBinding, Result, Val};2use indexmap::IndexMap;3use jrsonnet_interner::IStr;4use jrsonnet_parser::{ExprLocation, Visibility};5use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};67#[derive(Debug)]8pub struct ObjMember {9	pub add: bool,10	pub visibility: Visibility,11	pub invoke: LazyBinding,12	pub location: Option<ExprLocation>,13}1415// Field => This16type CacheKey = (IStr, usize);17#[derive(Debug)]18pub struct ObjValueInternals {19	super_obj: Option<ObjValue>,20	this_entries: Rc<HashMap<IStr, ObjMember>>,21	value_cache: RefCell<HashMap<CacheKey, Option<Val>>>,22}23#[derive(Clone)]24pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);25impl Debug for ObjValue {26	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {27		if let Some(super_obj) = self.0.super_obj.as_ref() {28			if f.alternate() {29				write!(f, "{:#?}", super_obj)?;30			} else {31				write!(f, "{:?}", super_obj)?;32			}33			write!(f, " + ")?;34		}35		let mut debug = f.debug_struct("ObjValue");36		for (name, member) in self.0.this_entries.iter() {37			debug.field(&name, member);38		}39		#[cfg(feature = "unstable")]40		{41			debug.finish_non_exhaustive()42		}43		#[cfg(not(feature = "unstable"))]44		{45			debug.finish()46		}47	}48}4950impl ObjValue {51	pub fn new(super_obj: Option<Self>, this_entries: Rc<HashMap<IStr, ObjMember>>) -> Self {52		Self(Rc::new(ObjValueInternals {53			super_obj,54			this_entries,55			value_cache: RefCell::new(HashMap::new()),56		}))57	}58	pub fn new_empty() -> Self {59		Self::new(None, Rc::new(HashMap::new()))60	}61	pub fn with_super(&self, super_obj: Self) -> Self {62		match &self.0.super_obj {63			None => Self::new(Some(super_obj), self.0.this_entries.clone()),64			Some(v) => Self::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),65		}66	}67	pub fn enum_fields(&self, handler: &impl Fn(&IStr, &Visibility)) {68		if let Some(s) = &self.0.super_obj {69			s.enum_fields(handler);70		}71		for (name, member) in self.0.this_entries.iter() {72			handler(name, &member.visibility);73		}74	}75	pub fn fields_visibility(&self) -> IndexMap<IStr, bool> {76		let out = Rc::new(RefCell::new(IndexMap::new()));77		self.enum_fields(&|name, visibility| {78			let mut out = out.borrow_mut();79			match visibility {80				Visibility::Normal => {81					if !out.contains_key(name) {82						out.insert(name.to_owned(), true);83					}84				}85				Visibility::Hidden => {86					out.insert(name.to_owned(), false);87				}88				Visibility::Unhide => {89					out.insert(name.to_owned(), true);90				}91			};92		});93		Rc::try_unwrap(out).unwrap().into_inner()94	}95	pub fn visible_fields(&self) -> Vec<IStr> {96		let mut visible_fields: Vec<_> = self97			.fields_visibility()98			.into_iter()99			.filter(|(_k, v)| *v)100			.map(|(k, _)| k)101			.collect();102		visible_fields.sort();103		visible_fields104	}105	pub fn get(&self, key: IStr) -> Result<Option<Val>> {106		Ok(self.get_raw(key, None)?)107	}108	pub(crate) fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {109		let real_this = real_this.unwrap_or(self);110		let cache_key = (key.clone(), Rc::as_ptr(&real_this.0) as usize);111112		if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {113			return Ok(v.clone());114		}115		let value = match (self.0.this_entries.get(&key), &self.0.super_obj) {116			(Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),117			(Some(k), Some(s)) => {118				let our = self.evaluate_this(k, real_this)?;119				if k.add {120					s.get_raw(key, Some(real_this))?121						.map_or(Ok(Some(our.clone())), |v| {122							Ok(Some(evaluate_add_op(&v, &our)?))123						})124				} else {125					Ok(Some(our))126				}127			}128			(None, Some(s)) => s.get_raw(key, Some(real_this)),129			(None, None) => Ok(None),130		}?;131		self.0132			.value_cache133			.borrow_mut()134			.insert(cache_key, value.clone());135		Ok(value)136	}137	fn evaluate_this(&self, v: &ObjMember, real_this: &Self) -> Result<Val> {138		Ok(v.invoke139			.evaluate(Some(real_this.clone()), self.0.super_obj.clone())?140			.evaluate()?)141	}142}143impl PartialEq for ObjValue {144	fn eq(&self, other: &Self) -> bool {145		Rc::ptr_eq(&self.0, &other.0)146	}147}
after · crates/jrsonnet-evaluator/src/obj.rs
1use crate::{evaluate_add_op, LazyBinding, Result, Val};2use indexmap::IndexMap;3use jrsonnet_interner::IStr;4use jrsonnet_parser::{ExprLocation, Visibility};5use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};67#[derive(Debug)]8pub struct ObjMember {9	pub add: bool,10	pub visibility: Visibility,11	pub invoke: LazyBinding,12	pub location: Option<ExprLocation>,13}1415// Field => This16type CacheKey = (IStr, usize);17#[derive(Debug)]18pub struct ObjValueInternals {19	super_obj: Option<ObjValue>,20	this_entries: Rc<HashMap<IStr, ObjMember>>,21	value_cache: RefCell<HashMap<CacheKey, Option<Val>>>,22}23#[derive(Clone)]24pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);25impl Debug for ObjValue {26	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {27		if let Some(super_obj) = self.0.super_obj.as_ref() {28			if f.alternate() {29				write!(f, "{:#?}", super_obj)?;30			} else {31				write!(f, "{:?}", super_obj)?;32			}33			write!(f, " + ")?;34		}35		let mut debug = f.debug_struct("ObjValue");36		for (name, member) in self.0.this_entries.iter() {37			debug.field(&name, member);38		}39		#[cfg(feature = "unstable")]40		{41			debug.finish_non_exhaustive()42		}43		#[cfg(not(feature = "unstable"))]44		{45			debug.finish()46		}47	}48}4950impl ObjValue {51	pub fn new(super_obj: Option<Self>, this_entries: Rc<HashMap<IStr, ObjMember>>) -> Self {52		Self(Rc::new(ObjValueInternals {53			super_obj,54			this_entries,55			value_cache: RefCell::new(HashMap::new()),56		}))57	}58	pub fn new_empty() -> Self {59		Self::new(None, Rc::new(HashMap::new()))60	}61	pub fn with_super(&self, super_obj: Self) -> Self {62		match &self.0.super_obj {63			None => Self::new(Some(super_obj), self.0.this_entries.clone()),64			Some(v) => Self::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),65		}66	}67	pub fn enum_fields(&self, handler: &impl Fn(&IStr, &Visibility)) {68		if let Some(s) = &self.0.super_obj {69			s.enum_fields(handler);70		}71		for (name, member) in self.0.this_entries.iter() {72			handler(name, &member.visibility);73		}74	}75	pub fn fields_visibility(&self) -> IndexMap<IStr, bool> {76		let out = Rc::new(RefCell::new(IndexMap::new()));77		self.enum_fields(&|name, visibility| {78			let mut out = out.borrow_mut();79			match visibility {80				Visibility::Normal => {81					if !out.contains_key(name) {82						out.insert(name.to_owned(), true);83					}84				}85				Visibility::Hidden => {86					out.insert(name.to_owned(), false);87				}88				Visibility::Unhide => {89					out.insert(name.to_owned(), true);90				}91			};92		});93		Rc::try_unwrap(out).unwrap().into_inner()94	}95	pub fn visible_fields(&self) -> Vec<IStr> {96		let mut visible_fields: Vec<_> = self97			.fields_visibility()98			.into_iter()99			.filter(|(_k, v)| *v)100			.map(|(k, _)| k)101			.collect();102		visible_fields.sort();103		visible_fields104	}105	pub fn get(&self, key: IStr) -> Result<Option<Val>> {106		Ok(self.get_raw(key, None)?)107	}108	pub(crate) fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {109		let real_this = real_this.unwrap_or(self);110		let cache_key = (key.clone(), Rc::as_ptr(&real_this.0) as usize);111112		if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {113			return Ok(v.clone());114		}115		let value = match (self.0.this_entries.get(&key), &self.0.super_obj) {116			(Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),117			(Some(k), Some(s)) => {118				let our = self.evaluate_this(k, real_this)?;119				if k.add {120					s.get_raw(key, Some(real_this))?121						.map_or(Ok(Some(our.clone())), |v| {122							Ok(Some(evaluate_add_op(&v, &our)?))123						})124				} else {125					Ok(Some(our))126				}127			}128			(None, Some(s)) => s.get_raw(key, Some(real_this)),129			(None, None) => Ok(None),130		}?;131		self.0132			.value_cache133			.borrow_mut()134			.insert(cache_key, value.clone());135		Ok(value)136	}137	fn evaluate_this(&self, v: &ObjMember, real_this: &Self) -> Result<Val> {138		Ok(v.invoke139			.evaluate(Some(real_this.clone()), self.0.super_obj.clone())?140			.evaluate()?)141	}142143	pub fn ptr_eq(a: &ObjValue, b: &ObjValue) -> bool {144		Rc::ptr_eq(&a.0, &b.0)145	}146}147impl PartialEq for ObjValue {148	fn eq(&self, other: &Self) -> bool {149		Rc::ptr_eq(&self.0, &other.0)150	}151}
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -253,6 +253,14 @@
 			}
 		}
 	}
+
+	pub fn ptr_eq(a: &ArrValue, b: &ArrValue) -> bool {
+		match (a, b) {
+			(ArrValue::Lazy(a), ArrValue::Lazy(b)) => Rc::ptr_eq(a, b),
+			(ArrValue::Eager(a), ArrValue::Eager(b)) => Rc::ptr_eq(a, b),
+			_ => false,
+		}
+	}
 }
 
 impl From<Vec<LazyVal>> for ArrValue {
@@ -533,8 +541,10 @@
 		return Ok(false);
 	}
 	match (val_a, val_b) {
-		// Cant test for ptr equality, because all fields needs to be evaluated
 		(Val::Arr(a), Val::Arr(b)) => {
+			if ArrValue::ptr_eq(a, b) {
+				return Ok(true);
+			}
 			if a.len() != b.len() {
 				return Ok(false);
 			}
@@ -546,6 +556,9 @@
 			Ok(true)
 		}
 		(Val::Obj(a), Val::Obj(b)) => {
+			if ObjValue::ptr_eq(a, b) {
+				return Ok(true);
+			}
 			let fields = a.visible_fields();
 			if fields != b.visible_fields() {
 				return Ok(false);