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

difftreelog

refactor drop im-rc

tnwykoutYaroslav Bolyukin2026-05-05parent: #5eebad4.patch.diff
in: master
Not worth it. It gives some improvements in some benchmarks, but at the
cost of much complex memory layout, I do not consider that a win

5 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -163,15 +163,6 @@
 checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3"
 
 [[package]]
-name = "bitmaps"
-version = "2.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "031043d04099746d8db04daf1fa424b2bc8bd69d92b25962dcde24da39ab64a2"
-dependencies = [
- "typenum",
-]
-
-[[package]]
 name = "block-buffer"
 version = "0.12.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -702,21 +693,6 @@
 checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954"
 
 [[package]]
-name = "im-rc"
-version = "15.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "af1955a75fa080c677d3972822ec4bad316169ab1cfc6c257a942c2265dbe5fe"
-dependencies = [
- "bitmaps",
- "rand_core 0.6.4",
- "rand_xoshiro",
- "refpool",
- "sized-chunks",
- "typenum",
- "version_check",
-]
-
-[[package]]
 name = "indexmap"
 version = "2.14.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -829,7 +805,6 @@
  "drop_bomb",
  "educe",
  "hi-doc",
- "im-rc",
  "insta",
  "jrsonnet-gcmodule",
  "jrsonnet-interner",
@@ -881,7 +856,6 @@
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "21dd97b40cbfb2043094219f95d96519858ba1aee4e8260eb048a1774832a517"
 dependencies = [
- "im-rc",
  "jrsonnet-gcmodule-derive",
 ]
 
@@ -1356,7 +1330,7 @@
 checksum = "44c5af06bb1b7d3216d91932aed5265164bf384dc89cd6ba05cf59a35f5f76ea"
 dependencies = [
  "rand_chacha",
- "rand_core 0.9.5",
+ "rand_core",
 ]
 
 [[package]]
@@ -1366,14 +1340,8 @@
 checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
 dependencies = [
  "ppv-lite86",
- "rand_core 0.9.5",
+ "rand_core",
 ]
-
-[[package]]
-name = "rand_core"
-version = "0.6.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
 
 [[package]]
 name = "rand_core"
@@ -1382,15 +1350,6 @@
 checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"
 dependencies = [
  "getrandom 0.3.4",
-]
-
-[[package]]
-name = "rand_xoshiro"
-version = "0.6.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa"
-dependencies = [
- "rand_core 0.6.4",
 ]
 
 [[package]]
@@ -1432,12 +1391,6 @@
 ]
 
 [[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"
@@ -1639,17 +1592,6 @@
 version = "2.7.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa"
-
-[[package]]
-name = "sized-chunks"
-version = "0.6.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "16d69225bde7a69b235da73377861095455d298f2b970996eec25ddbb42b3d1e"
-dependencies = [
- "bitmaps",
- "refpool",
- "typenum",
-]
 
 [[package]]
 name = "smallvec"
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.4", features = ["im-rc"] }
+jrsonnet-gcmodule = { version = "0.4.4" }
 # 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.
modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -76,7 +76,6 @@
   "Hash",
   "PartialEq",
 ] }
-im-rc = { version = "15.1.0", features = ["pool"] }
 smallvec = "1.15.1"
 drop_bomb.workspace = true
 
modifiedcrates/jrsonnet-evaluator/src/obj/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/obj/mod.rs
1use std::{2	any::Any,3	cell::{Cell, RefCell},4	clone::Clone,5	cmp::Reverse,6	collections::hash_map::Entry,7	fmt::{self, Debug},8	hash::{Hash, Hasher},9	num::Saturating,10	ops::ControlFlow,11};1213use educe::Educe;14use im_rc::{Vector, vector};15use jrsonnet_gcmodule::{Acyclic, Cc, Trace, Weak, cc_dyn};16use jrsonnet_interner::IStr;17use jrsonnet_ir::Span;18use rustc_hash::{FxHashMap, FxHashSet};1920mod oop;2122pub use jrsonnet_ir::Visibility;23pub use oop::ObjValueBuilder;2425use crate::{26	CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,27	arr::{PickObjectKeyValues, PickObjectValues},28	bail,29	error::{ErrorKind::*, suggest_object_fields},30	evaluate::operator::evaluate_add_op,31	identity_hash,32	val::{ArrValue, ThunkValue},33};3435#[cfg(not(feature = "exp-preserve-order"))]36pub mod ordering {37	#![allow(38		// This module works as stub for preserve-order feature39		clippy::unused_self,40	)]4142	use jrsonnet_gcmodule::Trace;4344	#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]45	pub struct FieldIndex(());46	impl FieldIndex {47		pub fn absolute(_v: u32) -> Self {48			Self(())49		}50		#[must_use]51		pub const fn next(self) -> Self {52			Self(())53		}54	}5556	#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]57	pub struct SuperDepth(());58	impl SuperDepth {59		pub(super) fn deepen(self) {}60	}61}6263#[cfg(feature = "exp-preserve-order")]64pub mod ordering {65	use jrsonnet_gcmodule::Trace;6667	#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]68	pub struct FieldIndex(u32);69	impl FieldIndex {70		pub fn absolute(v: u32) -> Self {71			Self(v)72		}73		#[must_use]74		pub fn next(self) -> Self {75			Self(self.0 + 1)76		}77	}7879	#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]80	pub struct SuperDepth(u32);81	impl SuperDepth {82		pub(super) fn deepen(&mut self) {83			self.0 += 1;84		}85	}86}8788use ordering::{FieldIndex, SuperDepth};8990#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]91pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);92impl FieldSortKey {93	pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {94		Self(Reverse(depth), index)95	}96}9798// 0 - add99//  12 - visibility100#[derive(Clone, Copy)]101pub struct ObjFieldFlags(u8);102impl ObjFieldFlags {103	fn new(add: bool, visibility: Visibility) -> Self {104		let mut v = 0;105		if add {106			v |= 1;107		}108		v |= match visibility {109			Visibility::Normal => 0b000,110			Visibility::Hidden => 0b010,111			Visibility::Unhide => 0b100,112		};113		Self(v)114	}115	pub fn add(&self) -> bool {116		self.0 & 1 != 0117	}118	pub fn visibility(&self) -> Visibility {119		match (self.0 & 0b110) >> 1 {120			0b00 => Visibility::Normal,121			0b01 => Visibility::Hidden,122			0b10 => Visibility::Unhide,123			_ => unreachable!(),124		}125	}126}127impl Debug for ObjFieldFlags {128	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {129		f.debug_struct("ObjFieldFlags")130			.field("add", &self.add())131			.field("visibility", &self.visibility())132			.finish()133	}134}135136#[allow(clippy::module_name_repetitions)]137#[derive(Debug, Trace)]138pub struct ObjMember {139	#[trace(skip)]140	flags: ObjFieldFlags,141	original_index: FieldIndex,142	pub invoke: MaybeUnbound,143	pub location: Option<Span>,144}145146cc_dyn!(CcObjectAssertion, ObjectAssertion);147pub trait ObjectAssertion: Trace {148	fn run(&self, sup_this: SupThis) -> Result<()>;149}150151// Field => This152153#[derive(Trace, Debug)]154enum CacheValue {155	Cached(Result<Option<Val>>),156	Pending,157}158159pub type EnumFieldsHandler<'a> =160	dyn FnMut(SuperDepth, FieldIndex, IStr, EnumFields) -> ControlFlow<()> + 'a;161162#[derive(Debug)]163pub enum EnumFields {164	Normal(Visibility),165	Omit(Skip),166}167168#[derive(Trace, Clone)]169pub enum GetFor {170	// Return value171	Final(Val),172	// Continue iterating over cores, add current value to sum stack173	SuperPlus(Val),174	// Ignore the field value, stop at this layer instead175	Omit(#[trace(skip)] Skip),176	NotFound,177}178179#[derive(Acyclic, Clone)]180pub enum FieldVisibility {181	Found(Visibility),182	Omit(Skip),183	NotFound,184}185186#[derive(Acyclic, Clone)]187pub enum HasFieldIncludeHidden {188	Exists,189	NotFound,190	Omit(Skip),191}192193type Skip = Saturating<usize>;194195pub trait ObjectCore: Trace + Any + Debug {196	// If callback returns false, iteration stops, and this call returns false.197	fn enum_fields_core(198		&self,199		super_depth: &mut SuperDepth,200		handler: &mut EnumFieldsHandler<'_>,201	) -> bool;202203	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden;204205	fn get_for_core(&self, key: IStr, sup_this: SupThis, omit_only: bool) -> Result<GetFor>;206	fn field_visibility_core(&self, field: IStr) -> FieldVisibility;207208	fn run_assertions_core(&self, sup_this: SupThis) -> Result<()>;209}210211#[derive(Clone, Trace)]212pub struct WeakObjValue(#[trace(skip)] Weak<ObjValueInner>);213impl Debug for WeakObjValue {214	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {215		f.debug_tuple("WeakObjValue").finish()216	}217}218219impl PartialEq for WeakObjValue {220	fn eq(&self, other: &Self) -> bool {221		Weak::ptr_eq(&self.0, &other.0)222	}223}224225impl Eq for WeakObjValue {}226impl Hash for WeakObjValue {227	fn hash<H: Hasher>(&self, hasher: &mut H) {228		// Safety: usize is POD229		let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };230		hasher.write_usize(addr);231	}232}233234cc_dyn!(235	#[derive(Clone, Debug)]236	CcObjectCore, ObjectCore,237	pub fn new() {...}238);239240#[derive(Trace, Educe)]241#[educe(Debug)]242struct ObjValueInner {243	cores: Vector<CcObjectCore>,244	assertions_ran: Cell<bool>,245	has_assertions: bool,246	value_cache: RefCell<FxHashMap<(IStr, CoreIdx), CacheValue>>,247}248249thread_local! {250	static RUNNING_ASSERTIONS: RefCell<FxHashSet<ObjValue>> = RefCell::default();251}252fn is_asserting(obj: &ObjValue) -> bool {253	RUNNING_ASSERTIONS.with_borrow(|v| v.contains(obj))254}255/// Returns false if already asserting256fn start_asserting(obj: &ObjValue) -> bool {257	RUNNING_ASSERTIONS.with_borrow_mut(|v| v.insert(obj.clone()))258}259fn finish_asserting(obj: &ObjValue) {260	RUNNING_ASSERTIONS.with_borrow_mut(|v| {261		let r = v.remove(obj);262		debug_assert!(263			r,264			"finish_asserting was called before start_asserting or twice"265		);266	});267}268269thread_local! {270	static EMPTY_OBJ: ObjValue = ObjValue(Cc::new(ObjValueInner {271		cores: vector![],272		assertions_ran: Cell::new(true),273		has_assertions: false,274		value_cache: RefCell::default(),275	}))276}277278#[allow(clippy::module_name_repetitions)]279#[derive(Clone, Trace, Debug, Educe)]280#[educe(PartialEq, Hash, Eq)]281pub struct ObjValue(282	#[educe(PartialEq(method(Cc::ptr_eq)), Hash(method(identity_hash)))] Cc<ObjValueInner>,283);284285impl ObjValue {286	pub fn empty() -> Self {287		EMPTY_OBJ.with(Clone::clone)288	}289	pub fn is_empty(&self) -> bool {290		self.0.cores.is_empty() || self.len() == 0291	}292}293294#[derive(Trace, Debug)]295pub(crate) struct StandaloneSuperCore {296	sup: CoreIdx,297	this: ObjValue,298}299impl ObjectCore for StandaloneSuperCore {300	fn enum_fields_core(301		&self,302		super_depth: &mut SuperDepth,303		handler: &mut EnumFieldsHandler<'_>,304	) -> bool {305		self.this.enum_fields_idx(super_depth, handler, self.sup)306	}307308	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {309		if self.this.has_field_include_hidden_idx(name, self.sup) {310			HasFieldIncludeHidden::Exists311		} else {312			HasFieldIncludeHidden::NotFound313		}314	}315316	fn get_for_core(&self, key: IStr, _sup_this: SupThis, omit_only: bool) -> Result<GetFor> {317		if omit_only {318			return Ok(GetFor::NotFound);319		}320		let v = self.this.get_idx(key, self.sup)?;321		Ok(v.map_or(GetFor::NotFound, GetFor::Final))322	}323324	fn field_visibility_core(&self, field: IStr) -> FieldVisibility {325		self.this326			.field_visibility_idx(field, self.sup)327			.map_or(FieldVisibility::NotFound, FieldVisibility::Found)328	}329330	fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {331		self.this.run_assertions()332	}333}334335#[derive(Debug, Acyclic)]336struct OmitFieldsCore {337	omit: FxHashSet<IStr>,338	prev_layers: usize,339}340impl ObjectCore for OmitFieldsCore {341	fn enum_fields_core(342		&self,343		super_depth: &mut SuperDepth,344		handler: &mut EnumFieldsHandler<'_>,345	) -> bool {346		let mut fi = FieldIndex::default();347		for f in &self.omit {348			if handler(349				*super_depth,350				fi,351				f.clone(),352				EnumFields::Omit(Saturating(self.prev_layers)),353			) == ControlFlow::Break(())354			{355				return false;356			}357			fi = fi.next();358		}359		true360	}361362	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {363		if self.omit.contains(&name) {364			return HasFieldIncludeHidden::Omit(Saturating(self.prev_layers));365		}366		HasFieldIncludeHidden::NotFound367	}368369	fn get_for_core(&self, key: IStr, _sup_this: SupThis, _omit_only: bool) -> Result<GetFor> {370		if self.omit.contains(&key) {371			return Ok(GetFor::Omit(Saturating(self.prev_layers)));372		}373		Ok(GetFor::NotFound)374	}375376	fn field_visibility_core(&self, field: IStr) -> FieldVisibility {377		if self.omit.contains(&field) {378			return FieldVisibility::Omit(Saturating(self.prev_layers));379		}380		FieldVisibility::NotFound381	}382383	fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {384		Ok(())385	}386}387388#[derive(Hash, PartialEq, Eq, Trace, Clone, Copy, Debug)]389struct CoreIdx {390	idx: usize,391}392impl CoreIdx {393	fn super_exists(self) -> bool {394		self.idx != 0395	}396}397#[derive(Trace, Clone, PartialEq, Eq, Hash, Debug)]398pub struct SupThis {399	sup: CoreIdx,400	this: ObjValue,401}402impl SupThis {403	/// Create a `SupThis` for a freshly constructed object (no super).404	pub fn new(this: ObjValue) -> Self {405		Self {406			sup: CoreIdx {407				idx: this.0.cores.len(),408			},409			this,410		}411	}412	pub fn has_super(&self) -> bool {413		self.sup.super_exists()414	}415	/// Implementation of `"field" in super` operation,416	/// works faster than standalone super path.417	///418	/// In case of no `super` existence, returns false.419	pub fn field_in_super(&self, field: IStr) -> bool {420		self.this.has_field_include_hidden_idx(field, self.sup)421	}422	/// Implementation of `super.field` operation,423	/// works faster than standalone super path.424	///425	/// In case of no `super` existence, returns `NoSuperFound`426	pub fn get_super(&self, field: IStr) -> Result<Option<Val>> {427		if !self.sup.super_exists() {428			bail!(NoSuperFound);429		}430		self.this.get_idx(field, self.sup)431	}432	/// `super` with `self` overriden for top-level lookups.433	/// Exists when super appears outside of `super.field`/`"field" in super` expressions434	/// Exclusive to jrsonnet.435	///436	/// Might return `NoSuperFound` error.437	pub fn standalone_super(&self) -> Result<ObjValue> {438		if !self.sup.super_exists() {439			bail!(NoSuperFound)440		}441		let mut out = ObjValue::builder();442		out.extend_with_core(StandaloneSuperCore {443			sup: self.sup,444			this: self.this.clone(),445		});446		Ok(out.build())447	}448	pub fn this(&self) -> &ObjValue {449		&self.this450	}451	pub fn downgrade(self) -> WeakSupThis {452		WeakSupThis {453			sup: self.sup,454			this: self.this.downgrade(),455		}456	}457}458#[derive(Trace, PartialEq, Eq, Hash, Debug)]459pub struct WeakSupThis {460	sup: CoreIdx,461	this: WeakObjValue,462}463464impl ObjValue {465	pub fn builder() -> ObjValueBuilder {466		ObjValueBuilder::new()467	}468	pub fn builder_with_capacity(capacity: usize) -> ObjValueBuilder {469		ObjValueBuilder::with_capacity(capacity)470	}471	pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {472		let mut out = ObjValueBuilder::with_capacity(1);473		out.with_super(self);474		let mut member = out.field(key);475		if value.flags.add() {476			member = member.add();477		}478		if let Some(loc) = value.location {479			member = member.with_location(loc);480		}481		let _ = member482			.with_visibility(value.flags.visibility())483			.binding(value.invoke);484		out.build()485	}486	pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {487		ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())488	}489490	pub fn extend(&mut self) -> ObjValueBuilder {491		let mut out = ObjValueBuilder::new();492		out.with_super(self.clone());493		out494	}495496	#[must_use]497	pub fn extend_from(&self, sup: Self) -> Self {498		let cores = sup.0.cores.clone() + self.0.cores.clone();499		let has_assertions = sup.0.has_assertions || self.0.has_assertions;500		ObjValue(Cc::new(ObjValueInner {501			cores,502			value_cache: RefCell::default(),503			assertions_ran: Cell::new(!has_assertions),504			has_assertions,505		}))506	}507	// #[must_use]508	// pub fn with_this(&self, this: Self) -> Self {509	// 	self.0.with_this(self.clone(), this)510	// }511	/// Returns amount of visible object fields512	/// If object only contains hidden fields - may return zero.513	pub fn len(&self) -> u32 {514		self.fields_visibility()515			.values()516			.filter(|d| d.visible())517			.count() as u32518	}519	/// For each field, calls callback.520	/// If callback returns false - ends iteration prematurely.521	///522	/// Returns false if ended prematurely523	pub fn enum_fields(&self, handler: &mut EnumFieldsHandler<'_>) -> bool {524		let mut super_depth = SuperDepth::default();525		self.enum_fields_idx(526			&mut super_depth,527			handler,528			CoreIdx {529				idx: self.0.cores.len(),530			},531		)532	}533534	fn iter_cores(&self, idx: CoreIdx) -> impl Iterator<Item = &CcObjectCore> {535		self.0.cores.iter().take(idx.idx).rev()536	}537	fn iter_cores_enumerate(&self, idx: CoreIdx) -> impl Iterator<Item = (CoreIdx, &CcObjectCore)> {538		self.0539			.cores540			.iter()541			.take(idx.idx)542			.enumerate()543			.rev()544			.map(|(idx, o)| (CoreIdx { idx }, o))545	}546547	fn enum_fields_idx(548		&self,549		super_depth: &mut SuperDepth,550		handler: &mut EnumFieldsHandler<'_>,551		idx: CoreIdx,552	) -> bool {553		for core in self.iter_cores(idx) {554			if !core.0.enum_fields_core(super_depth, handler) {555				return false;556			}557			super_depth.deepen();558		}559		true560	}561562	pub fn has_field_include_hidden(&self, name: IStr) -> bool {563		self.has_field_include_hidden_idx(564			name,565			CoreIdx {566				idx: self.0.cores.len(),567			},568		)569	}570	fn has_field_include_hidden_idx(&self, name: IStr, core: CoreIdx) -> bool {571		let mut skip = Saturating(0usize);572		for ele in self.iter_cores(core) {573			match ele.0.has_field_include_hidden_core(name.clone()) {574				HasFieldIncludeHidden::Exists => {575					if skip.0 == 0 {576						return true;577					}578				}579				HasFieldIncludeHidden::Omit(new_skip) => {580					// +1 including this core581					skip = skip.max(new_skip + Saturating(1));582				}583				HasFieldIncludeHidden::NotFound => {}584			}585			skip -= 1;586		}587		false588	}589	pub fn has_field(&self, name: IStr) -> bool {590		match self.field_visibility(name) {591			Some(Visibility::Unhide | Visibility::Normal) => true,592			Some(Visibility::Hidden) | None => false,593		}594	}595	pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {596		if include_hidden {597			self.has_field_include_hidden(name)598		} else {599			self.has_field(name)600		}601	}602	pub fn get(&self, key: IStr) -> Result<Option<Val>> {603		self.get_idx(604			key,605			CoreIdx {606				idx: self.0.cores.len(),607			},608		)609	}610611	fn get_idx(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {612		let cache_key = (key.clone(), core);613		{614			let mut cache = self.0.value_cache.borrow_mut();615			// entry_ref candidate?616			match cache.entry(cache_key.clone()) {617				Entry::Occupied(v) => match v.get() {618					CacheValue::Cached(v) => return v.clone(),619					CacheValue::Pending => {620						if !is_asserting(self) {621							bail!(InfiniteRecursionDetected);622						}623					}624				},625				Entry::Vacant(v) => {626					v.insert(CacheValue::Pending);627				}628			};629		}630		let result = self.get_idx_uncached(key, core);631		{632			let mut cache = self.0.value_cache.borrow_mut();633			cache.insert(cache_key, CacheValue::Cached(result.clone()));634		}635		result636	}637	fn get_idx_uncached(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {638		self.run_assertions()?;639		let mut first_add = None;640		let mut add_stack: Vec<Val> = Vec::new();641		let mut skip = Saturating(0);642		for (sup, core) in self.iter_cores_enumerate(core) {643			let sup_this = SupThis {644				sup,645				this: self.clone(),646			};647			match core.0.get_for_core(key.clone(), sup_this, skip.0 != 0)? {648				GetFor::Final(val) if first_add.is_none() => {649					if skip.0 == 0 {650						return Ok(Some(val));651					}652				}653				GetFor::Final(val) => {654					if skip.0 == 0 {655						add_stack.push(val);656						break;657					}658				}659				GetFor::SuperPlus(val) => {660					if skip.0 == 0 {661						if first_add.is_none() {662							first_add = Some(val);663						} else {664							add_stack.push(val);665						}666					}667				}668				GetFor::Omit(new_skip) => {669					skip = skip.max(new_skip + Saturating(1));670				}671				GetFor::NotFound => {}672			}673			skip -= 1;674		}675		let Some(first) = first_add else {676			if add_stack.is_empty() {677				return Ok(None);678			}679			return Ok(Some(add_stack.pop().expect("single element on stack")));680		};681		if add_stack.is_empty() {682			return Ok(Some(first));683		}684		add_stack.insert(0, first);685		let mut values = add_stack.into_iter().rev();686		let init = values.next().expect("at least 2 elements");687688		values689			.try_fold(init, |a, b| evaluate_add_op(&a, &b))690			.map(Some)691	}692693	pub fn get_or_bail(&self, key: IStr) -> Result<Val> {694		let Some(value) = self.get(key.clone())? else {695			let suggestions = suggest_object_fields(self, key.clone());696			bail!(NoSuchField(key, suggestions))697		};698		Ok(value)699	}700701	fn field_visibility(&self, field: IStr) -> Option<Visibility> {702		self.field_visibility_idx(703			field,704			CoreIdx {705				idx: self.0.cores.len(),706			},707		)708	}709	fn field_visibility_idx(&self, field: IStr, core: CoreIdx) -> Option<Visibility> {710		let mut exists = false;711		let mut skip = Saturating(0usize);712		for ele in self.iter_cores(core) {713			let vis = ele.0.field_visibility_core(field.clone());714			match vis {715				FieldVisibility::Found(vis @ (Visibility::Unhide | Visibility::Hidden)) => {716					if skip.0 == 0 {717						return Some(vis);718					}719				}720				FieldVisibility::Found(Visibility::Normal) => {721					if skip.0 == 0 {722						exists = true;723					}724				}725				FieldVisibility::NotFound => {}726				FieldVisibility::Omit(new_skip) => {727					// +1 including this core728					skip = skip.max(new_skip + Saturating(1));729				}730			}731			skip -= 1;732		}733		exists.then_some(Visibility::Normal)734	}735736	pub fn run_assertions(&self) -> Result<()> {737		if self.0.assertions_ran.get() {738			return Ok(());739		}740		if !start_asserting(self) {741			return Ok(());742		}743		for (idx, ele) in self.0.cores.iter().enumerate() {744			let sup_this = SupThis {745				sup: CoreIdx { idx },746				this: self.clone(),747			};748			ele.0.run_assertions_core(sup_this).inspect_err(|_e| {749				finish_asserting(self);750			})?;751		}752		finish_asserting(self);753		self.0.assertions_ran.set(true);754		Ok(())755	}756757	pub fn iter(758		&self,759		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,760	) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {761		let fields = self.fields(762			#[cfg(feature = "exp-preserve-order")]763			preserve_order,764		);765		fields.into_iter().map(|field| {766			(767				field.clone(),768				self.get(field)769					.map(|opt| opt.expect("iterating over keys, field exists")),770			)771		})772	}773	pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {774		#[derive(Trace)]775		struct ObjFieldThunk {776			obj: ObjValue,777			key: IStr,778		}779		impl ThunkValue for ObjFieldThunk {780			type Output = Val;781782			fn get(&self) -> Result<Self::Output> {783				self.obj784					.get(self.key.clone())785					.transpose()786					.expect("field existence checked")787			}788		}789790		if !self.has_field_ex(key.clone(), true) {791			return None;792		}793794		Some(Thunk::new(ObjFieldThunk {795			obj: self.clone(),796			key,797		}))798	}799	pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {800		#[derive(Trace)]801		struct ObjFieldThunk {802			obj: ObjValue,803			key: IStr,804		}805		impl ThunkValue for ObjFieldThunk {806			type Output = Val;807808			fn get(&self) -> Result<Self::Output> {809				self.obj.get_or_bail(self.key.clone())810			}811		}812813		Thunk::new(ObjFieldThunk {814			obj: self.clone(),815			key,816		})817	}818819	#[allow(dead_code, reason = "used in object ...rest destructuring")]820	pub(crate) fn as_standalone(&self) -> StandaloneSuperCore {821		StandaloneSuperCore {822			sup: CoreIdx {823				idx: self.0.cores.len(),824			},825			this: self.clone(),826		}827	}828	pub fn ptr_eq(a: &Self, b: &Self) -> bool {829		Cc::ptr_eq(&a.0, &b.0)830	}831	pub fn downgrade(self) -> WeakObjValue {832		WeakObjValue(self.0.downgrade())833	}834}835836#[derive(Debug)]837struct FieldVisibilityData {838	omitted_until: Saturating<usize>,839	exists_visible: Option<Visibility>,840	#[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]841	key: FieldSortKey,842}843impl FieldVisibilityData {844	fn visible(&self) -> bool {845		self.exists_visible846			.expect("non-existing fields shall be dropped at the end of fn fields_visibility()")847			.is_visible()848	}849	#[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]850	fn sort_key(&self) -> FieldSortKey {851		self.key852	}853}854855impl ObjValue {856	fn fields_visibility(&self) -> FxHashMap<IStr, FieldVisibilityData> {857		let mut out = FxHashMap::default();858859		let mut super_depth = SuperDepth::default();860		let mut omit_index = Saturating(0);861		for core in self.0.cores.iter().rev() {862			core.0863				.enum_fields_core(&mut super_depth, &mut |depth, index, name, visibility| {864					let entry = out.entry(name);865					let data = entry.or_insert_with(|| FieldVisibilityData {866						exists_visible: None,867						key: FieldSortKey::new(depth, index),868						omitted_until: omit_index,869					});870					match visibility {871						EnumFields::Omit(new_skip) => {872							// +1 including this core873							data.omitted_until = data874								.omitted_until875								.max(omit_index + new_skip + Saturating(1));876						}877						EnumFields::Normal(Visibility::Normal) => {878							if data.omitted_until <= omit_index && data.exists_visible.is_none() {879								data.exists_visible = Some(Visibility::Normal);880							}881						}882						EnumFields::Normal(Visibility::Hidden) => {883							if data.omitted_until <= omit_index {884								data.exists_visible = Some(match data.exists_visible {885									// We're iterating in reverse, later unhide is preserved886									Some(Visibility::Unhide) => Visibility::Unhide,887									_ => Visibility::Hidden,888								});889							}890						}891						EnumFields::Normal(Visibility::Unhide) => {892							if data.omitted_until <= omit_index {893								data.exists_visible = Some(match data.exists_visible {894									// We're iterating in reverse, later hide is preserved895									Some(Visibility::Hidden) => Visibility::Hidden,896									_ => Visibility::Unhide,897								});898							}899						}900					}901					ControlFlow::Continue(())902				});903904			super_depth.deepen();905			omit_index += 1;906		}907908		out.retain(|_, v| v.exists_visible.is_some());909910		out911	}912	pub fn fields_ex(913		&self,914		include_hidden: bool,915		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,916	) -> Vec<IStr> {917		#[cfg(feature = "exp-preserve-order")]918		if preserve_order {919			let (mut fields, mut keys): (Vec<_>, Vec<_>) = self920				.fields_visibility()921				.into_iter()922				.filter(|(_, d)| include_hidden || d.visible())923				.enumerate()924				.map(|(idx, (k, d))| (k, (d.sort_key(), idx)))925				.unzip();926			keys.sort_unstable_by_key(|v| v.0);927			// Reorder in-place by resulting indexes928			for i in 0..fields.len() {929				let x = fields[i].clone();930				let mut j = i;931				loop {932					let k = keys[j].1;933					keys[j].1 = j;934					if k == i {935						break;936					}937					fields[j] = fields[k].clone();938					j = k;939				}940				fields[j] = x;941			}942			return fields;943		}944945		let mut fields: Vec<_> = self946			.fields_visibility()947			.into_iter()948			.filter(|(_, d)| include_hidden || d.visible())949			.map(|(k, _)| k)950			.collect();951		fields.sort_unstable();952		fields953	}954	pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {955		self.fields_ex(956			false,957			#[cfg(feature = "exp-preserve-order")]958			preserve_order,959		)960	}961	pub fn values_ex(962		&self,963		include_hidden: bool,964		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,965	) -> ArrValue {966		ArrValue::new(PickObjectValues::new(967			self.clone(),968			self.fields_ex(969				include_hidden,970				#[cfg(feature = "exp-preserve-order")]971				preserve_order,972			),973		))974	}975	pub fn values(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> ArrValue {976		self.values_ex(977			false,978			#[cfg(feature = "exp-preserve-order")]979			preserve_order,980		)981	}982	pub fn key_values_ex(983		&self,984		include_hidden: bool,985		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,986	) -> ArrValue {987		ArrValue::new(PickObjectKeyValues::new(988			self.clone(),989			self.fields_ex(990				include_hidden,991				#[cfg(feature = "exp-preserve-order")]992				preserve_order,993			),994		))995	}996	pub fn key_values(997		&self,998		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,999	) -> ArrValue {1000		self.key_values_ex(1001			false,1002			#[cfg(feature = "exp-preserve-order")]1003			preserve_order,1004		)1005	}1006}10071008#[allow(clippy::module_name_repetitions)]1009#[must_use = "value not added unless binding() was called"]1010pub struct ObjMemberBuilder<Kind> {1011	kind: Kind,1012	name: IStr,1013	add: bool,1014	visibility: Visibility,1015	original_index: FieldIndex,1016	location: Option<Span>,1017}10181019#[allow(clippy::missing_const_for_fn)]1020impl<Kind> ObjMemberBuilder<Kind> {1021	pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {1022		Self {1023			kind,1024			name,1025			original_index,1026			add: false,1027			visibility: Visibility::Normal,1028			location: None,1029		}1030	}10311032	pub const fn with_add(mut self, add: bool) -> Self {1033		self.add = add;1034		self1035	}1036	pub fn add(self) -> Self {1037		self.with_add(true)1038	}1039	pub fn with_visibility(mut self, visibility: Visibility) -> Self {1040		self.visibility = visibility;1041		self1042	}1043	pub fn hide(self) -> Self {1044		self.with_visibility(Visibility::Hidden)1045	}1046	pub fn with_location(mut self, location: Span) -> Self {1047		self.location = Some(location);1048		self1049	}1050	fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {1051		(1052			self.kind,1053			self.name,1054			ObjMember {1055				flags: ObjFieldFlags::new(self.add, self.visibility),1056				original_index: self.original_index,1057				invoke: binding,1058				location: self.location,1059			},1060		)1061	}1062}10631064pub struct ExtendBuilder<'v>(&'v mut ObjValue);1065impl ObjMemberBuilder<ExtendBuilder<'_>> {1066	pub fn value(self, value: impl Into<Val>) {1067		self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));1068	}1069	pub fn bindable(self, bindable: impl Unbound<Bound = Val>) {1070		self.binding(MaybeUnbound::Unbound(CcUnbound::new(bindable)));1071	}1072	pub fn binding(self, binding: MaybeUnbound) {1073		let (receiver, name, member) = self.build_member(binding);1074		let new = receiver.0.clone();1075		*receiver.0 = new.extend_with_raw_member(name, member);1076	}1077}
after · crates/jrsonnet-evaluator/src/obj/mod.rs
1use std::{2	any::Any,3	cell::{Cell, RefCell},4	clone::Clone,5	cmp::Reverse,6	collections::hash_map::Entry,7	fmt::{self, Debug},8	hash::{Hash, Hasher},9	num::Saturating,10	ops::ControlFlow,11};1213use educe::Educe;14use jrsonnet_gcmodule::{Acyclic, Cc, Trace, Weak, cc_dyn};15use jrsonnet_interner::IStr;16use jrsonnet_ir::Span;17use rustc_hash::{FxHashMap, FxHashSet};1819mod oop;2021pub use jrsonnet_ir::Visibility;22pub use oop::ObjValueBuilder;2324use crate::{25	CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,26	arr::{PickObjectKeyValues, PickObjectValues},27	bail,28	error::{ErrorKind::*, suggest_object_fields},29	evaluate::operator::evaluate_add_op,30	identity_hash,31	val::{ArrValue, ThunkValue},32};3334#[cfg(not(feature = "exp-preserve-order"))]35pub mod ordering {36	#![allow(37		// This module works as stub for preserve-order feature38		clippy::unused_self,39	)]4041	use jrsonnet_gcmodule::Trace;4243	#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]44	pub struct FieldIndex(());45	impl FieldIndex {46		pub fn absolute(_v: u32) -> Self {47			Self(())48		}49		#[must_use]50		pub const fn next(self) -> Self {51			Self(())52		}53	}5455	#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]56	pub struct SuperDepth(());57	impl SuperDepth {58		pub(super) fn deepen(self) {}59	}60}6162#[cfg(feature = "exp-preserve-order")]63pub mod ordering {64	use jrsonnet_gcmodule::Trace;6566	#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]67	pub struct FieldIndex(u32);68	impl FieldIndex {69		pub fn absolute(v: u32) -> Self {70			Self(v)71		}72		#[must_use]73		pub fn next(self) -> Self {74			Self(self.0 + 1)75		}76	}7778	#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]79	pub struct SuperDepth(u32);80	impl SuperDepth {81		pub(super) fn deepen(&mut self) {82			self.0 += 1;83		}84	}85}8687use ordering::{FieldIndex, SuperDepth};8889#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]90pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);91impl FieldSortKey {92	pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {93		Self(Reverse(depth), index)94	}95}9697// 0 - add98//  12 - visibility99#[derive(Clone, Copy, Acyclic)]100pub struct ObjFieldFlags(u8);101impl ObjFieldFlags {102	fn new(add: bool, visibility: Visibility) -> Self {103		let mut v = 0;104		if add {105			v |= 1;106		}107		v |= match visibility {108			Visibility::Normal => 0b000,109			Visibility::Hidden => 0b010,110			Visibility::Unhide => 0b100,111		};112		Self(v)113	}114	pub fn add(&self) -> bool {115		self.0 & 1 != 0116	}117	pub fn visibility(&self) -> Visibility {118		match (self.0 & 0b110) >> 1 {119			0b00 => Visibility::Normal,120			0b01 => Visibility::Hidden,121			0b10 => Visibility::Unhide,122			_ => unreachable!(),123		}124	}125}126impl Debug for ObjFieldFlags {127	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {128		f.debug_struct("ObjFieldFlags")129			.field("add", &self.add())130			.field("visibility", &self.visibility())131			.finish()132	}133}134135#[allow(clippy::module_name_repetitions)]136#[derive(Debug, Trace)]137pub struct ObjMember {138	flags: ObjFieldFlags,139	original_index: FieldIndex,140	pub invoke: MaybeUnbound,141	pub location: Option<Span>,142}143144cc_dyn!(CcObjectAssertion, ObjectAssertion);145pub trait ObjectAssertion: Trace {146	fn run(&self, sup_this: SupThis) -> Result<()>;147}148149// Field => This150151#[derive(Trace, Debug)]152enum CacheValue {153	Cached(Result<Option<Val>>),154	Pending,155}156157pub type EnumFieldsHandler<'a> =158	dyn FnMut(SuperDepth, FieldIndex, IStr, EnumFields) -> ControlFlow<()> + 'a;159160#[derive(Debug)]161pub enum EnumFields {162	Normal(Visibility),163	Omit(Skip),164}165166#[derive(Trace, Clone)]167pub enum GetFor {168	// Return value169	Final(Val),170	// Continue iterating over cores, add current value to sum stack171	SuperPlus(Val),172	// Ignore the field value, stop at this layer instead173	Omit(#[trace(skip)] Skip),174	NotFound,175}176177#[derive(Acyclic, Clone)]178pub enum FieldVisibility {179	Found(Visibility),180	Omit(Skip),181	NotFound,182}183184#[derive(Acyclic, Clone)]185pub enum HasFieldIncludeHidden {186	Exists,187	NotFound,188	Omit(Skip),189}190191type Skip = Saturating<usize>;192193pub trait ObjectCore: Trace + Any + Debug {194	// If callback returns false, iteration stops, and this call returns false.195	fn enum_fields_core(196		&self,197		super_depth: &mut SuperDepth,198		handler: &mut EnumFieldsHandler<'_>,199	) -> bool;200201	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden;202203	fn get_for_core(&self, key: IStr, sup_this: SupThis, omit_only: bool) -> Result<GetFor>;204	fn field_visibility_core(&self, field: IStr) -> FieldVisibility;205206	fn run_assertions_core(&self, sup_this: SupThis) -> Result<()>;207}208209#[derive(Clone, Trace)]210pub struct WeakObjValue(#[trace(skip)] Weak<ObjValueInner>);211impl Debug for WeakObjValue {212	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {213		f.debug_tuple("WeakObjValue").finish()214	}215}216217impl PartialEq for WeakObjValue {218	fn eq(&self, other: &Self) -> bool {219		Weak::ptr_eq(&self.0, &other.0)220	}221}222223impl Eq for WeakObjValue {}224impl Hash for WeakObjValue {225	fn hash<H: Hasher>(&self, hasher: &mut H) {226		// Safety: usize is POD227		let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };228		hasher.write_usize(addr);229	}230}231232cc_dyn!(233	#[derive(Clone, Debug)]234	CcObjectCore, ObjectCore,235	pub fn new() {...}236);237238#[derive(Trace, Educe)]239#[educe(Debug)]240struct ObjValueInner {241	cores: Vec<CcObjectCore>,242	assertions_ran: Cell<bool>,243	has_assertions: bool,244	value_cache: RefCell<FxHashMap<(IStr, CoreIdx), CacheValue>>,245}246247thread_local! {248	static RUNNING_ASSERTIONS: RefCell<FxHashSet<ObjValue>> = RefCell::default();249}250fn is_asserting(obj: &ObjValue) -> bool {251	RUNNING_ASSERTIONS.with_borrow(|v| v.contains(obj))252}253/// Returns false if already asserting254fn start_asserting(obj: &ObjValue) -> bool {255	RUNNING_ASSERTIONS.with_borrow_mut(|v| v.insert(obj.clone()))256}257fn finish_asserting(obj: &ObjValue) {258	RUNNING_ASSERTIONS.with_borrow_mut(|v| {259		let r = v.remove(obj);260		debug_assert!(261			r,262			"finish_asserting was called before start_asserting or twice"263		);264	});265}266267thread_local! {268	static EMPTY_OBJ: ObjValue = ObjValue(Cc::new(ObjValueInner {269		cores: vec![],270		assertions_ran: Cell::new(true),271		has_assertions: false,272		value_cache: RefCell::default(),273	}))274}275276#[allow(clippy::module_name_repetitions)]277#[derive(Clone, Trace, Debug, Educe)]278#[educe(PartialEq, Hash, Eq)]279pub struct ObjValue(280	#[educe(PartialEq(method(Cc::ptr_eq)), Hash(method(identity_hash)))] Cc<ObjValueInner>,281);282283impl ObjValue {284	pub fn empty() -> Self {285		EMPTY_OBJ.with(Clone::clone)286	}287	pub fn is_empty(&self) -> bool {288		self.0.cores.is_empty() || self.len() == 0289	}290}291292#[derive(Trace, Debug)]293pub(crate) struct StandaloneSuperCore {294	sup: CoreIdx,295	this: ObjValue,296}297impl ObjectCore for StandaloneSuperCore {298	fn enum_fields_core(299		&self,300		super_depth: &mut SuperDepth,301		handler: &mut EnumFieldsHandler<'_>,302	) -> bool {303		self.this.enum_fields_idx(super_depth, handler, self.sup)304	}305306	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {307		if self.this.has_field_include_hidden_idx(name, self.sup) {308			HasFieldIncludeHidden::Exists309		} else {310			HasFieldIncludeHidden::NotFound311		}312	}313314	fn get_for_core(&self, key: IStr, _sup_this: SupThis, omit_only: bool) -> Result<GetFor> {315		if omit_only {316			return Ok(GetFor::NotFound);317		}318		let v = self.this.get_idx(key, self.sup)?;319		Ok(v.map_or(GetFor::NotFound, GetFor::Final))320	}321322	fn field_visibility_core(&self, field: IStr) -> FieldVisibility {323		self.this324			.field_visibility_idx(field, self.sup)325			.map_or(FieldVisibility::NotFound, FieldVisibility::Found)326	}327328	fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {329		self.this.run_assertions()330	}331}332333#[derive(Debug, Acyclic)]334struct OmitFieldsCore {335	omit: FxHashSet<IStr>,336	prev_layers: usize,337}338impl ObjectCore for OmitFieldsCore {339	fn enum_fields_core(340		&self,341		super_depth: &mut SuperDepth,342		handler: &mut EnumFieldsHandler<'_>,343	) -> bool {344		let mut fi = FieldIndex::default();345		for f in &self.omit {346			if handler(347				*super_depth,348				fi,349				f.clone(),350				EnumFields::Omit(Saturating(self.prev_layers)),351			) == ControlFlow::Break(())352			{353				return false;354			}355			fi = fi.next();356		}357		true358	}359360	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {361		if self.omit.contains(&name) {362			return HasFieldIncludeHidden::Omit(Saturating(self.prev_layers));363		}364		HasFieldIncludeHidden::NotFound365	}366367	fn get_for_core(&self, key: IStr, _sup_this: SupThis, _omit_only: bool) -> Result<GetFor> {368		if self.omit.contains(&key) {369			return Ok(GetFor::Omit(Saturating(self.prev_layers)));370		}371		Ok(GetFor::NotFound)372	}373374	fn field_visibility_core(&self, field: IStr) -> FieldVisibility {375		if self.omit.contains(&field) {376			return FieldVisibility::Omit(Saturating(self.prev_layers));377		}378		FieldVisibility::NotFound379	}380381	fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {382		Ok(())383	}384}385386#[derive(Hash, PartialEq, Eq, Trace, Clone, Copy, Debug)]387struct CoreIdx {388	idx: usize,389}390impl CoreIdx {391	fn super_exists(self) -> bool {392		self.idx != 0393	}394}395#[derive(Trace, Clone, PartialEq, Eq, Hash, Debug)]396pub struct SupThis {397	sup: CoreIdx,398	this: ObjValue,399}400impl SupThis {401	/// Create a `SupThis` for a freshly constructed object (no super).402	pub fn new(this: ObjValue) -> Self {403		Self {404			sup: CoreIdx {405				idx: this.0.cores.len(),406			},407			this,408		}409	}410	pub fn has_super(&self) -> bool {411		self.sup.super_exists()412	}413	/// Implementation of `"field" in super` operation,414	/// works faster than standalone super path.415	///416	/// In case of no `super` existence, returns false.417	pub fn field_in_super(&self, field: IStr) -> bool {418		self.this.has_field_include_hidden_idx(field, self.sup)419	}420	/// Implementation of `super.field` operation,421	/// works faster than standalone super path.422	///423	/// In case of no `super` existence, returns `NoSuperFound`424	pub fn get_super(&self, field: IStr) -> Result<Option<Val>> {425		if !self.sup.super_exists() {426			bail!(NoSuperFound);427		}428		self.this.get_idx(field, self.sup)429	}430	/// `super` with `self` overriden for top-level lookups.431	/// Exists when super appears outside of `super.field`/`"field" in super` expressions432	/// Exclusive to jrsonnet.433	///434	/// Might return `NoSuperFound` error.435	pub fn standalone_super(&self) -> Result<ObjValue> {436		if !self.sup.super_exists() {437			bail!(NoSuperFound)438		}439		let mut out = ObjValue::builder();440		out.extend_with_core(StandaloneSuperCore {441			sup: self.sup,442			this: self.this.clone(),443		});444		Ok(out.build())445	}446	pub fn this(&self) -> &ObjValue {447		&self.this448	}449	pub fn downgrade(self) -> WeakSupThis {450		WeakSupThis {451			sup: self.sup,452			this: self.this.downgrade(),453		}454	}455}456#[derive(Trace, PartialEq, Eq, Hash, Debug)]457pub struct WeakSupThis {458	sup: CoreIdx,459	this: WeakObjValue,460}461462impl ObjValue {463	pub fn builder() -> ObjValueBuilder {464		ObjValueBuilder::new()465	}466	pub fn builder_with_capacity(capacity: usize) -> ObjValueBuilder {467		ObjValueBuilder::with_capacity(capacity)468	}469	pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {470		let mut out = ObjValueBuilder::with_capacity(1);471		out.with_super(self);472		let mut member = out.field(key);473		if value.flags.add() {474			member = member.add();475		}476		if let Some(loc) = value.location {477			member = member.with_location(loc);478		}479		let _ = member480			.with_visibility(value.flags.visibility())481			.binding(value.invoke);482		out.build()483	}484	pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {485		ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())486	}487488	pub fn extend(&mut self) -> ObjValueBuilder {489		let mut out = ObjValueBuilder::new();490		out.with_super(self.clone());491		out492	}493494	#[must_use]495	pub fn extend_from(&self, sup: Self) -> Self {496		let mut cores = Vec::with_capacity(sup.0.cores.len() + self.0.cores.len());497		cores.extend(sup.0.cores.iter().cloned());498		cores.extend(self.0.cores.iter().cloned());499500		let has_assertions = sup.0.has_assertions || self.0.has_assertions;501		ObjValue(Cc::new(ObjValueInner {502			cores,503			value_cache: RefCell::default(),504			assertions_ran: Cell::new(!has_assertions),505			has_assertions,506		}))507	}508	// #[must_use]509	// pub fn with_this(&self, this: Self) -> Self {510	// 	self.0.with_this(self.clone(), this)511	// }512	/// Returns amount of visible object fields513	/// If object only contains hidden fields - may return zero.514	pub fn len(&self) -> u32 {515		self.fields_visibility()516			.values()517			.filter(|d| d.visible())518			.count() as u32519	}520	/// For each field, calls callback.521	/// If callback returns false - ends iteration prematurely.522	///523	/// Returns false if ended prematurely524	pub fn enum_fields(&self, handler: &mut EnumFieldsHandler<'_>) -> bool {525		let mut super_depth = SuperDepth::default();526		self.enum_fields_idx(527			&mut super_depth,528			handler,529			CoreIdx {530				idx: self.0.cores.len(),531			},532		)533	}534535	fn iter_cores(&self, idx: CoreIdx) -> impl Iterator<Item = &CcObjectCore> {536		self.0.cores.iter().take(idx.idx).rev()537	}538	fn iter_cores_enumerate(&self, idx: CoreIdx) -> impl Iterator<Item = (CoreIdx, &CcObjectCore)> {539		self.0540			.cores541			.iter()542			.take(idx.idx)543			.enumerate()544			.rev()545			.map(|(idx, o)| (CoreIdx { idx }, o))546	}547548	fn enum_fields_idx(549		&self,550		super_depth: &mut SuperDepth,551		handler: &mut EnumFieldsHandler<'_>,552		idx: CoreIdx,553	) -> bool {554		for core in self.iter_cores(idx) {555			if !core.0.enum_fields_core(super_depth, handler) {556				return false;557			}558			super_depth.deepen();559		}560		true561	}562563	pub fn has_field_include_hidden(&self, name: IStr) -> bool {564		self.has_field_include_hidden_idx(565			name,566			CoreIdx {567				idx: self.0.cores.len(),568			},569		)570	}571	fn has_field_include_hidden_idx(&self, name: IStr, core: CoreIdx) -> bool {572		let mut skip = Saturating(0usize);573		for ele in self.iter_cores(core) {574			match ele.0.has_field_include_hidden_core(name.clone()) {575				HasFieldIncludeHidden::Exists => {576					if skip.0 == 0 {577						return true;578					}579				}580				HasFieldIncludeHidden::Omit(new_skip) => {581					// +1 including this core582					skip = skip.max(new_skip + Saturating(1));583				}584				HasFieldIncludeHidden::NotFound => {}585			}586			skip -= 1;587		}588		false589	}590	pub fn has_field(&self, name: IStr) -> bool {591		match self.field_visibility(name) {592			Some(Visibility::Unhide | Visibility::Normal) => true,593			Some(Visibility::Hidden) | None => false,594		}595	}596	pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {597		if include_hidden {598			self.has_field_include_hidden(name)599		} else {600			self.has_field(name)601		}602	}603	pub fn get(&self, key: IStr) -> Result<Option<Val>> {604		self.get_idx(605			key,606			CoreIdx {607				idx: self.0.cores.len(),608			},609		)610	}611612	fn get_idx(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {613		let cache_key = (key.clone(), core);614		{615			let mut cache = self.0.value_cache.borrow_mut();616			// entry_ref candidate?617			match cache.entry(cache_key.clone()) {618				Entry::Occupied(v) => match v.get() {619					CacheValue::Cached(v) => return v.clone(),620					CacheValue::Pending => {621						if !is_asserting(self) {622							bail!(InfiniteRecursionDetected);623						}624					}625				},626				Entry::Vacant(v) => {627					v.insert(CacheValue::Pending);628				}629			};630		}631		let result = self.get_idx_uncached(key, core);632		{633			let mut cache = self.0.value_cache.borrow_mut();634			cache.insert(cache_key, CacheValue::Cached(result.clone()));635		}636		result637	}638	fn get_idx_uncached(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {639		self.run_assertions()?;640		let mut first_add = None;641		let mut add_stack: Vec<Val> = Vec::new();642		let mut skip = Saturating(0);643		for (sup, core) in self.iter_cores_enumerate(core) {644			let sup_this = SupThis {645				sup,646				this: self.clone(),647			};648			match core.0.get_for_core(key.clone(), sup_this, skip.0 != 0)? {649				GetFor::Final(val) if first_add.is_none() => {650					if skip.0 == 0 {651						return Ok(Some(val));652					}653				}654				GetFor::Final(val) => {655					if skip.0 == 0 {656						add_stack.push(val);657						break;658					}659				}660				GetFor::SuperPlus(val) => {661					if skip.0 == 0 {662						if first_add.is_none() {663							first_add = Some(val);664						} else {665							add_stack.push(val);666						}667					}668				}669				GetFor::Omit(new_skip) => {670					skip = skip.max(new_skip + Saturating(1));671				}672				GetFor::NotFound => {}673			}674			skip -= 1;675		}676		let Some(first) = first_add else {677			if add_stack.is_empty() {678				return Ok(None);679			}680			return Ok(Some(add_stack.pop().expect("single element on stack")));681		};682		if add_stack.is_empty() {683			return Ok(Some(first));684		}685		add_stack.insert(0, first);686		let mut values = add_stack.into_iter().rev();687		let init = values.next().expect("at least 2 elements");688689		values690			.try_fold(init, |a, b| evaluate_add_op(&a, &b))691			.map(Some)692	}693694	pub fn get_or_bail(&self, key: IStr) -> Result<Val> {695		let Some(value) = self.get(key.clone())? else {696			let suggestions = suggest_object_fields(self, key.clone());697			bail!(NoSuchField(key, suggestions))698		};699		Ok(value)700	}701702	fn field_visibility(&self, field: IStr) -> Option<Visibility> {703		self.field_visibility_idx(704			field,705			CoreIdx {706				idx: self.0.cores.len(),707			},708		)709	}710	fn field_visibility_idx(&self, field: IStr, core: CoreIdx) -> Option<Visibility> {711		let mut exists = false;712		let mut skip = Saturating(0usize);713		for ele in self.iter_cores(core) {714			let vis = ele.0.field_visibility_core(field.clone());715			match vis {716				FieldVisibility::Found(vis @ (Visibility::Unhide | Visibility::Hidden)) => {717					if skip.0 == 0 {718						return Some(vis);719					}720				}721				FieldVisibility::Found(Visibility::Normal) => {722					if skip.0 == 0 {723						exists = true;724					}725				}726				FieldVisibility::NotFound => {}727				FieldVisibility::Omit(new_skip) => {728					// +1 including this core729					skip = skip.max(new_skip + Saturating(1));730				}731			}732			skip -= 1;733		}734		exists.then_some(Visibility::Normal)735	}736737	pub fn run_assertions(&self) -> Result<()> {738		if self.0.assertions_ran.get() {739			return Ok(());740		}741		if !start_asserting(self) {742			return Ok(());743		}744		for (idx, ele) in self.0.cores.iter().enumerate() {745			let sup_this = SupThis {746				sup: CoreIdx { idx },747				this: self.clone(),748			};749			ele.0.run_assertions_core(sup_this).inspect_err(|_e| {750				finish_asserting(self);751			})?;752		}753		finish_asserting(self);754		self.0.assertions_ran.set(true);755		Ok(())756	}757758	pub fn iter(759		&self,760		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,761	) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {762		let fields = self.fields(763			#[cfg(feature = "exp-preserve-order")]764			preserve_order,765		);766		fields.into_iter().map(|field| {767			(768				field.clone(),769				self.get(field)770					.map(|opt| opt.expect("iterating over keys, field exists")),771			)772		})773	}774	pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {775		#[derive(Trace)]776		struct ObjFieldThunk {777			obj: ObjValue,778			key: IStr,779		}780		impl ThunkValue for ObjFieldThunk {781			type Output = Val;782783			fn get(&self) -> Result<Self::Output> {784				self.obj785					.get(self.key.clone())786					.transpose()787					.expect("field existence checked")788			}789		}790791		if !self.has_field_ex(key.clone(), true) {792			return None;793		}794795		Some(Thunk::new(ObjFieldThunk {796			obj: self.clone(),797			key,798		}))799	}800	pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {801		#[derive(Trace)]802		struct ObjFieldThunk {803			obj: ObjValue,804			key: IStr,805		}806		impl ThunkValue for ObjFieldThunk {807			type Output = Val;808809			fn get(&self) -> Result<Self::Output> {810				self.obj.get_or_bail(self.key.clone())811			}812		}813814		Thunk::new(ObjFieldThunk {815			obj: self.clone(),816			key,817		})818	}819820	#[allow(dead_code, reason = "used in object ...rest destructuring")]821	pub(crate) fn as_standalone(&self) -> StandaloneSuperCore {822		StandaloneSuperCore {823			sup: CoreIdx {824				idx: self.0.cores.len(),825			},826			this: self.clone(),827		}828	}829	pub fn ptr_eq(a: &Self, b: &Self) -> bool {830		Cc::ptr_eq(&a.0, &b.0)831	}832	pub fn downgrade(self) -> WeakObjValue {833		WeakObjValue(self.0.downgrade())834	}835}836837#[derive(Debug)]838struct FieldVisibilityData {839	omitted_until: Saturating<usize>,840	exists_visible: Option<Visibility>,841	#[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]842	key: FieldSortKey,843}844impl FieldVisibilityData {845	fn visible(&self) -> bool {846		self.exists_visible847			.expect("non-existing fields shall be dropped at the end of fn fields_visibility()")848			.is_visible()849	}850	#[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]851	fn sort_key(&self) -> FieldSortKey {852		self.key853	}854}855856impl ObjValue {857	fn fields_visibility(&self) -> FxHashMap<IStr, FieldVisibilityData> {858		let mut out = FxHashMap::default();859860		let mut super_depth = SuperDepth::default();861		let mut omit_index = Saturating(0);862		for core in self.0.cores.iter().rev() {863			core.0864				.enum_fields_core(&mut super_depth, &mut |depth, index, name, visibility| {865					let entry = out.entry(name);866					let data = entry.or_insert_with(|| FieldVisibilityData {867						exists_visible: None,868						key: FieldSortKey::new(depth, index),869						omitted_until: omit_index,870					});871					match visibility {872						EnumFields::Omit(new_skip) => {873							// +1 including this core874							data.omitted_until = data875								.omitted_until876								.max(omit_index + new_skip + Saturating(1));877						}878						EnumFields::Normal(Visibility::Normal) => {879							if data.omitted_until <= omit_index && data.exists_visible.is_none() {880								data.exists_visible = Some(Visibility::Normal);881							}882						}883						EnumFields::Normal(Visibility::Hidden) => {884							if data.omitted_until <= omit_index {885								data.exists_visible = Some(match data.exists_visible {886									// We're iterating in reverse, later unhide is preserved887									Some(Visibility::Unhide) => Visibility::Unhide,888									_ => Visibility::Hidden,889								});890							}891						}892						EnumFields::Normal(Visibility::Unhide) => {893							if data.omitted_until <= omit_index {894								data.exists_visible = Some(match data.exists_visible {895									// We're iterating in reverse, later hide is preserved896									Some(Visibility::Hidden) => Visibility::Hidden,897									_ => Visibility::Unhide,898								});899							}900						}901					}902					ControlFlow::Continue(())903				});904905			super_depth.deepen();906			omit_index += 1;907		}908909		out.retain(|_, v| v.exists_visible.is_some());910911		out912	}913	pub fn fields_ex(914		&self,915		include_hidden: bool,916		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,917	) -> Vec<IStr> {918		#[cfg(feature = "exp-preserve-order")]919		if preserve_order {920			let (mut fields, mut keys): (Vec<_>, Vec<_>) = self921				.fields_visibility()922				.into_iter()923				.filter(|(_, d)| include_hidden || d.visible())924				.enumerate()925				.map(|(idx, (k, d))| (k, (d.sort_key(), idx)))926				.unzip();927			keys.sort_unstable_by_key(|v| v.0);928			// Reorder in-place by resulting indexes929			for i in 0..fields.len() {930				let x = fields[i].clone();931				let mut j = i;932				loop {933					let k = keys[j].1;934					keys[j].1 = j;935					if k == i {936						break;937					}938					fields[j] = fields[k].clone();939					j = k;940				}941				fields[j] = x;942			}943			return fields;944		}945946		let mut fields: Vec<_> = self947			.fields_visibility()948			.into_iter()949			.filter(|(_, d)| include_hidden || d.visible())950			.map(|(k, _)| k)951			.collect();952		fields.sort_unstable();953		fields954	}955	pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {956		self.fields_ex(957			false,958			#[cfg(feature = "exp-preserve-order")]959			preserve_order,960		)961	}962	pub fn values_ex(963		&self,964		include_hidden: bool,965		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,966	) -> ArrValue {967		ArrValue::new(PickObjectValues::new(968			self.clone(),969			self.fields_ex(970				include_hidden,971				#[cfg(feature = "exp-preserve-order")]972				preserve_order,973			),974		))975	}976	pub fn values(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> ArrValue {977		self.values_ex(978			false,979			#[cfg(feature = "exp-preserve-order")]980			preserve_order,981		)982	}983	pub fn key_values_ex(984		&self,985		include_hidden: bool,986		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,987	) -> ArrValue {988		ArrValue::new(PickObjectKeyValues::new(989			self.clone(),990			self.fields_ex(991				include_hidden,992				#[cfg(feature = "exp-preserve-order")]993				preserve_order,994			),995		))996	}997	pub fn key_values(998		&self,999		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,1000	) -> ArrValue {1001		self.key_values_ex(1002			false,1003			#[cfg(feature = "exp-preserve-order")]1004			preserve_order,1005		)1006	}1007}10081009#[allow(clippy::module_name_repetitions)]1010#[must_use = "value not added unless binding() was called"]1011pub struct ObjMemberBuilder<Kind> {1012	kind: Kind,1013	name: IStr,1014	add: bool,1015	visibility: Visibility,1016	original_index: FieldIndex,1017	location: Option<Span>,1018}10191020#[allow(clippy::missing_const_for_fn)]1021impl<Kind> ObjMemberBuilder<Kind> {1022	pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {1023		Self {1024			kind,1025			name,1026			original_index,1027			add: false,1028			visibility: Visibility::Normal,1029			location: None,1030		}1031	}10321033	pub const fn with_add(mut self, add: bool) -> Self {1034		self.add = add;1035		self1036	}1037	pub fn add(self) -> Self {1038		self.with_add(true)1039	}1040	pub fn with_visibility(mut self, visibility: Visibility) -> Self {1041		self.visibility = visibility;1042		self1043	}1044	pub fn hide(self) -> Self {1045		self.with_visibility(Visibility::Hidden)1046	}1047	pub fn with_location(mut self, location: Span) -> Self {1048		self.location = Some(location);1049		self1050	}1051	fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {1052		(1053			self.kind,1054			self.name,1055			ObjMember {1056				flags: ObjFieldFlags::new(self.add, self.visibility),1057				original_index: self.original_index,1058				invoke: binding,1059				location: self.location,1060			},1061		)1062	}1063}10641065pub struct ExtendBuilder<'v>(&'v mut ObjValue);1066impl ObjMemberBuilder<ExtendBuilder<'_>> {1067	pub fn value(self, value: impl Into<Val>) {1068		self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));1069	}1070	pub fn bindable(self, bindable: impl Unbound<Bound = Val>) {1071		self.binding(MaybeUnbound::Unbound(CcUnbound::new(bindable)));1072	}1073	pub fn binding(self, binding: MaybeUnbound) {1074		let (receiver, name, member) = self.build_member(binding);1075		let new = receiver.0.clone();1076		*receiver.0 = new.extend_with_raw_member(name, member);1077	}1078}
modifiedcrates/jrsonnet-evaluator/src/obj/oop.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj/oop.rs
+++ b/crates/jrsonnet-evaluator/src/obj/oop.rs
@@ -4,7 +4,6 @@
 	ops::ControlFlow,
 };
 
-use im_rc::Vector;
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_ir::IStr;
 use rustc_hash::{FxHashMap, FxHashSet};
@@ -118,7 +117,7 @@
 
 #[allow(clippy::module_name_repetitions)]
 pub struct ObjValueBuilder {
-	sup: Vector<CcObjectCore>,
+	sup: Vec<CcObjectCore>,
 	has_assertions: bool,
 
 	new: OopObject,
@@ -130,7 +129,7 @@
 	}
 	pub fn with_capacity(capacity: usize) -> Self {
 		Self {
-			sup: Vector::new(),
+			sup: Vec::new(),
 			has_assertions: false,
 			new: OopObject::new(FxHashMap::with_capacity(capacity), None),
 			next_field_index: FieldIndex::default(),
@@ -141,7 +140,7 @@
 	}
 	pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self {
 		self.has_assertions |= super_obj.0.has_assertions;
-		self.sup = super_obj.0.cores.clone();
+		self.sup.clone_from(&super_obj.0.cores);
 		self
 	}
 
@@ -178,20 +177,19 @@
 
 	pub fn extend_with_core(&mut self, core: impl ObjectCore) {
 		self.commit();
-		self.sup.push_back(CcObjectCore::new(core));
+		self.sup.push(CcObjectCore::new(core));
 	}
 
 	fn commit(&mut self) {
 		if !self.new.is_empty() {
-			self.sup
-				.push_back(CcObjectCore::new(mem::take(&mut self.new)));
+			self.sup.push(CcObjectCore::new(mem::take(&mut self.new)));
 		}
 		self.next_field_index = FieldIndex::default();
 	}
 
 	pub fn with_fields_omitted(&mut self, omit: FxHashSet<IStr>) {
 		self.commit();
-		self.sup.push_back(CcObjectCore::new(OmitFieldsCore {
+		self.sup.push(CcObjectCore::new(OmitFieldsCore {
 			omit,
 			prev_layers: self.sup.len(),
 		}));