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
--- 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
--- 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
before · crates/jrsonnet-evaluator/src/manifest.rs
1use std::{borrow::Cow, fmt::Write, ptr};23use crate::{Result, ResultExt, Val, bail, in_description_frame};45pub trait ManifestFormat {6	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;7	fn manifest(&self, val: Val) -> Result<String> {8		let mut out = String::new();9		self.manifest_buf(val, &mut out)?;10		Ok(out)11	}12	/// When outputing to file, is it safe to append a trailing newline (I.e newline won't change13	/// the meaning).14	///15	/// Default implementation returns `true`16	fn file_trailing_newline(&self) -> bool {17		true18	}19}20impl<T> ManifestFormat for Box<T>21where22	T: ManifestFormat + ?Sized,23{24	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {25		let inner = &**self;26		inner.manifest_buf(val, buf)27	}28	fn file_trailing_newline(&self) -> bool {29		let inner = &**self;30		inner.file_trailing_newline()31	}32}33impl<T> ManifestFormat for &'_ T34where35	T: ManifestFormat + ?Sized,36{37	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {38		let inner = &**self;39		inner.manifest_buf(val, buf)40	}41	fn file_trailing_newline(&self) -> bool {42		let inner = &**self;43		inner.file_trailing_newline()44	}45}4647#[derive(PartialEq, Eq, Clone, Copy)]48enum JsonFormatting {49	// Applied in manifestification50	Manifest,51	/// Used for std.manifestJson52	/// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest53	Std,54	/// No line breaks, used in `obj+''`55	ToString,56	/// Minified json57	Minify,58}5960pub struct JsonFormat<'s> {61	padding: Cow<'s, str>,62	mtype: JsonFormatting,63	newline: &'s str,64	key_val_sep: &'s str,65	#[cfg(feature = "exp-preserve-order")]66	preserve_order: bool,67	#[cfg(feature = "exp-bigint")]68	preserve_bigints: bool,69	debug_truncate_strings: Option<usize>,70}7172impl<'s> JsonFormat<'s> {73	// Minifying format74	pub fn minify(#[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Self {75		Self {76			padding: Cow::Borrowed(""),77			mtype: JsonFormatting::Minify,78			newline: "\n",79			key_val_sep: ":",80			#[cfg(feature = "exp-preserve-order")]81			preserve_order,82			#[cfg(feature = "exp-bigint")]83			preserve_bigints: false,84			debug_truncate_strings: None,85		}86	}87	/// Same format as std.toString, except does not keeps top-level string as-is88	/// To avoid confusion, the format is private in jrsonnet, use [`ToStringFormat`] instead89	const fn std_to_string_helper() -> Self {90		Self {91			padding: Cow::Borrowed(""),92			mtype: JsonFormatting::ToString,93			newline: "\n",94			key_val_sep: ": ",95			#[cfg(feature = "exp-preserve-order")]96			preserve_order: false,97			#[cfg(feature = "exp-bigint")]98			preserve_bigints: false,99			debug_truncate_strings: None,100		}101	}102	pub fn std_to_json(103		padding: String,104		newline: &'s str,105		key_val_sep: &'s str,106		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,107	) -> Self {108		Self {109			padding: Cow::Owned(padding),110			mtype: JsonFormatting::Std,111			newline,112			key_val_sep,113			#[cfg(feature = "exp-preserve-order")]114			preserve_order,115			#[cfg(feature = "exp-bigint")]116			preserve_bigints: false,117			debug_truncate_strings: None,118		}119	}120	// Same format as CLI manifestification121	pub fn cli(122		padding: usize,123		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,124	) -> Self {125		if padding == 0 {126			return Self::minify(127				#[cfg(feature = "exp-preserve-order")]128				preserve_order,129			);130		}131		Self {132			padding: Cow::Owned(" ".repeat(padding)),133			mtype: JsonFormatting::Manifest,134			newline: "\n",135			key_val_sep: ": ",136			#[cfg(feature = "exp-preserve-order")]137			preserve_order,138			#[cfg(feature = "exp-bigint")]139			preserve_bigints: false,140			debug_truncate_strings: None,141		}142	}143	// Same format as CLI manifestification144	pub fn debug() -> Self {145		Self {146			padding: Cow::Borrowed("   "),147			mtype: JsonFormatting::Manifest,148			newline: "\n",149			key_val_sep: ": ",150			#[cfg(feature = "exp-preserve-order")]151			preserve_order: true,152			#[cfg(feature = "exp-bigint")]153			preserve_bigints: true,154			debug_truncate_strings: Some(256),155		}156	}157}158impl Default for JsonFormat<'static> {159	fn default() -> Self {160		Self {161			padding: Cow::Borrowed("    "),162			mtype: JsonFormatting::Manifest,163			newline: "\n",164			key_val_sep: ": ",165			#[cfg(feature = "exp-preserve-order")]166			preserve_order: false,167			#[cfg(feature = "exp-bigint")]168			preserve_bigints: false,169			debug_truncate_strings: None,170		}171	}172}173174pub fn manifest_json_ex(val: &Val, options: &JsonFormat<'_>) -> Result<String> {175	let mut out = String::new();176	manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;177	Ok(out)178}179180#[allow(clippy::too_many_lines)]181fn manifest_json_ex_buf(182	val: &Val,183	buf: &mut String,184	cur_padding: &mut String,185	options: &JsonFormat<'_>,186) -> Result<()> {187	use JsonFormatting::*;188189	let mtype = options.mtype;190	match val {191		Val::Bool(v) => {192			if *v {193				buf.push_str("true");194			} else {195				buf.push_str("false");196			}197		}198		Val::Null => buf.push_str("null"),199		Val::Str(s) => {200			let flat = s.clone().into_flat();201			if let Some(truncate) = options.debug_truncate_strings {202				if flat.len() > truncate {203					let (start, end) = flat.split_at(truncate / 2);204					let (_, end) = end.split_at(end.len() - truncate / 2);205					escape_string_json_buf(&format!("{start}..{end}"), buf);206				} else {207					escape_string_json_buf(&flat, buf);208				}209			} else {210				escape_string_json_buf(&flat, buf);211			}212		}213		Val::Num(n) => write!(buf, "{n}").unwrap(),214		#[cfg(feature = "exp-bigint")]215		Val::BigInt(n) => {216			if options.preserve_bigints {217				write!(buf, "{n}").unwrap();218			} else {219				write!(buf, "{:?}", n.to_string()).unwrap();220			}221		}222		Val::Arr(items) => {223			buf.push('[');224225			let old_len = cur_padding.len();226			cur_padding.push_str(&options.padding);227228			let mut had_items = false;229			for (i, item) in items.iter().enumerate() {230				had_items = true;231				let item = item.with_description(|| format!("elem <{i}> evaluation"))?;232233				if i != 0 {234					buf.push(',');235				}236				match mtype {237					Manifest | Std => {238						buf.push_str(options.newline);239						buf.push_str(cur_padding);240					}241					ToString if i != 0 => buf.push(' '),242					Minify | ToString => {}243				}244245				in_description_frame(246					|| format!("elem <{i}> manifestification"),247					|| manifest_json_ex_buf(&item, buf, cur_padding, options),248				)?;249			}250251			cur_padding.truncate(old_len);252253			match mtype {254				Manifest | ToString if !had_items => {255					// Empty array as "[ ]"256					buf.push(' ');257				}258				Manifest => {259					buf.push_str(options.newline);260					buf.push_str(cur_padding);261				}262				Std => {263					if !had_items {264						// Stdlib formats empty array as "[\n\n]"265						buf.push_str(options.newline);266					}267					buf.push_str(options.newline);268					buf.push_str(cur_padding);269				}270				Minify | ToString => {}271			}272273			buf.push(']');274		}275		Val::Obj(obj) => {276			obj.run_assertions()?;277			buf.push('{');278279			let old_len = cur_padding.len();280			cur_padding.push_str(&options.padding);281282			let mut had_fields = false;283			for (i, (key, value)) in obj284				.iter(285					#[cfg(feature = "exp-preserve-order")]286					options.preserve_order,287				)288				.enumerate()289			{290				had_fields = true;291				let value = value.with_description(|| format!("field <{key}> evaluation"))?;292293				if i != 0 {294					buf.push(',');295				}296				match mtype {297					Manifest | Std => {298						buf.push_str(options.newline);299						buf.push_str(cur_padding);300					}301					ToString if i != 0 => buf.push(' '),302					Minify | ToString => {}303				}304305				escape_string_json_buf(&key, buf);306				buf.push_str(options.key_val_sep);307				in_description_frame(308					|| format!("field <{key}> manifestification"),309					|| manifest_json_ex_buf(&value, buf, cur_padding, options),310				)?;311			}312313			cur_padding.truncate(old_len);314315			match mtype {316				Manifest | ToString if !had_fields => {317					// Empty object as "{ }"318					buf.push(' ');319				}320				Manifest => {321					buf.push_str(options.newline);322					buf.push_str(cur_padding);323				}324				Std => {325					if !had_fields {326						// Stdlib formats empty object as "{\n\n}"327						buf.push_str(options.newline);328					}329					buf.push_str(options.newline);330					buf.push_str(cur_padding);331				}332				Minify | ToString => {}333			}334335			buf.push('}');336		}337		Val::Func(_) => bail!("tried to manifest function"),338	}339	Ok(())340}341342impl ManifestFormat for JsonFormat<'_> {343	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {344		manifest_json_ex_buf(&val, buf, &mut String::new(), self)345	}346}347348/// Same as [`JsonFormat`] with pre-set options, but top-level string is serialized as-is,349/// without quoting.350pub struct ToStringFormat;351impl ManifestFormat for ToStringFormat {352	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {353		const JSON_TO_STRING: JsonFormat = JsonFormat::std_to_string_helper();354		if let Some(str) = val.as_str() {355			out.push_str(&str);356			return Ok(());357		}358		#[cfg(feature = "exp-bigint")]359		if let Some(int) = val.as_bigint() {360			out.push_str(&int.to_str_radix(10));361			return Ok(());362		}363		JSON_TO_STRING.manifest_buf(val, out)364	}365	fn file_trailing_newline(&self) -> bool {366		false367	}368}369pub struct StringFormat;370impl ManifestFormat for StringFormat {371	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {372		let Val::Str(s) = val else {373			bail!(374				"output should be string for string manifest format, got {}",375				val.value_type()376			)377		};378		write!(out, "{s}").unwrap();379		Ok(())380	}381	fn file_trailing_newline(&self) -> bool {382		false383	}384}385386pub struct YamlStreamFormat<I> {387	inner: I,388	c_document_end: bool,389	end_newline: bool,390}391impl<I> YamlStreamFormat<I> {392	pub fn std_yaml_stream(inner: I, c_document_end: bool) -> Self {393		Self {394			inner,395			c_document_end,396			// Stdlib format always inserts useless newline at the end397			end_newline: true,398		}399	}400	pub fn cli(inner: I) -> Self {401		Self {402			inner,403			c_document_end: true,404			end_newline: false,405		}406	}407}408impl<I: ManifestFormat> ManifestFormat for YamlStreamFormat<I> {409	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {410		let Val::Arr(arr) = val else {411			bail!(412				"output should be array for yaml stream format, got {}",413				val.value_type()414			)415		};416		for (i, v) in arr.iter().enumerate() {417			if i != 0 {418				out.push('\n');419			}420			let v = v.with_description(|| format!("elem <{i}> evaluation"))?;421			out.push_str("---\n");422			in_description_frame(423				|| format!("elem <{i}> manifestification"),424				|| self.inner.manifest_buf(v, out),425			)?;426		}427		if self.c_document_end {428			out.push('\n');429			out.push_str("...");430		}431		if self.end_newline {432			out.push('\n');433		}434		Ok(())435	}436}437438pub fn escape_string_json(s: &str) -> String {439	let mut buf = String::new();440	escape_string_json_buf(s, &mut buf);441	buf442}443444// Json string encoding was borrowed from https://github.com/serde-rs/json445446const BB: u8 = b'b'; // \x08447const TT: u8 = b't'; // \x09448const NN: u8 = b'n'; // \x0A449const FF: u8 = b'f'; // \x0C450const RR: u8 = b'r'; // \x0D451const QU: u8 = b'"'; // \x22452const BS: u8 = b'\\'; // \x5C453const UU: u8 = b'u'; // \x00...\x1F except the ones above454const __: u8 = 0;455456// Lookup table of escape sequences. A value of b'x' at index i means that byte457// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped.458static ESCAPE: [u8; 256] = [459	//   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F460	UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, // 0461	UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1462	__, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2463	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3464	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4465	__, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5466	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6467	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7468	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8469	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9470	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A471	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B472	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C473	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D474	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E475	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F476];477478pub fn escape_string_json_buf(value: &str, buf: &mut String) {479	// Safety: we only write correct utf-8 in this function480	let buf: &mut Vec<u8> = unsafe { &mut *ptr::from_mut(buf).cast::<Vec<u8>>() };481	let bytes = value.as_bytes();482483	// Perfect for ascii strings, removes any reallocations484	buf.reserve(value.len() + 2);485486	buf.push(b'"');487488	let mut start = 0;489490	for (i, &byte) in bytes.iter().enumerate() {491		let escape = ESCAPE[byte as usize];492		if escape == __ {493			continue;494		}495496		if start < i {497			buf.extend_from_slice(&bytes[start..i]);498		}499		start = i + 1;500501		match escape {502			self::BB | self::TT | self::NN | self::FF | self::RR | self::QU | self::BS => {503				buf.extend_from_slice(&[b'\\', escape]);504			}505			self::UU => {506				static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef";507				let bytes = &[508					b'\\',509					b'u',510					b'0',511					b'0',512					HEX_DIGITS[(byte >> 4) as usize],513					HEX_DIGITS[(byte & 0xF) as usize],514				];515				buf.extend_from_slice(bytes);516			}517			_ => unreachable!(),518		}519	}520521	if start == bytes.len() {522		buf.push(b'"');523		return;524	}525526	buf.extend_from_slice(&bytes[start..]);527	buf.push(b'"');528}
after · crates/jrsonnet-evaluator/src/manifest.rs
1use std::{borrow::Cow, fmt::Write, hint::black_box, ptr};23use crate::{Result, ResultExt, Val, bail, in_description_frame};45pub trait ManifestFormat {6	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;7	fn manifest(&self, val: Val) -> Result<String> {8		let mut out = String::new();9		self.manifest_buf(val, &mut out)?;10		Ok(out)11	}12	/// When outputing to file, is it safe to append a trailing newline (I.e newline won't change13	/// the meaning).14	///15	/// Default implementation returns `true`16	fn file_trailing_newline(&self) -> bool {17		true18	}19}20impl<T> ManifestFormat for Box<T>21where22	T: ManifestFormat + ?Sized,23{24	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {25		let inner = &**self;26		inner.manifest_buf(val, buf)27	}28	fn file_trailing_newline(&self) -> bool {29		let inner = &**self;30		inner.file_trailing_newline()31	}32}33impl<T> ManifestFormat for &'_ T34where35	T: ManifestFormat + ?Sized,36{37	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {38		let inner = &**self;39		inner.manifest_buf(val, buf)40	}41	fn file_trailing_newline(&self) -> bool {42		let inner = &**self;43		inner.file_trailing_newline()44	}45}4647pub struct BlackBoxFormat;48impl ManifestFormat for BlackBoxFormat {49	#[allow(clippy::only_used_in_recursion)]50	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {51		match val {52			Val::Bool(v) => {53				black_box(v);54			}55			val @ Val::Null => {56				black_box(val);57			}58			Val::Str(str_value) => {59				black_box(format!("{str_value}"));60			}61			Val::Num(num_value) => {62				black_box(num_value);63			}64			Val::Arr(arr_value) => {65				for ele in arr_value.iter() {66					let ele = ele?;67					self.manifest_buf(ele, buf)?;68				}69			}70			Val::Obj(obj_value) => {71				for (name, value) in obj_value.iter() {72					black_box(name);73					let value = value?;74					self.manifest_buf(value, buf)?;75				}76			}77			Val::Func(func_val) => {78				black_box(func_val);79				bail!("tried to manifest function")80			}81		}82		Ok(())83	}84}8586#[derive(PartialEq, Eq, Clone, Copy)]87enum JsonFormatting {88	// Applied in manifestification89	Manifest,90	/// Used for std.manifestJson91	/// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest92	Std,93	/// No line breaks, used in `obj+''`94	ToString,95	/// Minified json96	Minify,97}9899pub struct JsonFormat<'s> {100	padding: Cow<'s, str>,101	mtype: JsonFormatting,102	newline: &'s str,103	key_val_sep: &'s str,104	#[cfg(feature = "exp-preserve-order")]105	preserve_order: bool,106	#[cfg(feature = "exp-bigint")]107	preserve_bigints: bool,108}109110impl<'s> JsonFormat<'s> {111	// Minifying format112	pub fn minify(#[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Self {113		Self {114			padding: Cow::Borrowed(""),115			mtype: JsonFormatting::Minify,116			newline: "\n",117			key_val_sep: ":",118			#[cfg(feature = "exp-preserve-order")]119			preserve_order,120			#[cfg(feature = "exp-bigint")]121			preserve_bigints: false,122		}123	}124	/// Same format as std.toString, except does not keeps top-level string as-is125	/// To avoid confusion, the format is private in jrsonnet, use [`ToStringFormat`] instead126	const fn std_to_string_helper() -> Self {127		Self {128			padding: Cow::Borrowed(""),129			mtype: JsonFormatting::ToString,130			newline: "\n",131			key_val_sep: ": ",132			#[cfg(feature = "exp-preserve-order")]133			preserve_order: false,134			#[cfg(feature = "exp-bigint")]135			preserve_bigints: false,136		}137	}138	pub fn std_to_json(139		padding: String,140		newline: &'s str,141		key_val_sep: &'s str,142		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,143	) -> Self {144		Self {145			padding: Cow::Owned(padding),146			mtype: JsonFormatting::Std,147			newline,148			key_val_sep,149			#[cfg(feature = "exp-preserve-order")]150			preserve_order,151			#[cfg(feature = "exp-bigint")]152			preserve_bigints: false,153		}154	}155	// Same format as CLI manifestification156	pub fn cli(157		padding: usize,158		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,159	) -> Self {160		if padding == 0 {161			return Self::minify(162				#[cfg(feature = "exp-preserve-order")]163				preserve_order,164			);165		}166		Self {167			padding: Cow::Owned(" ".repeat(padding)),168			mtype: JsonFormatting::Manifest,169			newline: "\n",170			key_val_sep: ": ",171			#[cfg(feature = "exp-preserve-order")]172			preserve_order,173			#[cfg(feature = "exp-bigint")]174			preserve_bigints: false,175		}176	}177	// Same format as CLI manifestification178	pub fn debug() -> Self {179		Self {180			padding: Cow::Borrowed("   "),181			mtype: JsonFormatting::Manifest,182			newline: "\n",183			key_val_sep: ": ",184			#[cfg(feature = "exp-preserve-order")]185			preserve_order: true,186			#[cfg(feature = "exp-bigint")]187			preserve_bigints: true,188		}189	}190}191impl Default for JsonFormat<'static> {192	fn default() -> Self {193		Self {194			padding: Cow::Borrowed("    "),195			mtype: JsonFormatting::Manifest,196			newline: "\n",197			key_val_sep: ": ",198			#[cfg(feature = "exp-preserve-order")]199			preserve_order: false,200			#[cfg(feature = "exp-bigint")]201			preserve_bigints: false,202		}203	}204}205206pub fn manifest_json_ex(val: &Val, options: &JsonFormat<'_>) -> Result<String> {207	let mut out = String::new();208	manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;209	Ok(out)210}211212#[allow(clippy::too_many_lines)]213fn manifest_json_ex_buf(214	val: &Val,215	buf: &mut String,216	cur_padding: &mut String,217	options: &JsonFormat<'_>,218) -> Result<()> {219	use JsonFormatting::*;220221	let mtype = options.mtype;222	match val {223		Val::Bool(v) => {224			if *v {225				buf.push_str("true");226			} else {227				buf.push_str("false");228			}229		}230		Val::Null => buf.push_str("null"),231		Val::Str(s) => {232			buf.reserve(2 + s.len());233			buf.push('"');234			s.chunks(&mut |c| {235				escape_string_json_buf_raw(c, buf);236			});237			buf.push('"');238		}239		Val::Num(n) => write!(buf, "{n}").unwrap(),240		#[cfg(feature = "exp-bigint")]241		Val::BigInt(n) => {242			if options.preserve_bigints {243				write!(buf, "{n}").unwrap();244			} else {245				write!(buf, "{:?}", n.to_string()).unwrap();246			}247		}248		Val::Arr(items) => {249			buf.push('[');250251			let old_len = cur_padding.len();252			cur_padding.push_str(&options.padding);253254			let mut had_items = false;255			for (i, item) in items.iter().enumerate() {256				had_items = true;257				let item = item.with_description(|| format!("elem <{i}> evaluation"))?;258259				if i != 0 {260					buf.push(',');261				}262				match mtype {263					Manifest | Std => {264						buf.push_str(options.newline);265						buf.push_str(cur_padding);266					}267					ToString if i != 0 => buf.push(' '),268					Minify | ToString => {}269				}270271				in_description_frame(272					|| format!("elem <{i}> manifestification"),273					|| manifest_json_ex_buf(&item, buf, cur_padding, options),274				)?;275			}276277			cur_padding.truncate(old_len);278279			match mtype {280				Manifest | ToString if !had_items => {281					// Empty array as "[ ]"282					buf.push(' ');283				}284				Manifest => {285					buf.push_str(options.newline);286					buf.push_str(cur_padding);287				}288				Std => {289					if !had_items {290						// Stdlib formats empty array as "[\n\n]"291						buf.push_str(options.newline);292					}293					buf.push_str(options.newline);294					buf.push_str(cur_padding);295				}296				Minify | ToString => {}297			}298299			buf.push(']');300		}301		Val::Obj(obj) => {302			obj.run_assertions()?;303			buf.push('{');304305			let old_len = cur_padding.len();306			cur_padding.push_str(&options.padding);307308			let mut had_fields = false;309			for (i, (key, value)) in obj310				.iter(311					#[cfg(feature = "exp-preserve-order")]312					options.preserve_order,313				)314				.enumerate()315			{316				had_fields = true;317				let value = value.with_description(|| format!("field <{key}> evaluation"))?;318319				if i != 0 {320					buf.push(',');321				}322				match mtype {323					Manifest | Std => {324						buf.push_str(options.newline);325						buf.push_str(cur_padding);326					}327					ToString if i != 0 => buf.push(' '),328					Minify | ToString => {}329				}330331				escape_string_json_buf(&key, buf);332				buf.push_str(options.key_val_sep);333				in_description_frame(334					|| format!("field <{key}> manifestification"),335					|| manifest_json_ex_buf(&value, buf, cur_padding, options),336				)?;337			}338339			cur_padding.truncate(old_len);340341			match mtype {342				Manifest | ToString if !had_fields => {343					// Empty object as "{ }"344					buf.push(' ');345				}346				Manifest => {347					buf.push_str(options.newline);348					buf.push_str(cur_padding);349				}350				Std => {351					if !had_fields {352						// Stdlib formats empty object as "{\n\n}"353						buf.push_str(options.newline);354					}355					buf.push_str(options.newline);356					buf.push_str(cur_padding);357				}358				Minify | ToString => {}359			}360361			buf.push('}');362		}363		Val::Func(_) => bail!("tried to manifest function"),364	}365	Ok(())366}367368impl ManifestFormat for JsonFormat<'_> {369	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {370		manifest_json_ex_buf(&val, buf, &mut String::new(), self)371	}372}373374/// Same as [`JsonFormat`] with pre-set options, but top-level string is serialized as-is,375/// without quoting.376pub struct ToStringFormat;377impl ManifestFormat for ToStringFormat {378	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {379		const JSON_TO_STRING: JsonFormat = JsonFormat::std_to_string_helper();380		if let Some(str) = val.as_str() {381			out.push_str(&str);382			return Ok(());383		}384		#[cfg(feature = "exp-bigint")]385		if let Some(int) = val.as_bigint() {386			out.push_str(&int.to_str_radix(10));387			return Ok(());388		}389		JSON_TO_STRING.manifest_buf(val, out)390	}391	fn file_trailing_newline(&self) -> bool {392		false393	}394}395pub struct StringFormat;396impl ManifestFormat for StringFormat {397	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {398		let Val::Str(s) = val else {399			bail!(400				"output should be string for string manifest format, got {}",401				val.value_type()402			)403		};404		write!(out, "{s}").unwrap();405		Ok(())406	}407	fn file_trailing_newline(&self) -> bool {408		false409	}410}411412pub struct YamlStreamFormat<I> {413	inner: I,414	c_document_end: bool,415	end_newline: bool,416}417impl<I> YamlStreamFormat<I> {418	pub fn std_yaml_stream(inner: I, c_document_end: bool) -> Self {419		Self {420			inner,421			c_document_end,422			// Stdlib format always inserts useless newline at the end423			end_newline: true,424		}425	}426	pub fn cli(inner: I) -> Self {427		Self {428			inner,429			c_document_end: true,430			end_newline: false,431		}432	}433}434impl<I: ManifestFormat> ManifestFormat for YamlStreamFormat<I> {435	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {436		let Val::Arr(arr) = val else {437			bail!(438				"output should be array for yaml stream format, got {}",439				val.value_type()440			)441		};442		for (i, v) in arr.iter().enumerate() {443			if i != 0 {444				out.push('\n');445			}446			let v = v.with_description(|| format!("elem <{i}> evaluation"))?;447			out.push_str("---\n");448			in_description_frame(449				|| format!("elem <{i}> manifestification"),450				|| self.inner.manifest_buf(v, out),451			)?;452		}453		if self.c_document_end {454			out.push('\n');455			out.push_str("...");456		}457		if self.end_newline {458			out.push('\n');459		}460		Ok(())461	}462}463464pub fn escape_string_json(s: &str) -> String {465	let mut buf = String::new();466	escape_string_json_buf(s, &mut buf);467	buf468}469470// Json string encoding was borrowed from https://github.com/serde-rs/json471472const BB: u8 = b'b'; // \x08473const TT: u8 = b't'; // \x09474const NN: u8 = b'n'; // \x0A475const FF: u8 = b'f'; // \x0C476const RR: u8 = b'r'; // \x0D477const QU: u8 = b'"'; // \x22478const BS: u8 = b'\\'; // \x5C479const UU: u8 = b'u'; // \x00...\x1F except the ones above480const __: u8 = 0;481482// Lookup table of escape sequences. A value of b'x' at index i means that byte483// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped.484static ESCAPE: [u8; 256] = [485	//   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F486	UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, // 0487	UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1488	__, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2489	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3490	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4491	__, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5492	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6493	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7494	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8495	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9496	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A497	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B498	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C499	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D500	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E501	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F502];503504pub fn escape_string_json_buf(value: &str, buf: &mut String) {505	buf.reserve_exact(value.len() + 2);506	escape_string_json_buf_raw(value, buf);507}508509fn escape_string_json_buf_raw(value: &str, buf: &mut String) {510	// Safety: we only write correct utf-8 in this function511	let buf: &mut Vec<u8> = unsafe { &mut *ptr::from_mut(buf).cast::<Vec<u8>>() };512	let bytes = value.as_bytes();513514	let mut start = 0;515516	for (i, &byte) in bytes.iter().enumerate() {517		let escape = ESCAPE[byte as usize];518		if escape == __ {519			continue;520		}521522		if start < i {523			buf.extend_from_slice(&bytes[start..i]);524		}525		start = i + 1;526527		match escape {528			self::BB | self::TT | self::NN | self::FF | self::RR | self::QU | self::BS => {529				buf.extend_from_slice(&[b'\\', escape]);530			}531			self::UU => {532				static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef";533				let bytes = &[534					b'\\',535					b'u',536					b'0',537					b'0',538					HEX_DIGITS[(byte >> 4) as usize],539					HEX_DIGITS[(byte & 0xF) as usize],540				];541				buf.extend_from_slice(bytes);542			}543			_ => unreachable!(),544		}545	}546547	if start == bytes.len() {548		return;549	}550551	buf.extend_from_slice(&bytes[start..]);552}
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