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

difftreelog

refactor unified ContextBuilder

lzpkwkksYaroslav Bolyukin2026-04-05parent: #edb8e7e.patch.diff
in: master

17 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -699,6 +699,7 @@
  "bitmaps",
  "rand_core 0.6.4",
  "rand_xoshiro",
+ "refpool",
  "sized-chunks",
  "typenum",
  "version_check",
@@ -861,9 +862,9 @@
 
 [[package]]
 name = "jrsonnet-gcmodule"
-version = "0.4.3"
+version = "0.4.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8a6a63a6e55ba82764e483d7f8a181f25db95a8f25da8ae6520e95a5fe39c6a6"
+checksum = "21dd97b40cbfb2043094219f95d96519858ba1aee4e8260eb048a1774832a517"
 dependencies = [
  "im-rc",
  "jrsonnet-gcmodule-derive",
@@ -871,9 +872,9 @@
 
 [[package]]
 name = "jrsonnet-gcmodule-derive"
-version = "0.4.3"
+version = "0.4.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "095fe3c4c0acf32de80205a8a479ef63c216b9efb0024dec9eb7fe1c5ef1f1a1"
+checksum = "ede3d0445c2a7d7adab0a3cc33bdb33df78ffebebc21a2848c221526cb1795d4"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -1420,6 +1421,12 @@
 ]
 
 [[package]]
+name = "refpool"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "369e86b80fa7dc8c561dd9613a5bf25c59d2d3073cd66c47fd9e39802f0ecb58"
+
+[[package]]
 name = "regex"
 version = "1.12.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1633,6 +1640,7 @@
 checksum = "16d69225bde7a69b235da73377861095455d298f2b970996eec25ddbb42b3d1e"
 dependencies = [
  "bitmaps",
+ "refpool",
  "typenum",
 ]
 
@@ -1738,6 +1746,7 @@
  "jrsonnet-evaluator",
  "jrsonnet-gcmodule",
  "jrsonnet-stdlib",
+ "mimallocator",
  "serde",
  "serde_json",
 ]
modifiedCargo.tomldiffbeforeafterboth
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -22,7 +22,7 @@
 jrsonnet-cli = { path = "./crates/jrsonnet-cli", version = "0.5.0-pre98" }
 jrsonnet-types = { path = "./crates/jrsonnet-types", version = "0.5.0-pre98" }
 jrsonnet-formatter = { path = "./crates/jrsonnet-formatter", version = "0.5.0-pre98" }
-jrsonnet-gcmodule = { version = "0.4.3", features = ["im-rc"] }
+jrsonnet-gcmodule = { version = "0.4.4", features = ["im-rc"] }
 # Diagnostics.
 # hi-doc is my library, which handles text formatting very well, but isn't polished enough yet
 # Previous implementation was based on annotate-snippets, which I don't like for many reasons.
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -200,7 +200,7 @@
 		val = s.evaluate_snippet_with(
 			"<exp_apply>".to_owned(),
 			&apply,
-			InitialUnderscore(Thunk::evaluated(val)),
+			&InitialUnderscore(Thunk::evaluated(val)),
 		)?;
 	}
 
modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -76,7 +76,7 @@
   "Hash",
   "PartialEq",
 ] }
-im-rc = "15.1.0"
+im-rc = { version = "15.1.0", features = ["pool"] }
 
 [build-dependencies]
 rustversion = "1.0.22"
modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -3,11 +3,11 @@
 use educe::Educe;
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
-use rustc_hash::FxHashMap;
+use rustc_hash::{FxHashMap, FxHashSet};
 
 use crate::{
-	ObjValue, Pending, Result, SupThis, Thunk, Val, error::ErrorKind::*, gc::WithCapacityExt as _,
-	map::LayeredHashMap,
+	ObjValue, Pending, Result, SupThis, Thunk, Val, bail, error::ErrorKind::*,
+	gc::WithCapacityExt as _,
 };
 /// Context keeps information about current lexical code location
 ///
@@ -20,7 +20,9 @@
 struct ContextInternal {
 	dollar: Option<ObjValue>,
 	sup_this: Option<SupThis>,
-	bindings: LayeredHashMap,
+	bindings: FxHashMap<IStr, Thunk<Val>>,
+
+	branch_point: Option<Context>,
 }
 impl Context {
 	pub fn new_future() -> Pending<Self> {
@@ -71,14 +73,18 @@
 			return Ok(val);
 		}
 
+		if let Some(branch_point) = &self.0.branch_point {
+			return branch_point.binding(name);
+		}
+
 		let mut heap = Vec::new();
-		self.0.bindings.clone().iter_keys(|k| {
-			let conf = strsim::jaro_winkler(&k as &str, &name as &str);
+		for k in self.0.bindings.keys() {
+			let conf = strsim::jaro_winkler(k as &str, &name as &str);
 			if conf < 0.8 {
-				return;
+				continue;
 			}
-			heap.push((conf, k));
-		});
+			heap.push((conf, k.clone()));
+		}
 		heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));
 
 		bail!(VariableIsNotDefined(
@@ -98,103 +104,91 @@
 	}
 
 	#[must_use]
-	pub fn with_var(self, name: impl Into<IStr>, value: Val) -> Self {
-		let mut new_bindings = FxHashMap::with_capacity(1);
-		new_bindings.insert(name.into(), Thunk::evaluated(value));
-		self.extend_bindings(new_bindings)
-	}
-
-	#[must_use]
-	pub fn extend_bindings_sup_this(
-		self,
-		new_bindings: FxHashMap<IStr, Thunk<Val>>,
-		sup_this: SupThis,
-	) -> Self {
-		let ctx = &self;
-		let dollar = ctx
-			.0
-			.dollar
-			.clone()
-			.or_else(|| Some(sup_this.this().clone()));
-		let bindings = if new_bindings.is_empty() {
-			ctx.0.bindings.clone()
+	pub fn branch_point(self) -> Self {
+		if self.0.bindings.is_empty() {
+			self
 		} else {
-			ctx.0.bindings.clone().extend(new_bindings)
-		};
-		Self(Cc::new(ContextInternal {
-			dollar,
-			sup_this: Some(sup_this),
-			bindings,
-		}))
-	}
-	#[must_use]
-	pub fn extend_bindings(self, new_bindings: FxHashMap<IStr, Thunk<Val>>) -> Self {
-		if new_bindings.is_empty() {
-			return self;
+			ContextBuilder::extend(self).build()
 		}
-		let ctx = &self;
-		let bindings = if new_bindings.is_empty() {
-			ctx.0.bindings.clone()
-		} else {
-			ctx.0.bindings.clone().extend(new_bindings)
-		};
-		Self(Cc::new(ContextInternal {
-			dollar: ctx.0.dollar.clone(),
-			sup_this: ctx.0.sup_this.clone(),
-			bindings,
-		}))
 	}
 }
 
-#[derive(Default)]
+#[derive(Clone)]
 pub struct ContextBuilder {
+	dollar: Option<ObjValue>,
+	sup_this: Option<SupThis>,
 	bindings: FxHashMap<IStr, Thunk<Val>>,
-	extend: Option<Context>,
+	filled: FxHashSet<IStr>,
+	branch_point: Option<Context>,
 }
 
 impl ContextBuilder {
 	pub fn new() -> Self {
-		Self::with_capacity(0)
+		Self {
+			dollar: None,
+			sup_this: None,
+			bindings: FxHashMap::new(),
+			filled: FxHashSet::new(),
+			branch_point: None,
+		}
 	}
 
-	pub fn with_capacity(capacity: usize) -> Self {
+	pub fn extend_fast(parent: Context) -> Self {
 		Self {
-			bindings: FxHashMap::with_capacity(capacity),
-			extend: None,
+			dollar: parent.0.dollar.clone(),
+			sup_this: parent.0.sup_this.clone(),
+			bindings: parent.0.bindings.clone(),
+			filled: FxHashSet::new(),
+			branch_point: parent.0.branch_point.clone(),
 		}
 	}
 
 	pub fn extend(parent: Context) -> Self {
 		Self {
+			dollar: parent.0.dollar.clone(),
+			sup_this: parent.0.sup_this.clone(),
 			bindings: FxHashMap::new(),
-			extend: Some(parent),
+			filled: FxHashSet::new(),
+			branch_point: Some(parent.clone()),
 		}
 	}
 
-	/// # Panics
-	///
-	/// If `name` is already bound. Makes no sense to bind same local multiple times,
-	/// unless it is separate context layers.
-	pub fn bind(&mut self, name: impl Into<IStr>, value: Thunk<Val>) -> &mut Self {
-		let old = self.bindings.insert(name.into(), value);
-		assert!(old.is_none(), "variable bound twice in single context call");
+	pub fn bind(&mut self, name: impl Into<IStr>, value: Thunk<Val>) {
+		let _ = self.bindings.insert(name.into(), value);
+	}
+	/// After commit, binds would shadow the previous declarations
+	#[must_use]
+	pub fn commit(mut self) -> Self {
+		self.filled.clear();
 		self
 	}
-	pub fn binds(&mut self, bindings: FxHashMap<IStr, Thunk<Val>>) -> &mut Self {
-		for (k, v) in bindings {
-			self.bind(k, v);
+	pub fn try_bind(&mut self, name: impl Into<IStr>, value: Thunk<Val>) -> Result<()> {
+		let name = name.into();
+		if !self.filled.insert(name.clone()) {
+			bail!(DuplicateLocalVar(name))
 		}
-		self
+		self.bind(name, value);
+		Ok(())
 	}
 	pub fn build(self) -> Context {
-		if let Some(parent) = self.extend {
-			parent.extend_bindings(self.bindings)
-		} else {
-			Context(Cc::new(ContextInternal {
-				bindings: LayeredHashMap::new(self.bindings),
-				dollar: None,
-				sup_this: None,
-			}))
+		Context(Cc::new(ContextInternal {
+			dollar: self.dollar,
+			sup_this: self.sup_this,
+			bindings: self.bindings,
+			branch_point: self.branch_point,
+		}))
+	}
+	pub fn build_sup_this(mut self, st: SupThis) -> Context {
+		if self.dollar.is_none() {
+			self.dollar = Some(st.this().clone());
 		}
+		self.sup_this = Some(st);
+		self.build()
+	}
+}
+
+impl Default for ContextBuilder {
+	fn default() -> Self {
+		Self::new()
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -249,6 +249,10 @@
 
 #[derive(Clone, Trace)]
 pub struct Error(Box<(ErrorKind, StackTrace)>);
+
+#[cfg(target_pointer_width = "64")]
+static_assertions::assert_eq_size!(Error, usize);
+
 impl Error {
 	pub fn new(e: ErrorKind) -> Self {
 		Self(Box::new((e, StackTrace(vec![]))))
modifiedcrates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -1,30 +1,23 @@
-use std::{collections::HashMap, hash::BuildHasher};
-
-use jrsonnet_interner::IStr;
 use jrsonnet_ir::{BindSpec, Destruct};
 
 #[cfg(feature = "exp-preserve-order")]
 use crate::evaluate;
 use crate::{
-	Context, Pending, Thunk, Val, bail,
-	error::{ErrorKind::*, Result},
-	evaluate_method, evaluate_named_param,
+	Context, ContextBuilder, Pending, Thunk, Val, error::Result, evaluate_method,
+	evaluate_named_param,
 };
 
 #[allow(clippy::too_many_lines)]
 #[allow(unused_variables)]
-pub fn destruct<H: BuildHasher>(
+pub fn destruct(
 	d: &Destruct,
 	parent: Thunk<Val>,
 	fctx: Pending<Context>,
-	new_bindings: &mut HashMap<IStr, Thunk<Val>, H>,
+	new_bindings: &mut ContextBuilder,
 ) -> Result<()> {
 	match d {
 		Destruct::Full(v) => {
-			let old = new_bindings.insert(v.clone(), parent);
-			if old.is_some() {
-				bail!(DuplicateLocalVar(v.clone()))
-			}
+			new_bindings.try_bind(v.clone(), parent)?;
 		}
 		#[cfg(feature = "exp-destruct")]
 		Destruct::Skip => {}
@@ -187,10 +180,10 @@
 	Ok(())
 }
 
-pub fn evaluate_dest<H: BuildHasher>(
+pub fn evaluate_dest(
 	d: &BindSpec,
 	fctx: Pending<Context>,
-	new_bindings: &mut HashMap<IStr, Thunk<Val>, H>,
+	new_bindings: &mut ContextBuilder,
 ) -> Result<()> {
 	match d {
 		BindSpec::Field { into, value } => {
@@ -210,13 +203,10 @@
 			let params = params.clone();
 			let name = name.clone();
 			let value = value.clone();
-			let old = new_bindings.insert(name.clone(), {
-				let name = name.clone();
-				Thunk!(move || Ok(evaluate_method(fctx.unwrap(), name, params, value)))
-			});
-			if old.is_some() {
-				bail!(DuplicateLocalVar(name))
-			}
+			new_bindings.try_bind(
+				name.clone(),
+				Thunk!(move || Ok(evaluate_method(fctx.unwrap(), name, params, value))),
+			)?;
 		}
 	}
 	Ok(())
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_ir::{6	ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprParams, FieldMember,7	FieldName, ForSpecData, IfSpecData, ImportKind, LiteralType, ObjBody, ObjMembers, Spanned,8	function::ParamName,9};10use jrsonnet_types::ValType;11use rustc_hash::FxHashMap;1213use self::destructure::destruct;14use crate::{15	Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, ResultExt,16	SupThis, Unbound, Val,17	arr::ArrValue,18	bail,19	destructure::evaluate_dest,20	error::{ErrorKind::*, suggest_object_fields},21	evaluate::operator::{evaluate_binary_op_special, evaluate_unary_op},22	function::{CallLocation, FuncDesc, FuncVal, PreparedFuncVal},23	gc::WithCapacityExt as _,24	in_frame,25	typed::{FromUntyped, IntoUntyped as _, Typed},26	val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},27	with_state,28};29pub mod destructure;30pub mod operator;3132// This is the amount of bytes that need to be left on the stack before increasing the size.33// It must be at least as large as the stack required by any code that does not call34// `ensure_sufficient_stack`.35const RED_ZONE: usize = 100 * 1024; // 100k3637// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then38// on. This flag has performance relevant characteristics. Don't set it too high.39const STACK_PER_RECURSION: usize = 1024 * 1024; // 1MB4041/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations42/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit43/// from this.44///45/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.46#[inline]47pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {48	stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)49}5051pub fn evaluate_trivial(expr: &Expr) -> Option<Val> {52	fn is_trivial(expr: &Expr) -> bool {53		match expr {54			Expr::Str(_)55			| Expr::Num(_)56			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,57			Expr::Arr(a) => a.iter().all(is_trivial),58			_ => false,59		}60	}61	Some(match expr {62		Expr::Str(s) => Val::string(s.clone()),63		Expr::Num(n) => {64			Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))65		}66		Expr::Literal(LiteralType::False) => Val::Bool(false),67		Expr::Literal(LiteralType::True) => Val::Bool(true),68		Expr::Literal(LiteralType::Null) => Val::Null,69		Expr::Arr(n) => {70			if n.iter().any(|e| !is_trivial(e)) {71				return None;72			}73			Val::Arr(74				n.iter()75					.map(evaluate_trivial)76					.map(|e| e.expect("checked trivial"))77					.collect(),78			)79		}80		_ => return None,81	})82}8384pub fn evaluate_method(ctx: Context, name: IStr, params: ExprParams, body: Rc<Expr>) -> Val {85	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {86		name,87		ctx,88		params,89		body,90	})))91}9293pub fn evaluate_field_name(ctx: Context, field_name: &Spanned<FieldName>) -> Result<Option<IStr>> {94	Ok(match &field_name.value {95		FieldName::Fixed(n) => Some(n.clone()),96		FieldName::Dyn(expr) => in_frame(97			CallLocation::new(&field_name.span),98			|| "evaluating field name".to_string(),99			|| {100				let v = evaluate(ctx, expr)?;101				Ok(if matches!(v, Val::Null) {102					None103				} else {104					Some(IStr::from_untyped(v)?)105				})106			},107		)?,108	})109}110111pub fn evaluate_comp(112	ctx: Context,113	specs: &[CompSpec],114	mut guaranteed_reserve: usize,115	callback: &mut impl FnMut(Context, usize) -> Result<()>,116) -> Result<()> {117	match specs.first() {118		None => callback(ctx, guaranteed_reserve)?,119		Some(CompSpec::IfSpec(IfSpecData { cond, span: _ })) => {120			if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {121				evaluate_comp(ctx, &specs[1..], 0, callback)?;122			}123		}124		Some(CompSpec::ForSpec(ForSpecData {125			destruct: into,126			over,127		})) => {128			match evaluate(ctx.clone(), over)? {129				Val::Arr(list) => {130					guaranteed_reserve = guaranteed_reserve.max(1) * list.len();131					for (i, item) in list.iter_lazy().enumerate() {132						let fctx = Pending::new();133						let mut new_bindings = FxHashMap::with_capacity(into.binds_len());134						destruct(into, item, fctx.clone(), &mut new_bindings)?;135						let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);136137						let specs = &specs[1..];138						evaluate_comp(139							ctx,140							specs,141							if i == 0 || !specs.is_empty() {142								guaranteed_reserve143							} else {144								0145							},146							callback,147						)?;148					}149				}150				#[cfg(feature = "exp-object-iteration")]151				Val::Obj(obj) => {152					let fields = obj.fields(153						// TODO: Should there be ability to preserve iteration order?154						#[cfg(feature = "exp-preserve-order")]155						false,156					);157					guaranteed_reserve = guaranteed_reserve.max(1) * fields.len();158					for field in fields {159						let fctx = Pending::new();160						let mut new_bindings = FxHashMap::with_capacity(into.binds_len());161						let obj = obj.clone();162						let value = Thunk::evaluated(Val::arr(vec![163							Thunk::evaluated(Val::string(field.clone())),164							obj.get_lazy(field).transpose().expect(165								"field exists, as field name was obtained from object.fields()",166							),167						]));168						destruct(into, value, fctx.clone(), &mut new_bindings)?;169						let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);170171						evaluate_comp(ctx, &specs[1..], callback)?;172					}173				}174				_ => bail!(InComprehensionCanOnlyIterateOverArray),175			}176		}177	}178	Ok(())179}180181fn evaluate_arr_comp(ctx: Context, expr: &Rc<Expr>, comp_specs: &[CompSpec]) -> Result<ArrValue> {182	'eager: {183		let mut out = Vec::new();184185		if evaluate_comp(ctx.clone(), comp_specs, 0, &mut |ctx, reserve| {186			if reserve != 0 {187				out.reserve(reserve);188			}189			out.push(evaluate(ctx, expr)?);190			Ok(())191		})192		.is_err()193		{194			break 'eager;195		}196197		return Ok(ArrValue::new(out));198	};199	let mut out = Vec::new();200	evaluate_comp(ctx, comp_specs, 0, &mut |ctx, reserve| {201		if reserve != 0 {202			out.reserve(reserve);203		}204		let expr = expr.clone();205		out.push(Thunk!(move || evaluate(ctx, &expr)));206		Ok(())207	})?;208	Ok(ArrValue::new(out))209}210211trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}212impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}213214fn evaluate_object_locals(215	fctx: Context,216	locals: Rc<Vec<BindSpec>>,217) -> impl CloneableUnbound<Context> {218	#[derive(Trace, Clone)]219	struct UnboundLocals {220		fctx: Context,221		locals: Rc<Vec<BindSpec>>,222	}223	impl Unbound for UnboundLocals {224		type Bound = Context;225226		fn bind(&self, sup_this: SupThis) -> Result<Context> {227			let fctx = Context::new_future();228			let mut new_bindings =229				FxHashMap::with_capacity(self.locals.iter().map(BindSpec::binds_len).sum());230			for b in self.locals.iter() {231				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;232			}233234			let ctx = self.fctx.clone();235236			let ctx = ctx237				.extend_bindings_sup_this(new_bindings, sup_this)238				.into_future(fctx);239240			Ok(ctx)241		}242	}243244	UnboundLocals { fctx, locals }245}246247pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(248	builder: &mut ObjValueBuilder,249	ctx: Context,250	uctx: B,251	field: &FieldMember,252) -> Result<()> {253	let name = evaluate_field_name(ctx, &field.name)?;254	let Some(name) = name else {255		return Ok(());256	};257258	match field {259		FieldMember {260			plus,261			params: None,262			visibility,263			value,264			..265		} => {266			#[derive(Trace)]267			struct UnboundValue<B: Trace> {268				uctx: B,269				value: Rc<Expr>,270				name: IStr,271			}272			impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {273				type Bound = Val;274				fn bind(&self, sup_this: SupThis) -> Result<Val> {275					evaluate_named(self.uctx.bind(sup_this)?, &self.value, self.name.clone())276				}277			}278279			builder280				.field(name.clone())281				.with_add(*plus)282				.with_visibility(*visibility)283				.with_location(field.name.span.clone())284				.bindable(UnboundValue {285					uctx,286					value: value.clone(),287					name,288				})?;289		}290		FieldMember {291			params: Some(params),292			visibility,293			value,294			..295		} => {296			#[derive(Trace)]297			struct UnboundMethod<B: Trace> {298				uctx: B,299				value: Rc<Expr>,300				params: ExprParams,301				name: IStr,302			}303			impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {304				type Bound = Val;305				fn bind(&self, sup_this: SupThis) -> Result<Val> {306					Ok(evaluate_method(307						self.uctx.bind(sup_this)?,308						self.name.clone(),309						self.params.clone(),310						self.value.clone(),311					))312				}313			}314315			builder316				.field(name.clone())317				.with_visibility(*visibility)318				// .with_location(value.span())319				.bindable(UnboundMethod {320					uctx,321					value: value.clone(),322					params: params.clone(),323					name,324				})?;325		}326	}327	Ok(())328}329330#[derive(Trace, Clone)]331struct DirectUnbound(Context);332impl Unbound for DirectUnbound {333	type Bound = Context;334	fn bind(&self, sup_this: SupThis) -> Result<Context> {335		Ok(self336			.0337			.clone()338			.extend_bindings_sup_this(FxHashMap::new(), sup_this))339	}340}341342#[allow(clippy::too_many_lines)]343pub fn evaluate_member_list_object(344	super_obj: Option<ObjValue>,345	ctx: Context,346	members: &ObjMembers,347) -> Result<ObjValue> {348	#[derive(Trace)]349	struct ObjectAssert<B: Trace> {350		uctx: B,351		asserts: Rc<Vec<AssertStmt>>,352	}353	impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {354		fn run(&self, sup_this: SupThis) -> Result<()> {355			let ctx = self.uctx.bind(sup_this)?;356			for assert in &*self.asserts {357				evaluate_assert(ctx.clone(), assert)?;358			}359			Ok(())360		}361	}362363	let mut builder = ObjValueBuilder::new();364	if let Some(super_obj) = super_obj {365		builder.with_super(super_obj);366	}367368	if members.locals.is_empty() {369		// We can use the same context for all field evaluation, it doesn't depends on locals, only on this/super370		let uctx = DirectUnbound(ctx.clone());371		for field in &members.fields {372			evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;373		}374		if !members.asserts.is_empty() {375			builder.assert(ObjectAssert {376				uctx,377				asserts: members.asserts.clone(),378			});379		}380	} else {381		let locals = members.locals.clone();382		// We have single context for all fields, so we can cache them together383		let uctx = CachedUnbound::new(evaluate_object_locals(ctx.clone(), locals));384		for field in &members.fields {385			evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;386		}387		if !members.asserts.is_empty() {388			builder.assert(ObjectAssert {389				uctx,390				asserts: members.asserts.clone(),391			});392		}393	}394395	Ok(builder.build())396}397398pub fn evaluate_object(399	super_obj: Option<ObjValue>,400	ctx: Context,401	object: &ObjBody,402) -> Result<ObjValue> {403	Ok(match object {404		ObjBody::MemberList(members) => evaluate_member_list_object(super_obj, ctx, members)?,405		ObjBody::ObjComp(obj) => {406			let mut builder = ObjValueBuilder::new();407			if let Some(super_obj) = super_obj {408				builder.with_super(super_obj);409			}410			let locals = obj.locals.clone();411			evaluate_comp(ctx, &obj.compspecs, 0, &mut |ctx, reserve| {412				let uctx = evaluate_object_locals(ctx.clone(), locals.clone());413				builder.reserve_fields(reserve);414415				evaluate_field_member(&mut builder, ctx, uctx, &obj.field)416			})?;417418			builder.build()419		}420	})421}422423pub fn evaluate_apply(424	ctx: Context,425	value: &Expr,426	args: &ArgsDesc,427	loc: CallLocation<'_>,428	tailstrict: bool,429) -> Result<Val> {430	let value = evaluate(ctx.clone(), value)?;431	Ok(match value {432		Val::Func(f) => {433			let name = f.name();434			let unnamed = args435				.unnamed436				.iter()437				.cloned()438				.map(|un| evaluate_thunk(ctx.clone(), un, tailstrict))439				.collect::<Result<Vec<_>>>()?;440			let named = args441				.values442				.iter()443				.cloned()444				.map(|un| evaluate_thunk(ctx.clone(), un, tailstrict))445				.collect::<Result<Vec<_>>>()?;446			let prepare = PreparedFuncVal::new(f, args.unnamed.len(), &args.names)447				.with_description_src(loc, || format!("function <{name}> call"))?;448			let body = || prepare.call(loc, &unnamed, &named);449			if tailstrict {450				body()?451			} else {452				in_frame(loc, || format!("function <{name}> call"), body)?453			}454		}455		v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),456	})457}458459pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {460	let value = &assertion.0;461	let msg = &assertion.1;462	let assertion_result = in_frame(463		CallLocation::new(&value.span),464		|| "assertion condition".to_owned(),465		|| bool::from_untyped(evaluate(ctx.clone(), value)?),466	)?;467	if !assertion_result {468		in_frame(469			CallLocation::new(&value.span),470			|| "assertion failure".to_owned(),471			|| {472				if let Some(msg) = msg {473					bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));474				}475				bail!(AssertionFailed(Val::Null.to_string()?));476			},477		)?;478	}479	Ok(())480}481482pub fn evaluate_named_param(ctx: Context, expr: &Expr, name: ParamName) -> Result<Val> {483	match name {484		ParamName::Named(name) => evaluate_named(ctx, expr, name),485		ParamName::Unnamed => evaluate(ctx, expr),486	}487}488489pub fn evaluate_named(ctx: Context, expr: &Expr, name: IStr) -> Result<Val> {490	use Expr::*;491	Ok(match expr {492		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),493		_ => evaluate(ctx, expr)?,494	})495}496497pub fn evaluate_thunk(ctx: Context, expr: Rc<Expr>, tailstrict: bool) -> Result<Thunk<Val>> {498	Ok(if tailstrict {499		Thunk::evaluated(evaluate(ctx, &expr)?)500	} else {501		Thunk!(move || { evaluate(ctx, &expr) })502	})503}504#[allow(clippy::too_many_lines)]505pub fn evaluate(ctx: Context, expr: &Expr) -> Result<Val> {506	use Expr::*;507508	Ok(match expr {509		Literal(LiteralType::This) => Val::Obj(ctx.try_this()?),510		Literal(LiteralType::Super) => Val::Obj(ctx.try_sup_this()?.standalone_super()?),511		Literal(LiteralType::Dollar) => Val::Obj(ctx.try_dollar()?),512		Literal(LiteralType::True) => Val::Bool(true),513		Literal(LiteralType::False) => Val::Bool(false),514		Literal(LiteralType::Null) => Val::Null,515		Str(v) => Val::string(v.clone()),516		Num(v) => Val::try_num(*v)?,517		// I have tried to remove special behavior from super by implementing standalone-super518		// expresion, but looks like this case still needs special treatment.519		//520		// Note that other jsonnet implementations will fail on `if value in (super)` expression,521		// because the standalone super literal is not supported, that is because in other522		// implementations `in super` treated differently from `in smth_else`.523		BinaryOp(bin)524			if matches!(&bin.rhs, Expr::Literal(LiteralType::Super))525				&& bin.op == BinaryOpType::In =>526		{527			let sup_this = ctx.try_sup_this()?;528			// In jsonnet, "field" in e is eager, LHS expression is always executed regardless of super existence.529			// In jrsonnet, however, this wasn't true, this was kept here for compatibility.530			if !sup_this.has_super() {531				return Ok(Val::Bool(false));532			}533			let field = evaluate(ctx, &bin.lhs)?;534			Val::Bool(sup_this.field_in_super(field.to_string()?))535		}536		BinaryOp(bin) => evaluate_binary_op_special(ctx, &bin.lhs, bin.op, &bin.rhs)?,537		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,538		Var(name) => in_frame(539			CallLocation::new(&name.span),540			|| format!("local <{}> access", &**name),541			|| ctx.binding((**name).clone())?.evaluate(),542		)?,543		Index { indexable, parts } => ensure_sufficient_stack(|| {544			let mut parts = parts.iter();545			let mut indexable = if matches!(&**indexable, Expr::Literal(LiteralType::Super)) {546				let part = parts.next().expect("at least part should exist");547				// sup_this existence check might also be skipped here for null-coalesce...548				// But I believe this might cause errors.549				let sup_this = ctx.try_sup_this()?;550				if !sup_this.has_super() {551					#[cfg(feature = "exp-null-coaelse")]552					if part.null_coaelse {553						return Ok(Val::Null);554					}555					bail!(NoSuperFound)556				}557				let name = evaluate(ctx.clone(), &part.value)?;558559				let Val::Str(name) = name else {560					bail!(ValueIndexMustBeTypeGot(561						ValType::Obj,562						ValType::Str,563						name.value_type(),564					))565				};566567				let name = name.into_flat();568				match sup_this569					.get_super(name.clone())570					.with_description_src(&part.span, || format!("field <{name}> access"))?571				{572					Some(v) => v,573					#[cfg(feature = "exp-null-coaelse")]574					None if part.null_coaelse => return Ok(Val::Null),575					None => {576						let suggestions = suggest_object_fields(577							&sup_this.standalone_super().expect("super exists"),578							name.clone(),579						);580581						bail!(NoSuchField(name, suggestions))582					}583				}584			} else {585				evaluate(ctx.clone(), indexable)?586			};587588			for part in parts {589				indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {590					(Val::Obj(v), Val::Str(key)) => match v591						.get(key.clone().into_flat())592						.with_description_src(&part.span, || format!("field <{key}> access"))?593					{594						Some(v) => v,595						#[cfg(feature = "exp-null-coaelse")]596						None if part.null_coaelse => return Ok(Val::Null),597						None => {598							let suggestions = suggest_object_fields(&v, key.into_flat());599600							return Err(Error::from(NoSuchField(601								key.clone().into_flat(),602								suggestions,603							)))604							.with_description_src(&part.span, || format!("field <{key}> access"));605						}606					},607					(Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(608						ValType::Obj,609						ValType::Str,610						n.value_type(),611					)),612					(Val::Arr(v), Val::Num(n)) => {613						let n = n.get();614						if n.fract() > f64::EPSILON {615							bail!(FractionalIndex)616						}617						if n < 0.0 {618							#[expect(619								clippy::cast_possible_truncation,620								reason = "it would be truncated anyway"621							)]622							let n = n as isize;623							bail!(ArrayBoundsError(n, v.len()));624						}625						#[expect(626							clippy::cast_possible_truncation,627							clippy::cast_sign_loss,628							reason = "n is checked postive"629						)]630						v.get(n as usize)?631							.ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?632					}633					(Val::Arr(_), Val::Str(n)) => {634						bail!(AttemptedIndexAnArrayWithString(n.into_flat()))635					}636					(Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(637						ValType::Arr,638						ValType::Num,639						n.value_type(),640					)),641642					(Val::Str(s), Val::Num(n)) => Val::Str({643						let n = n.get();644						if n.fract() > f64::EPSILON {645							bail!(FractionalIndex)646						}647						if n < 0.0 {648							#[expect(649								clippy::cast_possible_truncation,650								reason = "it would be truncated anyway"651							)]652							let n = n as isize;653							bail!(ArrayBoundsError(n, s.into_flat().chars().count()));654						}655						#[expect(656							clippy::cast_sign_loss,657							clippy::cast_possible_truncation,658							reason = "n is positive, overflow will truncate as expected"659						)]660						let n = n as usize;661						let v: IStr = s662							.clone()663							.into_flat()664							.chars()665							.skip(n)666							.take(1)667							.collect::<String>()668							.into();669						if v.is_empty() {670							bail!(StringBoundsError(n, s.into_flat().chars().count()))671						}672						StrValue::Flat(v)673					}),674					(Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(675						ValType::Str,676						ValType::Num,677						n.value_type(),678					)),679					#[cfg(feature = "exp-null-coaelse")]680					(Val::Null, _) if part.null_coaelse => return Ok(Val::Null),681					(v, _) => bail!(CantIndexInto(v.value_type())),682				};683			}684			Ok(indexable)685		})?,686		LocalExpr(bindings, returned) => {687			let mut new_bindings: FxHashMap<IStr, Thunk<Val>> =688				FxHashMap::with_capacity(bindings.iter().map(BindSpec::binds_len).sum());689			let fctx = Context::new_future();690			for b in bindings {691				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;692			}693			let ctx = ctx.extend_bindings(new_bindings).into_future(fctx);694			evaluate(ctx, returned)?695		}696		Arr(items) => {697			if items.is_empty() {698				Val::arr(())699			} else {700				Val::Arr(ArrValue::expr(ctx, items.clone()))701			}702		}703		ArrComp(expr, comp_specs) => Val::Arr(evaluate_arr_comp(ctx, expr, comp_specs)?),704		Obj(body) => Val::Obj(evaluate_object(None, ctx, body)?),705		ObjExtend(a, b) => {706			let base = evaluate(ctx.clone(), a)?;707			match base {708				Val::Obj(base_obj) => Val::Obj(evaluate_object(Some(base_obj), ctx, b)?),709				_ => bail!("ObjExtend lhs should be an object value"),710			}711		}712		Apply(value, args, tailstrict) => ensure_sufficient_stack(|| {713			evaluate_apply(ctx, value, args, CallLocation::new(&args.span), *tailstrict)714		})?,715		Function(params, body) => {716			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())717		}718		AssertExpr(assert) => {719			evaluate_assert(ctx.clone(), &assert.assert)?;720			evaluate(ctx, &assert.rest)?721		}722		ErrorStmt(s, e) => in_frame(723			CallLocation::new(s),724			|| "error statement".to_owned(),725			|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),726		)?,727		IfElse(if_else) => {728			if in_frame(729				CallLocation::new(&if_else.cond.span),730				|| "if condition".to_owned(),731				|| bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.cond)?),732			)? {733				evaluate(ctx, &if_else.cond_then)?734			} else {735				match &if_else.cond_else {736					Some(v) => evaluate(ctx, v)?,737					None => Val::Null,738				}739			}740		}741		Slice(slice) => {742			fn parse_idx<T: Typed + FromUntyped>(743				ctx: Context,744				expr: Option<&Spanned<Expr>>,745				desc: &'static str,746			) -> Result<Option<T>> {747				if let Some(value) = expr {748					Ok(in_frame(749						CallLocation::new(&value.span),750						|| format!("slice {desc}"),751						|| <Option<T>>::from_untyped(evaluate(ctx, value)?),752					)?)753				} else {754					Ok(None)755				}756			}757758			let indexable = evaluate(ctx.clone(), &slice.value)?;759760			let start = parse_idx(ctx.clone(), slice.slice.start.as_ref(), "start")?;761			let end = parse_idx(ctx.clone(), slice.slice.end.as_ref(), "end")?;762			let step = parse_idx(ctx, slice.slice.step.as_ref(), "step")?;763764			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?765		}766		Import(kind, path) => {767			let Expr::Str(path) = &**path else {768				bail!("computed imports are not supported")769			};770			with_state(|s| {771				let span = &kind.span;772				let resolved_path = s.resolve_from(span.0.source_path(), path)?;773				Ok(match &**kind {774					ImportKind::Normal => in_frame(775						CallLocation::new(span),776						|| format!("import {:?}", path.clone()),777						|| s.import_resolved(resolved_path),778					)?,779					ImportKind::Str => Val::string(s.import_resolved_str(resolved_path)?),780					ImportKind::Bin => Val::arr(s.import_resolved_bin(resolved_path)?),781				}) as Result<Val>782			})?783		}784	})785}
after · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_ir::{6	ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprParams, FieldMember,7	FieldName, ForSpecData, IfSpecData, ImportKind, LiteralType, ObjBody, ObjMembers, Spanned,8	function::ParamName,9};10use jrsonnet_types::ValType;1112use self::destructure::destruct;13use crate::{14	Context, ContextBuilder, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,15	ResultExt, SupThis, Unbound, Val,16	arr::ArrValue,17	bail,18	destructure::evaluate_dest,19	error::{ErrorKind::*, suggest_object_fields},20	evaluate::operator::{evaluate_binary_op_special, evaluate_unary_op},21	function::{CallLocation, FuncDesc, FuncVal, PreparedFuncVal},22	in_frame,23	typed::{FromUntyped, IntoUntyped as _, Typed},24	val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},25	with_state,26};27pub mod destructure;28pub mod operator;2930// This is the amount of bytes that need to be left on the stack before increasing the size.31// It must be at least as large as the stack required by any code that does not call32// `ensure_sufficient_stack`.33const RED_ZONE: usize = 100 * 1024; // 100k3435// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then36// on. This flag has performance relevant characteristics. Don't set it too high.37const STACK_PER_RECURSION: usize = 1024 * 1024; // 1MB3839/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations40/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit41/// from this.42///43/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.44#[inline]45pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {46	stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)47}4849pub fn evaluate_trivial(expr: &Expr) -> Option<Val> {50	fn is_trivial(expr: &Expr) -> bool {51		match expr {52			Expr::Str(_)53			| Expr::Num(_)54			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,55			Expr::Arr(a) => a.iter().all(is_trivial),56			_ => false,57		}58	}59	Some(match expr {60		Expr::Str(s) => Val::string(s.clone()),61		Expr::Num(n) => {62			Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))63		}64		Expr::Literal(LiteralType::False) => Val::Bool(false),65		Expr::Literal(LiteralType::True) => Val::Bool(true),66		Expr::Literal(LiteralType::Null) => Val::Null,67		Expr::Arr(n) => {68			if n.iter().any(|e| !is_trivial(e)) {69				return None;70			}71			Val::Arr(72				n.iter()73					.map(evaluate_trivial)74					.map(|e| e.expect("checked trivial"))75					.collect(),76			)77		}78		_ => return None,79	})80}8182pub fn evaluate_method(ctx: Context, name: IStr, params: ExprParams, body: Rc<Expr>) -> Val {83	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {84		name,85		ctx,86		params,87		body,88	})))89}9091pub fn evaluate_field_name(ctx: Context, field_name: &Spanned<FieldName>) -> Result<Option<IStr>> {92	Ok(match &field_name.value {93		FieldName::Fixed(n) => Some(n.clone()),94		FieldName::Dyn(expr) => in_frame(95			CallLocation::new(&field_name.span),96			|| "evaluating field name".to_string(),97			|| {98				let v = evaluate(ctx, expr)?;99				Ok(if matches!(v, Val::Null) {100					None101				} else {102					Some(IStr::from_untyped(v)?)103				})104			},105		)?,106	})107}108109pub fn evaluate_comp(110	ctx: Context,111	specs: &[CompSpec],112	mut guaranteed_reserve: usize,113	callback: &mut impl FnMut(Context, usize) -> Result<()>,114) -> Result<()> {115	match specs.first() {116		None => callback(ctx, guaranteed_reserve)?,117		Some(CompSpec::IfSpec(IfSpecData { cond, span: _ })) => {118			if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {119				evaluate_comp(ctx, &specs[1..], 0, callback)?;120			}121		}122		Some(CompSpec::ForSpec(ForSpecData {123			destruct: into,124			over,125		})) => {126			match evaluate(ctx.clone(), over)? {127				Val::Arr(list) => {128					guaranteed_reserve = guaranteed_reserve.max(1) * list.len();129					for (i, item) in list.iter_lazy().enumerate() {130						let fctx = Pending::new();131						let mut ctx = ContextBuilder::extend_fast(ctx.clone());132						destruct(into, item, fctx.clone(), &mut ctx)?;133						let ctx = ctx.build().into_future(fctx);134135						let specs = &specs[1..];136						evaluate_comp(137							ctx,138							specs,139							if i == 0 || !specs.is_empty() {140								guaranteed_reserve141							} else {142								0143							},144							callback,145						)?;146					}147				}148				#[cfg(feature = "exp-object-iteration")]149				Val::Obj(obj) => {150					let fields = obj.fields(151						// TODO: Should there be ability to preserve iteration order?152						#[cfg(feature = "exp-preserve-order")]153						false,154					);155					guaranteed_reserve = guaranteed_reserve.max(1) * fields.len();156					for field in fields {157						let fctx = Pending::new();158						let mut new_bindings = FxHashMap::with_capacity(into.binds_len());159						let obj = obj.clone();160						let value = Thunk::evaluated(Val::arr(vec![161							Thunk::evaluated(Val::string(field.clone())),162							obj.get_lazy(field).transpose().expect(163								"field exists, as field name was obtained from object.fields()",164							),165						]));166						destruct(into, value, fctx.clone(), &mut new_bindings)?;167						let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);168169						evaluate_comp(ctx, &specs[1..], callback)?;170					}171				}172				_ => bail!(InComprehensionCanOnlyIterateOverArray),173			}174		}175	}176	Ok(())177}178179fn evaluate_arr_comp(ctx: Context, expr: &Rc<Expr>, comp_specs: &[CompSpec]) -> Result<ArrValue> {180	let ctx = ctx.branch_point();181	'eager: {182		let mut out = Vec::new();183184		if evaluate_comp(ctx.clone(), comp_specs, 0, &mut |ctx, reserve| {185			if reserve != 0 {186				out.reserve(reserve);187			}188			out.push(evaluate(ctx, expr)?);189			Ok(())190		})191		.is_err()192		{193			break 'eager;194		}195196		return Ok(ArrValue::new(out));197	};198	let mut out = Vec::new();199	evaluate_comp(ctx, comp_specs, 0, &mut |ctx, reserve| {200		if reserve != 0 {201			out.reserve(reserve);202		}203		let expr = expr.clone();204		out.push(Thunk!(move || evaluate(ctx, &expr)));205		Ok(())206	})?;207	Ok(ArrValue::new(out))208}209210trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}211impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}212213fn evaluate_object_locals(214	fctx: Context,215	locals: Rc<Vec<BindSpec>>,216) -> impl CloneableUnbound<Context> {217	#[derive(Trace, Clone)]218	struct UnboundLocals {219		fctx: Context,220		locals: Rc<Vec<BindSpec>>,221	}222	impl Unbound for UnboundLocals {223		type Bound = Context;224225		fn bind(&self, sup_this: SupThis) -> Result<Context> {226			let fctx = Context::new_future();227			let ctx = self.fctx.clone();228			let mut ctx = ContextBuilder::extend(ctx);229			for b in self.locals.iter() {230				evaluate_dest(b, fctx.clone(), &mut ctx)?;231			}232233			let ctx = ctx.build_sup_this(sup_this).into_future(fctx);234235			Ok(ctx)236		}237	}238239	UnboundLocals { fctx, locals }240}241242pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(243	builder: &mut ObjValueBuilder,244	ctx: Context,245	uctx: B,246	field: &FieldMember,247) -> Result<()> {248	let name = evaluate_field_name(ctx, &field.name)?;249	let Some(name) = name else {250		return Ok(());251	};252253	match field {254		FieldMember {255			plus,256			params: None,257			visibility,258			value,259			..260		} => {261			#[derive(Trace)]262			struct UnboundValue<B: Trace> {263				uctx: B,264				value: Rc<Expr>,265				name: IStr,266			}267			impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {268				type Bound = Val;269				fn bind(&self, sup_this: SupThis) -> Result<Val> {270					evaluate_named(self.uctx.bind(sup_this)?, &self.value, self.name.clone())271				}272			}273274			builder275				.field(name.clone())276				.with_add(*plus)277				.with_visibility(*visibility)278				.with_location(field.name.span.clone())279				.bindable(UnboundValue {280					uctx,281					value: value.clone(),282					name,283				})?;284		}285		FieldMember {286			params: Some(params),287			visibility,288			value,289			..290		} => {291			#[derive(Trace)]292			struct UnboundMethod<B: Trace> {293				uctx: B,294				value: Rc<Expr>,295				params: ExprParams,296				name: IStr,297			}298			impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {299				type Bound = Val;300				fn bind(&self, sup_this: SupThis) -> Result<Val> {301					Ok(evaluate_method(302						self.uctx.bind(sup_this)?,303						self.name.clone(),304						self.params.clone(),305						self.value.clone(),306					))307				}308			}309310			builder311				.field(name.clone())312				.with_visibility(*visibility)313				// .with_location(value.span())314				.bindable(UnboundMethod {315					uctx,316					value: value.clone(),317					params: params.clone(),318					name,319				})?;320		}321	}322	Ok(())323}324325#[derive(Trace, Clone)]326struct DirectUnbound(Context);327impl Unbound for DirectUnbound {328	type Bound = Context;329	fn bind(&self, sup_this: SupThis) -> Result<Context> {330		Ok(ContextBuilder::extend(self.0.clone()).build_sup_this(sup_this))331	}332}333334#[allow(clippy::too_many_lines)]335pub fn evaluate_member_list_object(336	super_obj: Option<ObjValue>,337	ctx: Context,338	members: &ObjMembers,339) -> Result<ObjValue> {340	#[derive(Trace)]341	struct ObjectAssert<B: Trace> {342		uctx: B,343		asserts: Rc<Vec<AssertStmt>>,344	}345	impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {346		fn run(&self, sup_this: SupThis) -> Result<()> {347			let ctx = self.uctx.bind(sup_this)?;348			for assert in &*self.asserts {349				evaluate_assert(ctx.clone(), assert)?;350			}351			Ok(())352		}353	}354355	let mut builder = ObjValueBuilder::new();356	if let Some(super_obj) = super_obj {357		builder.with_super(super_obj);358	}359360	if members.locals.is_empty() {361		// We can use the same context for all field evaluation, it doesn't depends on locals, only on this/super362		let uctx = DirectUnbound(ctx.clone());363		for field in &members.fields {364			evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;365		}366		if !members.asserts.is_empty() {367			builder.assert(ObjectAssert {368				uctx,369				asserts: members.asserts.clone(),370			});371		}372	} else {373		let locals = members.locals.clone();374		// We have single context for all fields, so we can cache them together375		let uctx = CachedUnbound::new(evaluate_object_locals(ctx.clone(), locals));376		for field in &members.fields {377			evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;378		}379		if !members.asserts.is_empty() {380			builder.assert(ObjectAssert {381				uctx,382				asserts: members.asserts.clone(),383			});384		}385	}386387	Ok(builder.build())388}389390pub fn evaluate_object(391	super_obj: Option<ObjValue>,392	ctx: Context,393	object: &ObjBody,394) -> Result<ObjValue> {395	Ok(match object {396		ObjBody::MemberList(members) => evaluate_member_list_object(super_obj, ctx, members)?,397		ObjBody::ObjComp(obj) => {398			let mut builder = ObjValueBuilder::new();399			if let Some(super_obj) = super_obj {400				builder.with_super(super_obj);401			}402			let locals = obj.locals.clone();403			evaluate_comp(404				ctx.branch_point(),405				&obj.compspecs,406				0,407				&mut |ctx, reserve| {408					let uctx = evaluate_object_locals(ctx.clone(), locals.clone());409					builder.reserve_fields(reserve);410411					evaluate_field_member(&mut builder, ctx, uctx, &obj.field)412				},413			)?;414415			builder.build()416		}417	})418}419420pub fn evaluate_apply(421	ctx: Context,422	value: &Expr,423	args: &ArgsDesc,424	loc: CallLocation<'_>,425	tailstrict: bool,426) -> Result<Val> {427	let value = evaluate(ctx.clone(), value)?;428	Ok(match value {429		Val::Func(f) => {430			let name = f.name();431			let unnamed = args432				.unnamed433				.iter()434				.cloned()435				.map(|un| evaluate_thunk(ctx.clone(), un, tailstrict))436				.collect::<Result<Vec<_>>>()?;437			let named = args438				.values439				.iter()440				.cloned()441				.map(|un| evaluate_thunk(ctx.clone(), un, tailstrict))442				.collect::<Result<Vec<_>>>()?;443			let prepare = PreparedFuncVal::new(f, args.unnamed.len(), &args.names)444				.with_description_src(loc, || format!("function <{name}> call"))?;445			let body = || prepare.call(loc, &unnamed, &named);446			if tailstrict {447				body()?448			} else {449				in_frame(loc, || format!("function <{name}> call"), body)?450			}451		}452		v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),453	})454}455456pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {457	let value = &assertion.0;458	let msg = &assertion.1;459	let assertion_result = in_frame(460		CallLocation::new(&value.span),461		|| "assertion condition".to_owned(),462		|| bool::from_untyped(evaluate(ctx.clone(), value)?),463	)?;464	if !assertion_result {465		in_frame(466			CallLocation::new(&value.span),467			|| "assertion failure".to_owned(),468			|| {469				if let Some(msg) = msg {470					bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));471				}472				bail!(AssertionFailed(Val::Null.to_string()?));473			},474		)?;475	}476	Ok(())477}478479pub fn evaluate_named_param(ctx: Context, expr: &Expr, name: ParamName) -> Result<Val> {480	match name {481		ParamName::Named(name) => evaluate_named(ctx, expr, name),482		ParamName::Unnamed => evaluate(ctx, expr),483	}484}485486pub fn evaluate_named(ctx: Context, expr: &Expr, name: IStr) -> Result<Val> {487	use Expr::*;488	Ok(match expr {489		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),490		_ => evaluate(ctx, expr)?,491	})492}493494pub fn evaluate_thunk(ctx: Context, expr: Rc<Expr>, tailstrict: bool) -> Result<Thunk<Val>> {495	Ok(if tailstrict {496		Thunk::evaluated(evaluate(ctx, &expr)?)497	} else {498		Thunk!(move || { evaluate(ctx, &expr) })499	})500}501#[allow(clippy::too_many_lines)]502pub fn evaluate(ctx: Context, expr: &Expr) -> Result<Val> {503	use Expr::*;504505	Ok(match expr {506		Literal(LiteralType::This) => Val::Obj(ctx.try_this()?),507		Literal(LiteralType::Super) => Val::Obj(ctx.try_sup_this()?.standalone_super()?),508		Literal(LiteralType::Dollar) => Val::Obj(ctx.try_dollar()?),509		Literal(LiteralType::True) => Val::Bool(true),510		Literal(LiteralType::False) => Val::Bool(false),511		Literal(LiteralType::Null) => Val::Null,512		Str(v) => Val::string(v.clone()),513		Num(v) => Val::try_num(*v)?,514		// I have tried to remove special behavior from super by implementing standalone-super515		// expresion, but looks like this case still needs special treatment.516		//517		// Note that other jsonnet implementations will fail on `if value in (super)` expression,518		// because the standalone super literal is not supported, that is because in other519		// implementations `in super` treated differently from `in smth_else`.520		BinaryOp(bin)521			if matches!(&bin.rhs, Expr::Literal(LiteralType::Super))522				&& bin.op == BinaryOpType::In =>523		{524			let sup_this = ctx.try_sup_this()?;525			// In jsonnet, "field" in e is eager, LHS expression is always executed regardless of super existence.526			// In jrsonnet, however, this wasn't true, this was kept here for compatibility.527			if !sup_this.has_super() {528				return Ok(Val::Bool(false));529			}530			let field = evaluate(ctx, &bin.lhs)?;531			Val::Bool(sup_this.field_in_super(field.to_string()?))532		}533		BinaryOp(bin) => evaluate_binary_op_special(ctx, &bin.lhs, bin.op, &bin.rhs)?,534		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,535		Var(name) => in_frame(536			CallLocation::new(&name.span),537			|| format!("local <{}> access", &**name),538			|| ctx.binding((**name).clone())?.evaluate(),539		)?,540		Index { indexable, parts } => ensure_sufficient_stack(|| {541			let mut parts = parts.iter();542			let mut indexable = if matches!(&**indexable, Expr::Literal(LiteralType::Super)) {543				let part = parts.next().expect("at least part should exist");544				// sup_this existence check might also be skipped here for null-coalesce...545				// But I believe this might cause errors.546				let sup_this = ctx.try_sup_this()?;547				if !sup_this.has_super() {548					#[cfg(feature = "exp-null-coaelse")]549					if part.null_coaelse {550						return Ok(Val::Null);551					}552					bail!(NoSuperFound)553				}554				let name = evaluate(ctx.clone(), &part.value)?;555556				let Val::Str(name) = name else {557					bail!(ValueIndexMustBeTypeGot(558						ValType::Obj,559						ValType::Str,560						name.value_type(),561					))562				};563564				let name = name.into_flat();565				match sup_this566					.get_super(name.clone())567					.with_description_src(&part.span, || format!("field <{name}> access"))?568				{569					Some(v) => v,570					#[cfg(feature = "exp-null-coaelse")]571					None if part.null_coaelse => return Ok(Val::Null),572					None => {573						let suggestions = suggest_object_fields(574							&sup_this.standalone_super().expect("super exists"),575							name.clone(),576						);577578						bail!(NoSuchField(name, suggestions))579					}580				}581			} else {582				evaluate(ctx.clone(), indexable)?583			};584585			for part in parts {586				indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {587					(Val::Obj(v), Val::Str(key)) => match v588						.get(key.clone().into_flat())589						.with_description_src(&part.span, || format!("field <{key}> access"))?590					{591						Some(v) => v,592						#[cfg(feature = "exp-null-coaelse")]593						None if part.null_coaelse => return Ok(Val::Null),594						None => {595							let suggestions = suggest_object_fields(&v, key.into_flat());596597							return Err(Error::from(NoSuchField(598								key.clone().into_flat(),599								suggestions,600							)))601							.with_description_src(&part.span, || format!("field <{key}> access"));602						}603					},604					(Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(605						ValType::Obj,606						ValType::Str,607						n.value_type(),608					)),609					(Val::Arr(v), Val::Num(n)) => {610						let n = n.get();611						if n.fract() > f64::EPSILON {612							bail!(FractionalIndex)613						}614						if n < 0.0 {615							#[expect(616								clippy::cast_possible_truncation,617								reason = "it would be truncated anyway"618							)]619							let n = n as isize;620							bail!(ArrayBoundsError(n, v.len()));621						}622						#[expect(623							clippy::cast_possible_truncation,624							clippy::cast_sign_loss,625							reason = "n is checked postive"626						)]627						v.get(n as usize)?628							.ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?629					}630					(Val::Arr(_), Val::Str(n)) => {631						bail!(AttemptedIndexAnArrayWithString(n.into_flat()))632					}633					(Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(634						ValType::Arr,635						ValType::Num,636						n.value_type(),637					)),638639					(Val::Str(s), Val::Num(n)) => Val::Str({640						let n = n.get();641						if n.fract() > f64::EPSILON {642							bail!(FractionalIndex)643						}644						if n < 0.0 {645							#[expect(646								clippy::cast_possible_truncation,647								reason = "it would be truncated anyway"648							)]649							let n = n as isize;650							bail!(ArrayBoundsError(n, s.into_flat().chars().count()));651						}652						#[expect(653							clippy::cast_sign_loss,654							clippy::cast_possible_truncation,655							reason = "n is positive, overflow will truncate as expected"656						)]657						let n = n as usize;658						let v: IStr = s659							.clone()660							.into_flat()661							.chars()662							.skip(n)663							.take(1)664							.collect::<String>()665							.into();666						if v.is_empty() {667							bail!(StringBoundsError(n, s.into_flat().chars().count()))668						}669						StrValue::Flat(v)670					}),671					(Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(672						ValType::Str,673						ValType::Num,674						n.value_type(),675					)),676					#[cfg(feature = "exp-null-coaelse")]677					(Val::Null, _) if part.null_coaelse => return Ok(Val::Null),678					(v, _) => bail!(CantIndexInto(v.value_type())),679				};680			}681			Ok(indexable)682		})?,683		LocalExpr(bindings, returned) => {684			let fctx = Context::new_future();685			let mut ctx = ContextBuilder::extend(ctx);686			for b in bindings {687				evaluate_dest(b, fctx.clone(), &mut ctx)?;688			}689			let ctx = ctx.build().into_future(fctx);690			evaluate(ctx, returned)?691		}692		Arr(items) => {693			if items.is_empty() {694				Val::arr(())695			} else {696				Val::Arr(ArrValue::expr(ctx, items.clone()))697			}698		}699		ArrComp(expr, comp_specs) => Val::Arr(evaluate_arr_comp(ctx, expr, comp_specs)?),700		Obj(body) => Val::Obj(evaluate_object(None, ctx, body)?),701		ObjExtend(a, b) => {702			let base = evaluate(ctx.clone(), a)?;703			match base {704				Val::Obj(base_obj) => Val::Obj(evaluate_object(Some(base_obj), ctx, b)?),705				_ => bail!("ObjExtend lhs should be an object value"),706			}707		}708		Apply(value, args, tailstrict) => ensure_sufficient_stack(|| {709			evaluate_apply(ctx, value, args, CallLocation::new(&args.span), *tailstrict)710		})?,711		Function(params, body) => {712			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())713		}714		AssertExpr(assert) => {715			evaluate_assert(ctx.clone(), &assert.assert)?;716			evaluate(ctx, &assert.rest)?717		}718		ErrorStmt(s, e) => in_frame(719			CallLocation::new(s),720			|| "error statement".to_owned(),721			|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),722		)?,723		IfElse(if_else) => {724			if in_frame(725				CallLocation::new(&if_else.cond.span),726				|| "if condition".to_owned(),727				|| bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.cond)?),728			)? {729				evaluate(ctx, &if_else.cond_then)?730			} else {731				match &if_else.cond_else {732					Some(v) => evaluate(ctx, v)?,733					None => Val::Null,734				}735			}736		}737		Slice(slice) => {738			fn parse_idx<T: Typed + FromUntyped>(739				ctx: Context,740				expr: Option<&Spanned<Expr>>,741				desc: &'static str,742			) -> Result<Option<T>> {743				if let Some(value) = expr {744					Ok(in_frame(745						CallLocation::new(&value.span),746						|| format!("slice {desc}"),747						|| <Option<T>>::from_untyped(evaluate(ctx, value)?),748					)?)749				} else {750					Ok(None)751				}752			}753754			let indexable = evaluate(ctx.clone(), &slice.value)?;755756			let start = parse_idx(ctx.clone(), slice.slice.start.as_ref(), "start")?;757			let end = parse_idx(ctx.clone(), slice.slice.end.as_ref(), "end")?;758			let step = parse_idx(ctx, slice.slice.step.as_ref(), "step")?;759760			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?761		}762		Import(kind, path) => {763			let Expr::Str(path) = &**path else {764				bail!("computed imports are not supported")765			};766			with_state(|s| {767				let span = &kind.span;768				let resolved_path = s.resolve_from(span.0.source_path(), path)?;769				Ok(match &**kind {770					ImportKind::Normal => in_frame(771						CallLocation::new(span),772						|| format!("import {:?}", path.clone()),773						|| s.import_resolved(resolved_path),774					)?,775					ImportKind::Str => Val::string(s.import_resolved_str(resolved_path)?),776					ImportKind::Bin => Val::arr(s.import_resolved_bin(resolved_path)?),777				}) as Result<Val>778			})?779		}780	})781}
modifiedcrates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -1,12 +1,10 @@
 use jrsonnet_ir::ExprParams;
-use rustc_hash::FxHashMap;
 
 use crate::{
-	Context, Thunk,
+	Context, ContextBuilder, Thunk,
 	destructure::destruct,
 	error::{ErrorKind::*, Result},
 	evaluate_named_param,
-	gc::WithCapacityExt as _,
 };
 
 /// Creates Context, which has all argument default values applied
@@ -14,7 +12,7 @@
 pub fn parse_default_function_call(body_ctx: Context, params: &ExprParams) -> Result<Context> {
 	let fctx = Context::new_future();
 
-	let mut bindings = FxHashMap::with_capacity(params.binds_len());
+	let mut ctx = ContextBuilder::extend(body_ctx);
 
 	for param in params.exprs.iter() {
 		if let Some(v) = &param.default {
@@ -27,7 +25,7 @@
 					Thunk!(move || evaluate_named_param(ctx.unwrap(), &value, name))
 				},
 				fctx.clone(),
-				&mut bindings,
+				&mut ctx,
 			)?;
 		} else {
 			destruct(
@@ -42,10 +40,10 @@
 					.into()))
 				},
 				fctx.clone(),
-				&mut bindings,
+				&mut ctx,
 			)?;
 		}
 	}
 
-	Ok(body_ctx.extend_bindings(bindings).into_future(fctx))
+	Ok(ctx.build().into_future(fctx))
 }
modifiedcrates/jrsonnet-evaluator/src/function/prepared.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/prepared.rs
+++ b/crates/jrsonnet-evaluator/src/function/prepared.rs
@@ -2,12 +2,12 @@
 
 use jrsonnet_gcmodule::{Acyclic, Trace};
 use jrsonnet_ir::{ExprParams, IStr, function::FunctionSignature};
-use rustc_hash::{FxHashMap, FxHashSet};
+use rustc_hash::FxHashSet;
 
 use super::{CallLocation, FuncVal};
 use crate::{
 	Context, ContextBuilder, Pending, Result, Thunk, Val, bail, destructure::destruct,
-	error::ErrorKind::*, evaluate_named_param, gc::WithCapacityExt,
+	error::ErrorKind::*, evaluate_named_param,
 };
 
 #[derive(Debug, Trace, Clone)]
@@ -118,7 +118,7 @@
 	unnamed: &[Thunk<Val>],
 	named: &[Thunk<Val>],
 ) -> Result<Context> {
-	let mut passed_args = FxHashMap::with_capacity(params.binds_len());
+	let mut ctx = ContextBuilder::extend(body_ctx);
 
 	let destruct_ctx = Pending::new();
 
@@ -127,7 +127,7 @@
 			&params.exprs[param_idx].destruct,
 			unnamed.clone(),
 			destruct_ctx.clone(),
-			&mut passed_args,
+			&mut ctx,
 		)?;
 	}
 
@@ -136,18 +136,16 @@
 			&params.exprs[param_idx].destruct,
 			named[arg_idx].clone(),
 			destruct_ctx.clone(),
-			&mut passed_args,
+			&mut ctx,
 		)?;
 	}
 
 	if prepared.defaults.is_empty() {
-		let body_ctx = body_ctx
-			.extend_bindings(passed_args)
-			.into_future(destruct_ctx);
+		let body_ctx = ctx.build().into_future(destruct_ctx);
 		Ok(body_ctx)
 	} else {
 		let fctx = Context::new_future();
-		let mut defaults = FxHashMap::with_capacity(params.binds_len() - passed_args.len());
+		let mut ctx = ctx.commit();
 		for param_idx in prepared.defaults.iter().copied() {
 			// let param = params.0.rc_idx(param_idx);
 			destruct(
@@ -163,13 +161,10 @@
 					})
 				},
 				fctx.clone(),
-				&mut defaults,
+				&mut ctx,
 			)?;
 		}
 
-		let mut ctx = ContextBuilder::extend(body_ctx);
-		ctx.binds(passed_args);
-		ctx.binds(defaults);
 		Ok(ctx.build().into_future(fctx).into_future(destruct_ctx))
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/gc.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/gc.rs
+++ b/crates/jrsonnet-evaluator/src/gc.rs
@@ -31,3 +31,5 @@
 }
 
 pub fn assert_trace<T: Trace>(_v: &T) {}
+
+pub type ImHashMap<K, V> = im_rc::HashMap<K, V, FxBuildHasher>;
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -15,7 +15,6 @@
 mod import;
 mod integrations;
 pub mod manifest;
-mod map;
 mod obj;
 pub mod stack;
 pub mod stdlib;
@@ -162,19 +161,7 @@
 
 /// During import, this trait will be called to create initial context for file.
 /// It may initialize global variables, stdlib for example.
-pub trait ContextInitializer: Trace {
-	/// For which size the builder should be preallocated
-	fn reserve_vars(&self) -> usize {
-		0
-	}
-	/// Initialize default file context.
-	/// Has default implementation, which calls `populate`.
-	/// Prefer to always implement `populate` instead.
-	fn initialize(&self, for_file: Source) -> Context {
-		let mut builder = ContextBuilder::with_capacity(self.reserve_vars());
-		self.populate(for_file, &mut builder);
-		builder.build()
-	}
+pub trait ContextInitializer {
 	/// For composability: extend builder. May panic if this initialization is not supported,
 	/// and the context may only be created via `initialize`.
 	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);
@@ -182,6 +169,18 @@
 	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.
 	fn as_any(&self) -> &dyn Any;
 }
+impl<T> ContextInitializer for &T
+where
+	T: ContextInitializer,
+{
+	fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
+		(*self).populate(for_file, builder);
+	}
+
+	fn as_any(&self) -> &dyn Any {
+		(*self).as_any()
+	}
+}
 
 /// Context initializer which adds nothing.
 impl ContextInitializer for () {
@@ -193,16 +192,8 @@
 
 impl<T> ContextInitializer for Option<T>
 where
-	T: ContextInitializer,
+	T: ContextInitializer + 'static,
 {
-	fn initialize(&self, for_file: Source) -> Context {
-		if let Some(ctx) = self {
-			ctx.initialize(for_file)
-		} else {
-			().initialize(for_file)
-		}
-	}
-
 	fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
 		if let Some(ctx) = self {
 			ctx.populate(for_file, builder);
@@ -218,12 +209,6 @@
 	($($gen:ident)*) => {
 		#[allow(non_snake_case)]
 		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {
-			fn reserve_vars(&self) -> usize {
-				let mut out = 0;
-				let ($($gen,)*) = self;
-				$(out += $gen.reserve_vars();)*
-				out
-			}
 			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
 				let ($($gen,)*) = self;
 				$($gen.populate(for_file.clone(), builder);)*
@@ -453,19 +438,17 @@
 
 	/// Creates context with all passed global variables
 	pub fn create_default_context(&self, source: Source) -> Context {
-		self.context_initializer().initialize(source)
+		self.create_default_context_with(source, &())
 	}
 
 	/// Creates context with all passed global variables, calling custom modifier
 	pub fn create_default_context_with(
 		&self,
 		source: Source,
-		context_initializer: impl ContextInitializer,
+		context_initializer: &dyn ContextInitializer,
 	) -> Context {
 		let default_initializer = self.context_initializer();
-		let mut builder = ContextBuilder::with_capacity(
-			default_initializer.reserve_vars() + context_initializer.reserve_vars(),
-		);
+		let mut builder = ContextBuilder::new();
 		default_initializer.populate(source.clone(), &mut builder);
 		context_initializer.populate(source, &mut builder);
 
@@ -516,20 +499,14 @@
 impl State {
 	/// Parses and evaluates the given snippet
 	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {
-		let code = code.into();
-		let source = Source::new_virtual(name.into(), code.clone());
-		let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {
-			path: source.clone(),
-			error: Box::new(e),
-		})?;
-		evaluate(self.create_default_context(source), &parsed)
+		self.evaluate_snippet_with(name, code, &())
 	}
 	/// Parses and evaluates the given snippet with custom context modifier
 	pub fn evaluate_snippet_with(
 		&self,
 		name: impl Into<IStr>,
 		code: impl Into<IStr>,
-		context_initializer: impl ContextInitializer,
+		context_initializer: &dyn ContextInitializer,
 	) -> Result<Val> {
 		let code = code.into();
 		let source = Source::new_virtual(name.into(), code.clone());
@@ -587,7 +564,7 @@
 	}
 	pub fn context_initializer(
 		&mut self,
-		context_initializer: impl ContextInitializer,
+		context_initializer: impl ContextInitializer + Trace,
 	) -> &mut Self {
 		let _ = self
 			.context_initializer
modifiedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -1,4 +1,4 @@
-use std::{borrow::Cow, fmt::Write, ptr};
+use std::{borrow::Cow, fmt::Write, hint::black_box, ptr};
 
 use crate::{Result, ResultExt, Val, bail, in_description_frame};
 
@@ -44,6 +44,45 @@
 	}
 }
 
+pub struct BlackBoxFormat;
+impl ManifestFormat for BlackBoxFormat {
+	#[allow(clippy::only_used_in_recursion)]
+	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {
+		match val {
+			Val::Bool(v) => {
+				black_box(v);
+			}
+			val @ Val::Null => {
+				black_box(val);
+			}
+			Val::Str(str_value) => {
+				black_box(format!("{str_value}"));
+			}
+			Val::Num(num_value) => {
+				black_box(num_value);
+			}
+			Val::Arr(arr_value) => {
+				for ele in arr_value.iter() {
+					let ele = ele?;
+					self.manifest_buf(ele, buf)?;
+				}
+			}
+			Val::Obj(obj_value) => {
+				for (name, value) in obj_value.iter() {
+					black_box(name);
+					let value = value?;
+					self.manifest_buf(value, buf)?;
+				}
+			}
+			Val::Func(func_val) => {
+				black_box(func_val);
+				bail!("tried to manifest function")
+			}
+		}
+		Ok(())
+	}
+}
+
 #[derive(PartialEq, Eq, Clone, Copy)]
 enum JsonFormatting {
 	// Applied in manifestification
@@ -66,7 +105,6 @@
 	preserve_order: bool,
 	#[cfg(feature = "exp-bigint")]
 	preserve_bigints: bool,
-	debug_truncate_strings: Option<usize>,
 }
 
 impl<'s> JsonFormat<'s> {
@@ -81,7 +119,6 @@
 			preserve_order,
 			#[cfg(feature = "exp-bigint")]
 			preserve_bigints: false,
-			debug_truncate_strings: None,
 		}
 	}
 	/// Same format as std.toString, except does not keeps top-level string as-is
@@ -96,7 +133,6 @@
 			preserve_order: false,
 			#[cfg(feature = "exp-bigint")]
 			preserve_bigints: false,
-			debug_truncate_strings: None,
 		}
 	}
 	pub fn std_to_json(
@@ -114,7 +150,6 @@
 			preserve_order,
 			#[cfg(feature = "exp-bigint")]
 			preserve_bigints: false,
-			debug_truncate_strings: None,
 		}
 	}
 	// Same format as CLI manifestification
@@ -137,7 +172,6 @@
 			preserve_order,
 			#[cfg(feature = "exp-bigint")]
 			preserve_bigints: false,
-			debug_truncate_strings: None,
 		}
 	}
 	// Same format as CLI manifestification
@@ -151,7 +185,6 @@
 			preserve_order: true,
 			#[cfg(feature = "exp-bigint")]
 			preserve_bigints: true,
-			debug_truncate_strings: Some(256),
 		}
 	}
 }
@@ -166,7 +199,6 @@
 			preserve_order: false,
 			#[cfg(feature = "exp-bigint")]
 			preserve_bigints: false,
-			debug_truncate_strings: None,
 		}
 	}
 }
@@ -197,18 +229,12 @@
 		}
 		Val::Null => buf.push_str("null"),
 		Val::Str(s) => {
-			let flat = s.clone().into_flat();
-			if let Some(truncate) = options.debug_truncate_strings {
-				if flat.len() > truncate {
-					let (start, end) = flat.split_at(truncate / 2);
-					let (_, end) = end.split_at(end.len() - truncate / 2);
-					escape_string_json_buf(&format!("{start}..{end}"), buf);
-				} else {
-					escape_string_json_buf(&flat, buf);
-				}
-			} else {
-				escape_string_json_buf(&flat, buf);
-			}
+			buf.reserve(2 + s.len());
+			buf.push('"');
+			s.chunks(&mut |c| {
+				escape_string_json_buf_raw(c, buf);
+			});
+			buf.push('"');
 		}
 		Val::Num(n) => write!(buf, "{n}").unwrap(),
 		#[cfg(feature = "exp-bigint")]
@@ -476,15 +502,15 @@
 ];
 
 pub fn escape_string_json_buf(value: &str, buf: &mut String) {
+	buf.reserve_exact(value.len() + 2);
+	escape_string_json_buf_raw(value, buf);
+}
+
+fn escape_string_json_buf_raw(value: &str, buf: &mut String) {
 	// Safety: we only write correct utf-8 in this function
 	let buf: &mut Vec<u8> = unsafe { &mut *ptr::from_mut(buf).cast::<Vec<u8>>() };
 	let bytes = value.as_bytes();
-
-	// Perfect for ascii strings, removes any reallocations
-	buf.reserve(value.len() + 2);
 
-	buf.push(b'"');
-
 	let mut start = 0;
 
 	for (i, &byte) in bytes.iter().enumerate() {
@@ -519,10 +545,8 @@
 	}
 
 	if start == bytes.len() {
-		buf.push(b'"');
 		return;
 	}
 
 	buf.extend_from_slice(&bytes[start..]);
-	buf.push(b'"');
 }
deletedcrates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/map.rs
+++ /dev/null
@@ -1,67 +0,0 @@
-use jrsonnet_gcmodule::{Cc, Trace};
-use jrsonnet_interner::IStr;
-use rustc_hash::FxHashMap;
-
-use crate::{Thunk, Val, gc::WithCapacityExt as _};
-
-#[derive(Trace, Debug)]
-#[trace(tracking(force))]
-pub struct LayeredHashMapInternals {
-	parent: Option<LayeredHashMap>,
-	current: FxHashMap<IStr, Thunk<Val>>,
-}
-
-#[derive(Trace, Debug)]
-pub struct LayeredHashMap(Cc<LayeredHashMapInternals>);
-
-impl LayeredHashMap {
-	pub fn iter_keys(self, mut handler: impl FnMut(IStr)) {
-		for k in self.0.current.keys() {
-			handler(k.clone());
-		}
-		if let Some(parent) = self.0.parent.clone() {
-			parent.iter_keys(handler);
-		}
-	}
-
-	pub(crate) fn new(layer: FxHashMap<IStr, Thunk<Val>>) -> Self {
-		Self(Cc::new(LayeredHashMapInternals {
-			parent: None,
-			current: layer,
-		}))
-	}
-
-	pub fn extend(self, new_layer: FxHashMap<IStr, Thunk<Val>>) -> Self {
-		Self(Cc::new(LayeredHashMapInternals {
-			parent: Some(self),
-			current: new_layer,
-		}))
-	}
-
-	pub fn get(&self, key: &IStr) -> Option<&Thunk<Val>> {
-		(self.0)
-			.current
-			.get(key)
-			.or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))
-	}
-
-	pub fn contains_key(&self, key: &IStr) -> bool {
-		(self.0).current.contains_key(key)
-			|| self.0.parent.as_ref().is_some_and(|p| p.contains_key(key))
-	}
-}
-
-impl Clone for LayeredHashMap {
-	fn clone(&self) -> Self {
-		Self(self.0.clone())
-	}
-}
-
-impl Default for LayeredHashMap {
-	fn default() -> Self {
-		Self(Cc::new(LayeredHashMapInternals {
-			parent: None,
-			current: FxHashMap::new(),
-		}))
-	}
-}
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -355,8 +355,19 @@
 			Self::Tree(Rc::new((a, b, len)))
 		}
 	}
+	pub fn chunks(&self, c: &mut impl FnMut(&IStr)) {
+		fn write_buf(s: &StrValue, c: &mut impl FnMut(&IStr)) {
+			match s {
+				StrValue::Flat(f) => c(f),
+				StrValue::Tree(t) => {
+					write_buf(&t.0, c);
+					write_buf(&t.1, c);
+				}
+			}
+		}
+		write_buf(self, c);
+	}
 	pub fn into_flat(&self) -> IStr {
-		#[cold]
 		fn write_buf(s: &StrValue, out: &mut String) {
 			match s {
 				StrValue::Flat(f) => out.push_str(f),
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -545,9 +545,6 @@
 	}
 }
 impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {
-	fn reserve_vars(&self) -> usize {
-		1
-	}
 	fn populate(&self, source: Source, builder: &mut ContextBuilder) {
 		let mut std = ObjValueBuilder::new();
 		std.with_super(self.stdlib_obj.clone());
modifiedtests/Cargo.tomldiffbeforeafterboth
--- a/tests/Cargo.toml
+++ b/tests/Cargo.toml
@@ -18,6 +18,7 @@
 jrsonnet-evaluator.workspace = true
 jrsonnet-gcmodule.workspace = true
 jrsonnet-stdlib.workspace = true
+mimallocator.workspace = true
 serde.workspace = true
 serde_json.workspace = true