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

difftreelog

refactor unified ContextBuilder

lzpkwkksYaroslav Bolyukin2026-04-25parent: #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
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -8,19 +8,17 @@
 	function::ParamName,
 };
 use jrsonnet_types::ValType;
-use rustc_hash::FxHashMap;
 
 use self::destructure::destruct;
 use crate::{
-	Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, ResultExt,
-	SupThis, Unbound, Val,
+	Context, ContextBuilder, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,
+	ResultExt, SupThis, Unbound, Val,
 	arr::ArrValue,
 	bail,
 	destructure::evaluate_dest,
 	error::{ErrorKind::*, suggest_object_fields},
 	evaluate::operator::{evaluate_binary_op_special, evaluate_unary_op},
 	function::{CallLocation, FuncDesc, FuncVal, PreparedFuncVal},
-	gc::WithCapacityExt as _,
 	in_frame,
 	typed::{FromUntyped, IntoUntyped as _, Typed},
 	val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},
@@ -130,9 +128,9 @@
 					guaranteed_reserve = guaranteed_reserve.max(1) * list.len();
 					for (i, item) in list.iter_lazy().enumerate() {
 						let fctx = Pending::new();
-						let mut new_bindings = FxHashMap::with_capacity(into.binds_len());
-						destruct(into, item, fctx.clone(), &mut new_bindings)?;
-						let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);
+						let mut ctx = ContextBuilder::extend_fast(ctx.clone());
+						destruct(into, item, fctx.clone(), &mut ctx)?;
+						let ctx = ctx.build().into_future(fctx);
 
 						let specs = &specs[1..];
 						evaluate_comp(
@@ -179,6 +177,7 @@
 }
 
 fn evaluate_arr_comp(ctx: Context, expr: &Rc<Expr>, comp_specs: &[CompSpec]) -> Result<ArrValue> {
+	let ctx = ctx.branch_point();
 	'eager: {
 		let mut out = Vec::new();
 
@@ -225,17 +224,13 @@
 
 		fn bind(&self, sup_this: SupThis) -> Result<Context> {
 			let fctx = Context::new_future();
-			let mut new_bindings =
-				FxHashMap::with_capacity(self.locals.iter().map(BindSpec::binds_len).sum());
+			let ctx = self.fctx.clone();
+			let mut ctx = ContextBuilder::extend(ctx);
 			for b in self.locals.iter() {
-				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;
+				evaluate_dest(b, fctx.clone(), &mut ctx)?;
 			}
 
-			let ctx = self.fctx.clone();
-
-			let ctx = ctx
-				.extend_bindings_sup_this(new_bindings, sup_this)
-				.into_future(fctx);
+			let ctx = ctx.build_sup_this(sup_this).into_future(fctx);
 
 			Ok(ctx)
 		}
@@ -332,10 +327,7 @@
 impl Unbound for DirectUnbound {
 	type Bound = Context;
 	fn bind(&self, sup_this: SupThis) -> Result<Context> {
-		Ok(self
-			.0
-			.clone()
-			.extend_bindings_sup_this(FxHashMap::new(), sup_this))
+		Ok(ContextBuilder::extend(self.0.clone()).build_sup_this(sup_this))
 	}
 }
 
@@ -408,12 +400,17 @@
 				builder.with_super(super_obj);
 			}
 			let locals = obj.locals.clone();
-			evaluate_comp(ctx, &obj.compspecs, 0, &mut |ctx, reserve| {
-				let uctx = evaluate_object_locals(ctx.clone(), locals.clone());
-				builder.reserve_fields(reserve);
+			evaluate_comp(
+				ctx.branch_point(),
+				&obj.compspecs,
+				0,
+				&mut |ctx, reserve| {
+					let uctx = evaluate_object_locals(ctx.clone(), locals.clone());
+					builder.reserve_fields(reserve);
 
-				evaluate_field_member(&mut builder, ctx, uctx, &obj.field)
-			})?;
+					evaluate_field_member(&mut builder, ctx, uctx, &obj.field)
+				},
+			)?;
 
 			builder.build()
 		}
@@ -684,13 +681,12 @@
 			Ok(indexable)
 		})?,
 		LocalExpr(bindings, returned) => {
-			let mut new_bindings: FxHashMap<IStr, Thunk<Val>> =
-				FxHashMap::with_capacity(bindings.iter().map(BindSpec::binds_len).sum());
 			let fctx = Context::new_future();
+			let mut ctx = ContextBuilder::extend(ctx);
 			for b in bindings {
-				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;
+				evaluate_dest(b, fctx.clone(), &mut ctx)?;
 			}
-			let ctx = ctx.extend_bindings(new_bindings).into_future(fctx);
+			let ctx = ctx.build().into_future(fctx);
 			evaluate(ctx, returned)?
 		}
 		Arr(items) => {
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
15mod import;15mod import;
16mod integrations;16mod integrations;
17pub mod manifest;17pub mod manifest;
18mod map;
19mod obj;18mod obj;
20pub mod stack;19pub mod stack;
21pub mod stdlib;20pub mod stdlib;
162161
163/// During import, this trait will be called to create initial context for file.162/// During import, this trait will be called to create initial context for file.
164/// It may initialize global variables, stdlib for example.163/// It may initialize global variables, stdlib for example.
165pub trait ContextInitializer: Trace {164pub trait ContextInitializer {
166 /// For which size the builder should be preallocated
167 fn reserve_vars(&self) -> usize {
168 0
169 }
170 /// Initialize default file context.
171 /// Has default implementation, which calls `populate`.
172 /// Prefer to always implement `populate` instead.
173 fn initialize(&self, for_file: Source) -> Context {
174 let mut builder = ContextBuilder::with_capacity(self.reserve_vars());
175 self.populate(for_file, &mut builder);
176 builder.build()
177 }
178 /// For composability: extend builder. May panic if this initialization is not supported,165 /// For composability: extend builder. May panic if this initialization is not supported,
179 /// and the context may only be created via `initialize`.166 /// and the context may only be created via `initialize`.
180 fn populate(&self, for_file: Source, builder: &mut ContextBuilder);167 fn populate(&self, for_file: Source, builder: &mut ContextBuilder);
181 /// Allows upcasting from abstract to concrete context initializer.168 /// Allows upcasting from abstract to concrete context initializer.
182 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.169 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.
183 fn as_any(&self) -> &dyn Any;170 fn as_any(&self) -> &dyn Any;
184}171}
172impl<T> ContextInitializer for &T
173where
174 T: ContextInitializer,
175{
176 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
177 (*self).populate(for_file, builder);
178 }
179
180 fn as_any(&self) -> &dyn Any {
181 (*self).as_any()
182 }
183}
185184
186/// Context initializer which adds nothing.185/// Context initializer which adds nothing.
187impl ContextInitializer for () {186impl ContextInitializer for () {
193192
194impl<T> ContextInitializer for Option<T>193impl<T> ContextInitializer for Option<T>
195where194where
196 T: ContextInitializer,195 T: ContextInitializer + 'static,
197{196{
198 fn initialize(&self, for_file: Source) -> Context {
199 if let Some(ctx) = self {
200 ctx.initialize(for_file)
201 } else {
202 ().initialize(for_file)
203 }
204 }
205
206 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {197 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
207 if let Some(ctx) = self {198 if let Some(ctx) = self {
218 ($($gen:ident)*) => {209 ($($gen:ident)*) => {
219 #[allow(non_snake_case)]210 #[allow(non_snake_case)]
220 impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {211 impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {
221 fn reserve_vars(&self) -> usize {
222 let mut out = 0;
223 let ($($gen,)*) = self;
224 $(out += $gen.reserve_vars();)*
225 out
226 }
227 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {212 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
228 let ($($gen,)*) = self;213 let ($($gen,)*) = self;
229 $($gen.populate(for_file.clone(), builder);)*214 $($gen.populate(for_file.clone(), builder);)*
453438
454 /// Creates context with all passed global variables439 /// Creates context with all passed global variables
455 pub fn create_default_context(&self, source: Source) -> Context {440 pub fn create_default_context(&self, source: Source) -> Context {
456 self.context_initializer().initialize(source)441 self.create_default_context_with(source, &())
457 }442 }
458443
459 /// Creates context with all passed global variables, calling custom modifier444 /// Creates context with all passed global variables, calling custom modifier
460 pub fn create_default_context_with(445 pub fn create_default_context_with(
461 &self,446 &self,
462 source: Source,447 source: Source,
463 context_initializer: impl ContextInitializer,448 context_initializer: &dyn ContextInitializer,
464 ) -> Context {449 ) -> Context {
465 let default_initializer = self.context_initializer();450 let default_initializer = self.context_initializer();
466 let mut builder = ContextBuilder::with_capacity(451 let mut builder = ContextBuilder::new();
467 default_initializer.reserve_vars() + context_initializer.reserve_vars(),
468 );
469 default_initializer.populate(source.clone(), &mut builder);452 default_initializer.populate(source.clone(), &mut builder);
470 context_initializer.populate(source, &mut builder);453 context_initializer.populate(source, &mut builder);
516impl State {499impl State {
517 /// Parses and evaluates the given snippet500 /// Parses and evaluates the given snippet
518 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {501 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {
519 let code = code.into();
520 let source = Source::new_virtual(name.into(), code.clone());502 self.evaluate_snippet_with(name, code, &())
521 let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {
522 path: source.clone(),
523 error: Box::new(e),
524 })?;
525 evaluate(self.create_default_context(source), &parsed)
526 }503 }
527 /// Parses and evaluates the given snippet with custom context modifier504 /// Parses and evaluates the given snippet with custom context modifier
528 pub fn evaluate_snippet_with(505 pub fn evaluate_snippet_with(
529 &self,506 &self,
530 name: impl Into<IStr>,507 name: impl Into<IStr>,
531 code: impl Into<IStr>,508 code: impl Into<IStr>,
532 context_initializer: impl ContextInitializer,509 context_initializer: &dyn ContextInitializer,
533 ) -> Result<Val> {510 ) -> Result<Val> {
534 let code = code.into();511 let code = code.into();
535 let source = Source::new_virtual(name.into(), code.clone());512 let source = Source::new_virtual(name.into(), code.clone());
587 }564 }
588 pub fn context_initializer(565 pub fn context_initializer(
589 &mut self,566 &mut self,
590 context_initializer: impl ContextInitializer,567 context_initializer: impl ContextInitializer + Trace,
591 ) -> &mut Self {568 ) -> &mut Self {
592 let _ = self569 let _ = self
593 .context_initializer570 .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