git.delta.rocks / jrsonnet / refs/commits / 64f3674f18c4

difftreelog

perf specialize super.field gets

Yaroslav Bolyukin2022-11-20parent: #30b370e.patch.diff
in: master

4 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -139,7 +139,7 @@
 }
 
 fn main_real(s: &State, opts: Opts) -> Result<(), Error> {
-	let (_stack_guard, tla, _gc_guard) = opts.general.configure(s)?;
+	let (tla, _gc_guard) = opts.general.configure(s)?;
 	let manifest_format = opts.manifest.configure(s)?;
 
 	let input = opts.input.input.ok_or(Error::MissingInputArgument)?;
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
6use std::{env, marker::PhantomData, path::PathBuf};6use std::{env, marker::PhantomData, path::PathBuf};
77
8use clap::Parser;8use clap::Parser;
9use jrsonnet_evaluator::{9use jrsonnet_evaluator::{error::Result, stack::set_stack_depth_limit, FileImportResolver, State};
10 error::Result, stack::StackDepthLimitOverrideGuard, FileImportResolver, State,
11};
12use jrsonnet_gcmodule::with_thread_object_space;10use jrsonnet_gcmodule::with_thread_object_space;
13pub use manifest::*;11pub use manifest::*;
48 jpath: Vec<PathBuf>,46 jpath: Vec<PathBuf>,
49}47}
50impl ConfigureState for MiscOpts {48impl ConfigureState for MiscOpts {
51 type Guards = StackDepthLimitOverrideGuard;49 type Guards = ();
52 fn configure(&self, s: &State) -> Result<Self::Guards> {50 fn configure(&self, s: &State) -> Result<Self::Guards> {
53 let mut library_paths = self.jpath.clone();51 let mut library_paths = self.jpath.clone();
54 library_paths.reverse();52 library_paths.reverse();
5856
59 s.set_import_resolver(Box::new(FileImportResolver::new(library_paths)));57 s.set_import_resolver(Box::new(FileImportResolver::new(library_paths)));
6058
61 let _depth_limit = jrsonnet_evaluator::stack::limit_stack_depth(self.max_stack);59 set_stack_depth_limit(self.max_stack);
62 Ok(_depth_limit)60 Ok(())
63 }61 }
64}62}
6563
8179
82impl ConfigureState for GeneralOpts {80impl ConfigureState for GeneralOpts {
83 type Guards = (81 type Guards = (
84 <MiscOpts as ConfigureState>::Guards,
85 <TlaOpts as ConfigureState>::Guards,82 <TlaOpts as ConfigureState>::Guards,
86 <GcOpts as ConfigureState>::Guards,83 <GcOpts as ConfigureState>::Guards,
87 );84 );
88 fn configure(&self, s: &State) -> Result<Self::Guards> {85 fn configure(&self, s: &State) -> Result<Self::Guards> {
89 // Configure trace first, because tla-code/ext-code can throw86 // Configure trace first, because tla-code/ext-code can throw
90 let misc_guards = self.misc.configure(s)?;87 self.misc.configure(s)?;
91 let tla_guards = self.tla.configure(s)?;88 let tla_guards = self.tla.configure(s)?;
92 self.std.configure(s)?;89 self.std.configure(s)?;
93 let gc_guards = self.gc.configure(s)?;90 let gc_guards = self.gc.configure(s)?;
94 Ok((misc_guards, tla_guards, gc_guards))91 Ok((tla_guards, gc_guards))
95 }92 }
96}93}
9794
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -389,7 +389,7 @@
 			ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
 				ctx.this()
 					.clone()
-					.expect("if super exists - then this should to"),
+					.expect("if super exists - then this should too"),
 			),
 		),
 		Literal(LiteralType::Dollar) => {
@@ -408,6 +408,21 @@
 			|| format!("variable <{name}> access"),
 			|| ctx.binding(name.clone())?.evaluate(),
 		)?,
+		Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
+			let name = evaluate(ctx.clone(), index)?;
+			let Val::Str(name) = name else {
+				throw!(ValueIndexMustBeTypeGot(
+					ValType::Obj,
+					ValType::Str,
+					name.value_type(),
+				))
+			};
+			ctx.super_obj()
+				.clone()
+				.expect("no super found")
+				.get_for(name, ctx.this().clone().expect("no this found"))?
+				.expect("value not found")
+		}
 		Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {
 			(Val::Obj(v), Val::Str(key)) => State::push(
 				CallLocation::new(loc),
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -128,7 +128,7 @@
 	assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,
 	assertions_ran: RefCell<GcHashSet<ObjValue>>,
 	this_entries: Cc<GcHashMap<IStr, ObjMember>>,
-	value_cache: RefCell<GcHashMap<IStr, CacheValue>>,
+	value_cache: RefCell<GcHashMap<(IStr, Option<WeakObjValue>), CacheValue>>,
 }
 
 #[derive(Clone, Trace)]
@@ -387,7 +387,8 @@
 
 	pub fn get(&self, key: IStr) -> Result<Option<Val>> {
 		self.run_assertions()?;
-		if let Some(v) = self.0.value_cache.borrow().get(&key) {
+		let cache_key = (key.clone(), None);
+		if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {
 			return Ok(match v {
 				CacheValue::Cached(v) => Some(v.clone()),
 				CacheValue::NotFound => None,
@@ -398,21 +399,48 @@
 		self.0
 			.value_cache
 			.borrow_mut()
-			.insert(key.clone(), CacheValue::Pending);
+			.insert(cache_key.clone(), CacheValue::Pending);
 		let value = self
-			.get_raw(
-				key.clone(),
-				self.0.this.clone().unwrap_or_else(|| self.clone()),
-			)
+			.get_raw(key, self.0.this.clone().unwrap_or_else(|| self.clone()))
 			.map_err(|e| {
 				self.0
 					.value_cache
 					.borrow_mut()
-					.insert(key.clone(), CacheValue::Errored(e.clone()));
+					.insert(cache_key.clone(), CacheValue::Errored(e.clone()));
 				e
 			})?;
 		self.0.value_cache.borrow_mut().insert(
-			key,
+			cache_key,
+			value
+				.as_ref()
+				.map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())),
+		);
+		Ok(value)
+	}
+	pub fn get_for(&self, key: IStr, this: Self) -> Result<Option<Val>> {
+		self.run_assertions()?;
+		let cache_key = (key.clone(), Some(this.clone().downgrade()));
+		if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {
+			return Ok(match v {
+				CacheValue::Cached(v) => Some(v.clone()),
+				CacheValue::NotFound => None,
+				CacheValue::Pending => throw!(InfiniteRecursionDetected),
+				CacheValue::Errored(e) => return Err(e.clone()),
+			});
+		}
+		self.0
+			.value_cache
+			.borrow_mut()
+			.insert(cache_key.clone(), CacheValue::Pending);
+		let value = self.get_raw(key, this).map_err(|e| {
+			self.0
+				.value_cache
+				.borrow_mut()
+				.insert(cache_key.clone(), CacheValue::Errored(e.clone()));
+			e
+		})?;
+		self.0.value_cache.borrow_mut().insert(
+			cache_key,
 			value
 				.as_ref()
 				.map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())),