git.delta.rocks / jrsonnet / refs/commits / 54c4db58ad3d

difftreelog

fix makeArray should be lazy

Yaroslav Bolyukin2022-12-03parent: #2afd5ff.patch.diff
in: master

7 files changed

modifiedcrates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -32,6 +32,8 @@
 	Reverse(Box<ReverseArray>),
 	/// Returned by `std.map` call
 	Mapped(MappedArray),
+	/// Returned by `std.repeat` call
+	Repeated(RepeatedArray),
 }
 
 impl ArrValue {
@@ -51,6 +53,10 @@
 		Self::Eager(EagerArray(values))
 	}
 
+	pub fn repeated(data: ArrValue, repeats: usize) -> Option<Self> {
+		Some(Self::Repeated(RepeatedArray::new(data, repeats)?))
+	}
+
 	pub fn bytes(bytes: IBytes) -> Self {
 		Self::Bytes(BytesArray(bytes))
 	}
@@ -76,7 +82,11 @@
 		// TODO: benchmark for an optimal value, currently just a arbitrary choice
 		const ARR_EXTEND_THRESHOLD: usize = 100;
 
-		if a.len() + b.len() > ARR_EXTEND_THRESHOLD {
+		if a.is_empty() {
+			b
+		} else if b.is_empty() {
+			a
+		} else if a.len() + b.len() > ARR_EXTEND_THRESHOLD {
 			Self::Extended(Cc::new(ExtendedArray::new(a, b)))
 		} else if let (Some(a), Some(b)) = (a.iter_cheap(), b.iter_cheap()) {
 			let mut out = Vec::with_capacity(a.len() + b.len());
@@ -189,10 +199,8 @@
 			(ArrValue::Lazy(a), ArrValue::Lazy(b)) => Cc::ptr_eq(&a.0, &b.0),
 			(ArrValue::Expr(a), ArrValue::Expr(b)) => Cc::ptr_eq(&a.0, &b.0),
 			(ArrValue::Eager(a), ArrValue::Eager(b)) => Cc::ptr_eq(&a.0, &b.0),
-			(ArrValue::Extended(a), ArrValue::Extended(b)) => Cc::ptr_eq(&a, &b),
+			(ArrValue::Extended(a), ArrValue::Extended(b)) => Cc::ptr_eq(a, b),
 			(ArrValue::Range(a), ArrValue::Range(b)) => a == b,
-			(ArrValue::Slice(_), ArrValue::Slice(_)) => false,
-			(ArrValue::Reverse(_), ArrValue::Reverse(_)) => false,
 			_ => false,
 		}
 	}
@@ -203,6 +211,7 @@
 			ArrValue::Extended(v) => v.a.is_cheap() && v.b.is_cheap(),
 			ArrValue::Slice(r) => r.inner.is_cheap(),
 			ArrValue::Reverse(i) => i.0.is_cheap(),
+			ArrValue::Repeated(v) => v.is_cheap(),
 			ArrValue::Expr(_) | ArrValue::Lazy(_) | ArrValue::Mapped(_) => false,
 		}
 	}
modifiedcrates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/arr/spec.rs
1use std::{2	cell::RefCell,3	iter::{self, Rev},4	mem::replace,5};67use jrsonnet_gcmodule::{Cc, Trace};8use jrsonnet_interner::IBytes;9use jrsonnet_parser::LocExpr;1011use super::ArrValue;12use crate::{13	error::ErrorKind::InfiniteRecursionDetected, evaluate, function::FuncVal, tb, typed::Any,14	val::ThunkValue, Context, Error, Result, Thunk, Val,15};1617pub trait ArrayLike {18	type Iter<'t>19	where20		Self: 't;21	type IterLazy<'t>22	where23		Self: 't;24	type IterCheap<'t>25	where26		Self: 't;2728	fn len(&self) -> usize;29	fn is_empty(&self) -> bool {30		self.len() == 031	}32	fn get(&self, index: usize) -> Result<Option<Val>>;33	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>>;34	fn get_cheap(&self, index: usize) -> Option<Val>;35	fn evaluated(&self) -> Result<Vec<Val>>;36	#[allow(clippy::iter_not_returning_iterator)]37	fn iter(&self) -> Self::Iter<'_>;38	fn iter_lazy(&self) -> Self::IterLazy<'_>;39	fn iter_cheap(&self) -> Option<Self::IterCheap<'_>>;40}4142#[derive(Debug, Clone, Trace)]43pub struct SliceArray {44	pub(crate) inner: ArrValue,45	pub(crate) from: u32,46	pub(crate) to: u32,47	pub(crate) step: u32,48}49type SliceArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;50type SliceArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;51type SliceArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;52impl ArrayLike for SliceArray {53	type Iter<'t> = SliceArrayIter<'t>;5455	type IterLazy<'t> = SliceArrayLazyIter<'t>;5657	type IterCheap<'t> = SliceArrayCheapIter<'t>;5859	fn len(&self) -> usize {60		iter::repeat(())61			.take((self.to - self.from) as usize)62			.step_by(self.step as usize)63			.count()64	}6566	fn get(&self, index: usize) -> Result<Option<Val>> {67		self.iter().nth(index).transpose()68	}6970	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {71		self.iter_lazy().nth(index)72	}7374	fn get_cheap(&self, index: usize) -> Option<Val> {75		self.iter_cheap()?.nth(index)76	}7778	fn evaluated(&self) -> Result<Vec<Val>> {79		self.iter().collect()80	}8182	fn iter(&self) -> SliceArrayIter<'_> {83		self.inner84			.iter()85			.skip(self.from as usize)86			.take((self.to - self.from) as usize)87			.step_by(self.step as usize)88	}8990	fn iter_lazy(&self) -> SliceArrayLazyIter<'_> {91		self.inner92			.iter_lazy()93			.skip(self.from as usize)94			.take((self.to - self.from) as usize)95			.step_by(self.step as usize)96	}9798	fn iter_cheap(&self) -> Option<SliceArrayCheapIter<'_>> {99		Some(100			self.inner101				.iter_cheap()?102				.skip(self.from as usize)103				.take((self.to - self.from) as usize)104				.step_by(self.step as usize),105		)106	}107}108109#[derive(Trace, Debug, Clone)]110pub struct BytesArray(pub IBytes);111type BytesArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;112type BytesArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;113type BytesArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;114impl ArrayLike for BytesArray {115	type Iter<'t> = BytesArrayIter<'t>;116117	type IterLazy<'t> = BytesArrayLazyIter<'t>;118119	type IterCheap<'t> = BytesArrayCheapIter<'t>;120121	fn len(&self) -> usize {122		self.0.len()123	}124125	fn get(&self, index: usize) -> Result<Option<Val>> {126		Ok(self.get_cheap(index))127	}128129	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {130		self.get_cheap(index).map(Thunk::evaluated)131	}132133	fn get_cheap(&self, index: usize) -> Option<Val> {134		self.0.get(index).map(|v| Val::Num(f64::from(*v)))135	}136137	fn evaluated(&self) -> Result<Vec<Val>> {138		self.iter().collect()139	}140141	fn iter(&self) -> BytesArrayIter<'_> {142		self.0.iter().map(|v| Ok(Val::Num(f64::from(*v))))143	}144145	fn iter_lazy(&self) -> BytesArrayLazyIter<'_> {146		self.0147			.iter()148			.map(|v| Thunk::evaluated(Val::Num(f64::from(*v))))149	}150151	fn iter_cheap(&self) -> Option<BytesArrayCheapIter<'_>> {152		Some(self.0.iter().map(|v| Val::Num(f64::from(*v))))153	}154}155156#[derive(Debug, Trace, Clone)]157enum ArrayThunk<T: 'static + Trace> {158	Computed(Val),159	Errored(Error),160	Waiting(T),161	Pending,162}163164#[derive(Debug, Trace)]165pub struct ExprArrayInner {166	ctx: Context,167	cached: RefCell<Vec<ArrayThunk<LocExpr>>>,168}169#[derive(Debug, Trace, Clone)]170pub struct ExprArray(pub Cc<ExprArrayInner>);171type ExprArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;172type ExprArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;173type ExprArrayCheapIter<'t> = iter::Empty<Val>;174impl ExprArray {175	pub fn new(ctx: Context, items: impl IntoIterator<Item = LocExpr>) -> Self {176		Self(Cc::new(ExprArrayInner {177			ctx,178			cached: RefCell::new(items.into_iter().map(ArrayThunk::Waiting).collect()),179		}))180	}181}182impl ArrayLike for ExprArray {183	type Iter<'t> = ExprArrayIter<'t>;184185	type IterLazy<'t> = ExprArrayLazyIter<'t>;186187	type IterCheap<'t> = ExprArrayCheapIter<'t>;188189	fn len(&self) -> usize {190		self.0.cached.borrow().len()191	}192	fn get(&self, index: usize) -> Result<Option<Val>> {193		if index >= self.len() {194			return Ok(None);195		}196		match &self.0.cached.borrow()[index] {197			ArrayThunk::Computed(c) => return Ok(Some(c.clone())),198			ArrayThunk::Errored(e) => return Err(e.clone()),199			ArrayThunk::Pending => return Err(InfiniteRecursionDetected.into()),200			ArrayThunk::Waiting(..) => {}201		};202203		let ArrayThunk::Waiting(expr) = replace(&mut self.0.cached.borrow_mut()[index], ArrayThunk::Pending) else {204			unreachable!()205		};206207		let new_value = match evaluate(self.0.ctx.clone(), &expr) {208			Ok(v) => v,209			Err(e) => {210				self.0.cached.borrow_mut()[index] = ArrayThunk::Errored(e.clone());211				return Err(e);212			}213		};214		self.0.cached.borrow_mut()[index] = ArrayThunk::Computed(new_value.clone());215		Ok(Some(new_value))216	}217	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {218		#[derive(Trace)]219		struct ArrayElement {220			arr_thunk: ExprArray,221			index: usize,222		}223224		impl ThunkValue for ArrayElement {225			type Output = Val;226227			fn get(self: Box<Self>) -> Result<Self::Output> {228				self.arr_thunk229					.get(self.index)230					.transpose()231					.expect("index checked")232			}233		}234235		if index >= self.len() {236			return None;237		}238		match &self.0.cached.borrow()[index] {239			ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())),240			ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())),241			ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}242		};243244		Some(Thunk::new(tb!(ArrayElement {245			arr_thunk: self.clone(),246			index,247		})))248	}249	fn get_cheap(&self, _index: usize) -> Option<Val> {250		None251	}252253	fn iter(&self) -> ExprArrayIter<'_> {254		(0..self.len()).map(|i| self.get(i).transpose().expect("index checked"))255	}256	fn iter_lazy(&self) -> ExprArrayLazyIter<'_> {257		(0..self.len()).map(|i| self.get_lazy(i).expect("index checked"))258	}259	fn iter_cheap(&self) -> Option<ExprArrayCheapIter<'_>> {260		None261	}262263	fn evaluated(&self) -> Result<Vec<Val>> {264		self.iter().collect()265	}266}267268#[derive(Trace, Debug, Clone)]269pub struct ExtendedArray {270	pub a: ArrValue,271	pub b: ArrValue,272	split: usize,273	len: usize,274}275type ExtendedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + 't;276type ExtendedArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + 't;277type ExtendedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + 't;278impl ExtendedArray {279	pub fn new(a: ArrValue, b: ArrValue) -> Self {280		let a_len = a.len();281		let b_len = b.len();282		Self {283			a,284			b,285			split: a_len,286			len: a_len.checked_add(b_len).expect("too large array value"),287		}288	}289}290impl ArrayLike for ExtendedArray {291	type Iter<'t> = ExtendedArrayIter<'t>;292293	type IterLazy<'t> = ExtendedArrayLazyIter<'t>;294295	type IterCheap<'t> = ExtendedArrayCheapIter<'t>;296297	fn get(&self, index: usize) -> Result<Option<Val>> {298		if self.split > index {299			self.a.get(index)300		} else {301			self.b.get(index - self.split)302		}303	}304	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {305		if self.split > index {306			self.a.get_lazy(index)307		} else {308			self.b.get_lazy(index - self.split)309		}310	}311312	fn len(&self) -> usize {313		self.len314	}315316	fn get_cheap(&self, index: usize) -> Option<Val> {317		if self.split > index {318			self.a.get_cheap(index)319		} else {320			self.b.get_cheap(index - self.split)321		}322	}323324	fn evaluated(&self) -> Result<Vec<Val>> {325		let mut out = self.a.evaluated()?;326		out.extend(self.b.evaluated()?.into_iter());327		Ok(out)328	}329330	fn iter(&self) -> ExtendedArrayIter<'_> {331		self.a.iter().chain(self.b.iter())332	}333	fn iter_lazy(&self) -> ExtendedArrayLazyIter<'_> {334		self.a.iter_lazy().chain(self.b.iter_lazy())335	}336	fn iter_cheap(&self) -> Option<ExtendedArrayCheapIter<'_>> {337		let a = self.a.iter_cheap()?;338		let b = self.b.iter_cheap()?;339		Some(a.chain(b))340	}341}342343#[derive(Trace, Debug, Clone)]344pub struct LazyArray(pub Cc<Vec<Thunk<Val>>>);345type LazyArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;346type LazyArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;347type LazyArrayCheapIter<'t> = iter::Empty<Val>;348impl ArrayLike for LazyArray {349	type Iter<'t> = LazyArrayIter<'t>;350351	type IterLazy<'t> = LazyArrayLazyIter<'t>;352353	type IterCheap<'t> = LazyArrayCheapIter<'t>;354355	fn len(&self) -> usize {356		self.0.len()357	}358	fn get(&self, index: usize) -> Result<Option<Val>> {359		let Some(v) = self.0.get(index) else {360			return Ok(None);361		};362		v.evaluate().map(Some)363	}364	fn get_cheap(&self, _index: usize) -> Option<Val> {365		None366	}367	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {368		self.0.get(index).cloned()369	}370	fn evaluated(&self) -> Result<Vec<Val>> {371		let mut out = Vec::with_capacity(self.len());372		for i in self.0.iter() {373			out.push(i.evaluate()?);374		}375		Ok(out)376	}377	fn iter(&self) -> LazyArrayIter<'_> {378		self.0.iter().map(Thunk::evaluate)379	}380	fn iter_lazy(&self) -> LazyArrayLazyIter<'_> {381		self.0.iter().cloned()382	}383	fn iter_cheap(&self) -> Option<LazyArrayCheapIter<'_>> {384		None385	}386}387388#[derive(Trace, Debug, Clone)]389pub struct EagerArray(pub Cc<Vec<Val>>);390type EagerArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;391type EagerArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;392type EagerArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;393impl ArrayLike for EagerArray {394	type Iter<'t> = EagerArrayIter<'t>;395396	type IterLazy<'t> = EagerArrayLazyIter<'t>;397398	type IterCheap<'t> = EagerArrayCheapIter<'t>;399400	fn len(&self) -> usize {401		self.0.len()402	}403404	fn get(&self, index: usize) -> Result<Option<Val>> {405		Ok(self.0.get(index).cloned())406	}407408	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {409		self.0.get(index).cloned().map(Thunk::evaluated)410	}411412	fn get_cheap(&self, index: usize) -> Option<Val> {413		self.0.get(index).cloned()414	}415416	fn evaluated(&self) -> Result<Vec<Val>> {417		Ok((*self.0).clone())418	}419420	fn iter(&self) -> EagerArrayIter<'_> {421		self.0.iter().cloned().map(Ok)422	}423424	fn iter_lazy(&self) -> EagerArrayLazyIter<'_> {425		self.0.iter().cloned().map(Thunk::evaluated)426	}427428	fn iter_cheap(&self) -> Option<EagerArrayCheapIter<'_>> {429		Some(self.0.iter().cloned())430	}431}432433/// Inclusive range type434#[derive(Debug, Trace, Clone, PartialEq, Eq)]435pub struct RangeArray {436	start: i32,437	end: i32,438}439struct RangeIter {440	start: i32,441	end: i32,442}443impl RangeIter {444	fn finished(&self) -> bool {445		self.end < self.start446	}447	fn finish(&mut self) {448		self.start = 0;449		self.end = -1;450	}451}452impl Iterator for RangeIter {453	type Item = i32;454455	fn next(&mut self) -> Option<Self::Item> {456		if self.finished() {457			return None;458		}459		let v = self.start;460		if v == self.end {461			self.finish();462		} else {463			self.start = v + 1;464		}465		Some(v)466	}467	fn nth(&mut self, n: usize) -> Option<Self::Item> {468		let v = (self.start as usize) + n;469		if v > self.end as usize {470			self.finish();471			None472		} else {473			self.start = v as i32;474			self.next()475		}476	}477	fn size_hint(&self) -> (usize, Option<usize>) {478		let len = self.len();479		(len, Some(len))480	}481}482impl DoubleEndedIterator for RangeIter {483	fn next_back(&mut self) -> Option<Self::Item> {484		if self.finished() {485			return None;486		}487		let v = self.end;488		if v == self.start {489			self.finish();490		} else {491			self.end = v - 1;492		}493		Some(v)494	}495	fn nth_back(&mut self, n: usize) -> Option<Self::Item> {496		let v = (self.end as usize) - n;497		if v < self.start as usize {498			self.finish();499			None500		} else {501			self.end = v as i32;502			self.next_back()503		}504	}505}506impl ExactSizeIterator for RangeIter {507	fn len(&self) -> usize {508		if self.finished() {509			0510		} else {511			(self.end as isize - self.start as isize + 1) as usize512		}513	}514}515impl RangeArray {516	pub fn empty() -> Self {517		Self::new_exclusive(0, 0)518	}519	pub fn new_exclusive(start: i32, end: i32) -> Self {520		end.checked_sub(1)521			.map_or_else(Self::empty, |end| Self { start, end })522	}523	pub fn new_inclusive(start: i32, end: i32) -> Self {524		Self { start, end }525	}526	fn range(&self) -> RangeIter {527		RangeIter {528			start: self.start,529			end: self.end,530		}531	}532}533534type RangeArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;535type RangeArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;536type RangeArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;537impl ArrayLike for RangeArray {538	type Iter<'t> = RangeArrayIter<'t>;539540	type IterLazy<'t> = RangeArrayLazyIter<'t>;541542	type IterCheap<'t> = RangeArrayCheapIter<'t>;543544	fn len(&self) -> usize {545		self.range().len()546	}547	fn is_empty(&self) -> bool {548		self.range().finished()549	}550551	fn get(&self, index: usize) -> Result<Option<Val>> {552		Ok(self.get_cheap(index))553	}554555	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {556		self.get_cheap(index).map(Thunk::evaluated)557	}558559	fn get_cheap(&self, index: usize) -> Option<Val> {560		self.range().nth(index).map(|i| Val::Num(f64::from(i)))561	}562563	fn evaluated(&self) -> Result<Vec<Val>> {564		Ok(self.range().map(|i| Val::Num(f64::from(i))).collect())565	}566567	fn iter(&self) -> RangeArrayIter<'_> {568		self.range().map(|i| Ok(Val::Num(f64::from(i))))569	}570571	fn iter_lazy(&self) -> RangeArrayLazyIter<'_> {572		self.range()573			.map(|i| Thunk::evaluated(Val::Num(f64::from(i))))574	}575576	fn iter_cheap(&self) -> Option<RangeArrayCheapIter<'_>> {577		Some(self.range().map(|i| Val::Num(f64::from(i))))578	}579}580581#[derive(Debug, Trace, Clone)]582pub struct ReverseArray(pub ArrValue);583impl ArrayLike for ReverseArray {584	type Iter<'t> = Rev<UnknownArrayIter<'t>>;585586	type IterLazy<'t> = Rev<UnknownArrayIterLazy<'t>>;587588	type IterCheap<'t> = Rev<UnknownArrayIterCheap<'t>>;589590	fn len(&self) -> usize {591		self.0.len()592	}593594	fn get(&self, index: usize) -> Result<Option<Val>> {595		self.0.get(self.0.len() - index - 1)596	}597598	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {599		self.0.get_lazy(self.0.len() - index - 1)600	}601602	fn get_cheap(&self, index: usize) -> Option<Val> {603		self.0.get_cheap(self.0.len() - index - 1)604	}605606	fn evaluated(&self) -> Result<Vec<Val>> {607		let mut v = self.0.evaluated()?;608		v.reverse();609		Ok(v)610	}611612	fn iter(&self) -> Rev<UnknownArrayIter<'_>> {613		self.0.iter().rev()614	}615616	fn iter_lazy(&self) -> Rev<UnknownArrayIterLazy<'_>> {617		self.0.iter_lazy().rev()618	}619620	fn iter_cheap(&self) -> Option<Rev<UnknownArrayIterCheap<'_>>> {621		Some(self.0.iter_cheap()?.rev())622	}623}624625#[derive(Trace, Debug)]626pub struct MappedArrayInner {627	inner: ArrValue,628	cached: RefCell<Vec<ArrayThunk<()>>>,629	mapper: FuncVal,630}631#[derive(Trace, Debug, Clone)]632pub struct MappedArray(Cc<MappedArrayInner>);633impl MappedArray {634	pub fn new(inner: ArrValue, mapper: FuncVal) -> Self {635		let len = inner.len();636		Self(Cc::new(MappedArrayInner {637			inner,638			cached: RefCell::new(vec![ArrayThunk::Waiting(()); len]),639			mapper,640		}))641	}642}643type MappedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;644type MappedArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;645type MappedArrayCheapIter<'t> = iter::Empty<Val>;646impl ArrayLike for MappedArray {647	type Iter<'t> = MappedArrayIter<'t>;648	type IterLazy<'t> = MappedArrayLazyIter<'t>;649	type IterCheap<'t> = MappedArrayCheapIter<'t>;650651	fn len(&self) -> usize {652		self.0.cached.borrow().len()653	}654655	fn get(&self, index: usize) -> Result<Option<Val>> {656		if index >= self.len() {657			return Ok(None);658		}659		match &self.0.cached.borrow()[index] {660			ArrayThunk::Computed(c) => return Ok(Some(c.clone())),661			ArrayThunk::Errored(e) => return Err(e.clone()),662			ArrayThunk::Pending => return Err(InfiniteRecursionDetected.into()),663			ArrayThunk::Waiting(..) => {}664		};665666		let ArrayThunk::Waiting(_) = replace(&mut self.0.cached.borrow_mut()[index], ArrayThunk::Pending) else {667			unreachable!()668		};669670		let val = self671			.0672			.inner673			.get(index)674			.transpose()675			.expect("index checked")676			.and_then(|r| self.0.mapper.evaluate_simple(&(Any(r),)));677678		let new_value = match val {679			Ok(v) => v,680			Err(e) => {681				self.0.cached.borrow_mut()[index] = ArrayThunk::Errored(e.clone());682				return Err(e);683			}684		};685		self.0.cached.borrow_mut()[index] = ArrayThunk::Computed(new_value.clone());686		Ok(Some(new_value))687	}688	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {689		#[derive(Trace)]690		struct ArrayElement {691			arr_thunk: MappedArray,692			index: usize,693		}694695		impl ThunkValue for ArrayElement {696			type Output = Val;697698			fn get(self: Box<Self>) -> Result<Self::Output> {699				self.arr_thunk700					.get(self.index)701					.transpose()702					.expect("index checked")703			}704		}705706		if index >= self.len() {707			return None;708		}709		match &self.0.cached.borrow()[index] {710			ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())),711			ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())),712			ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}713		};714715		Some(Thunk::new(tb!(ArrayElement {716			arr_thunk: self.clone(),717			index,718		})))719	}720721	fn get_cheap(&self, _index: usize) -> Option<Val> {722		None723	}724725	fn evaluated(&self) -> Result<Vec<Val>> {726		self.iter().collect()727	}728729	fn iter(&self) -> MappedArrayIter<'_> {730		(0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))731	}732733	fn iter_lazy(&self) -> MappedArrayLazyIter<'_> {734		(0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))735	}736737	fn iter_cheap(&self) -> Option<Self::IterCheap<'_>> {738		None739	}740}741// impl MappedArray742743macro_rules! impl_iter_enum {744	($n:ident => $v:ident) => {745		pub enum $n<'t> {746			Bytes(<BytesArray as ArrayLike>::$v<'t>),747			Expr(<ExprArray as ArrayLike>::$v<'t>),748			Lazy(<LazyArray as ArrayLike>::$v<'t>),749			Eager(<EagerArray as ArrayLike>::$v<'t>),750			Range(<RangeArray as ArrayLike>::$v<'t>),751			Slice(Box<<SliceArray as ArrayLike>::$v<'t>>),752			Extended(Box<<ExtendedArray as ArrayLike>::$v<'t>>),753			Reverse(Box<<ReverseArray as ArrayLike>::$v<'t>>),754			Mapped(Box<<MappedArray as ArrayLike>::$v<'t>>),755		}756	};757}758759macro_rules! pass {760	($t:ident.$m:ident($($ident:ident),*)) => {761		match $t {762			Self::Bytes(e) => e.$m($($ident)*),763			Self::Expr(e) => e.$m($($ident)*),764			Self::Lazy(e) => e.$m($($ident)*),765			Self::Eager(e) => e.$m($($ident)*),766			Self::Range(e) => e.$m($($ident)*),767			Self::Slice(e) => e.$m($($ident)*),768			Self::Extended(e) => e.$m($($ident)*),769			Self::Reverse(e) => e.$m($($ident)*),770			Self::Mapped(e) => e.$m($($ident)*),771		}772	};773}774pub(super) use pass;775776macro_rules! pass_iter_call {777	($t:ident.$c:ident $(in $wrap:ident)? => $e:ident) => {778		match $t {779			ArrValue::Bytes(e) => $e::Bytes($($wrap!)?(e.$c())),780			ArrValue::Lazy(e) => $e::Lazy($($wrap!)?(e.$c())),781			ArrValue::Expr(e) => $e::Expr($($wrap!)?(e.$c())),782			ArrValue::Eager(e) => $e::Eager($($wrap!)?(e.$c())),783			ArrValue::Range(e) => $e::Range($($wrap!)?(e.$c())),784			ArrValue::Slice(e) => $e::Slice(Box::new($($wrap!)?(e.$c()))),785			ArrValue::Extended(e) => $e::Extended(Box::new($($wrap!)?(e.$c()))),786			ArrValue::Reverse(e) => $e::Reverse(Box::new($($wrap!)?(e.$c()))),787			ArrValue::Mapped(e) => $e::Mapped(Box::new($($wrap!)?(e.$c()))),788		}789	};790}791pub(super) use pass_iter_call;792793macro_rules! impl_iter {794	($t:ident => $out:ty) => {795		impl Iterator for $t<'_> {796			type Item = $out;797798			fn next(&mut self) -> Option<Self::Item> {799				pass!(self.next())800			}801			fn nth(&mut self, count: usize) -> Option<Self::Item> {802				pass!(self.nth(count))803			}804			fn size_hint(&self) -> (usize, Option<usize>) {805				pass!(self.size_hint())806			}807		}808		impl DoubleEndedIterator for $t<'_> {809			fn next_back(&mut self) -> Option<Self::Item> {810				pass!(self.next_back())811			}812			fn nth_back(&mut self, count: usize) -> Option<Self::Item> {813				pass!(self.nth_back(count))814			}815		}816		impl ExactSizeIterator for $t<'_> {817			fn len(&self) -> usize {818				match self {819					Self::Bytes(e) => e.len(),820					Self::Expr(e) => e.len(),821					Self::Lazy(e) => e.len(),822					Self::Eager(e) => e.len(),823					Self::Range(e) => e.len(),824					Self::Slice(e) => e.len(),825					Self::Extended(e) => {826						e.size_hint().1.expect("overflow is checked in constructor")827					}828					Self::Reverse(e) => e.len(),829					Self::Mapped(e) => e.len(),830				}831			}832		}833	};834}835836impl_iter_enum!(UnknownArrayIter => Iter);837impl_iter_enum!(UnknownArrayIterLazy => IterLazy);838impl_iter_enum!(UnknownArrayIterCheap => IterCheap);839impl_iter!(UnknownArrayIter => Result<Val>);840impl_iter!(UnknownArrayIterLazy => Thunk<Val>);841impl_iter!(UnknownArrayIterCheap => Val);
modifiedcrates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -12,7 +12,9 @@
 	native::NativeDesc,
 	parse::{parse_default_function_call, parse_function_call},
 };
-use crate::{evaluate, gc::TraceBox, typed::Any, Context, ContextBuilder, Result, Val};
+use crate::{
+	evaluate, evaluate_trivial, gc::TraceBox, typed::Any, Context, ContextBuilder, Result, Val,
+};
 
 pub mod arglike;
 pub mod builtin;
@@ -80,6 +82,10 @@
 	) -> Result<Context> {
 		parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)
 	}
+
+	pub fn evaluate_trivial(&self) -> Option<Val> {
+		evaluate_trivial(&self.body)
+	}
 }
 
 /// Represents a Jsonnet function value, including plain functions and user-provided builtins.
@@ -201,4 +207,11 @@
 	pub const fn identity() -> Self {
 		Self::Id
 	}
+
+	pub fn evaluate_trivial(&self) -> Option<Val> {
+		match self {
+			FuncVal::Normal(n) => n.evaluate_trivial(),
+			_ => None,
+		}
+	}
 }
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -1,23 +1,41 @@
 use jrsonnet_evaluator::{
-	error::Result,
+	error::{ErrorKind::RuntimeError, Result},
 	function::{builtin, FuncVal},
 	throw,
-	typed::{Any, BoundedUsize, Either2, NativeFn, Typed, VecVal},
-	val::{equals, ArrValue, IndexableVal},
+	typed::{Any, BoundedI32, BoundedUsize, Either2, NativeFn, Typed},
+	val::{equals, ArrValue, IndexableVal, StrValue},
 	Either, IStr, Val,
 };
 use jrsonnet_gcmodule::Cc;
 
 #[builtin]
-pub fn builtin_make_array(sz: usize, func: NativeFn<((f64,), Any)>) -> Result<VecVal> {
-	let mut out = Vec::with_capacity(sz);
-	for i in 0..sz {
-		out.push(func(i as f64)?.0);
+pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {
+	if *sz == 0 {
+		return Ok(ArrValue::empty());
+	}
+	if let Some(trivial) = func.evaluate_trivial() {
+		let mut out = Vec::with_capacity(*sz as usize);
+		for _ in 0..*sz {
+			out.push(trivial.clone())
+		}
+		Ok(ArrValue::eager(Cc::new(out)))
+	} else {
+		Ok(ArrValue::range_exclusive(0, *sz).map(func))
 	}
-	Ok(VecVal(Cc::new(out)))
 }
 
 #[builtin]
+pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Any> {
+	Ok(Any(match what {
+		Either2::A(s) => Val::Str(StrValue::Flat(s.repeat(count).into())),
+		Either2::B(arr) => Val::Arr(
+			ArrValue::repeated(arr, count)
+				.ok_or_else(|| RuntimeError("repeated length overflow".into()))?,
+		),
+	}))
+}
+
+#[builtin]
 pub fn builtin_slice(
 	indexable: IndexableVal,
 	index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -63,6 +63,7 @@
 		("isFunction", builtin_is_function::INST),
 		// Arrays
 		("makeArray", builtin_make_array::INST),
+		("repeat", builtin_repeat::INST),
 		("slice", builtin_slice::INST),
 		("map", builtin_map::INST),
 		("flatMap", builtin_flatmap::INST),
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -27,13 +27,6 @@
 
   split(str, c):: std.splitLimit(str, c, -1),
 
-  repeat(what, count)::
-    local joiner =
-      if std.isString(what) then ''
-      else if std.isArray(what) then []
-      else error 'std.repeat first argument must be an array or a string';
-    std.join(joiner, std.makeArray(count, function(i) what)),
-
   mapWithIndex(func, arr)::
     if !std.isFunction(func) then
       error ('std.mapWithIndex first param must be function, got ' + std.type(func))
addednix/jrsonnet-release.nixdiffbeforeafterboth
--- /dev/null
+++ b/nix/jrsonnet-release.nix
@@ -0,0 +1,24 @@
+{ lib, fetchFromGitHub, rustPlatform, runCommand, makeWrapper }:
+
+
+rustPlatform.buildRustPackage rec {
+  pname = "jrsonnet";
+  version = "5f0f8de9f52f961e2ff162e0a3fd4ca20a275f1d";
+
+  src = fetchFromGitHub {
+    owner = "CertainLach";
+    repo = pname;
+    rev = version;
+    hash = lib.fakeHash;
+  };
+
+  cargoTestFlags = [ "--package=jrsonnet --features=mimalloc,legacy-this-file" ];
+  cargoBuildFlags = [ "--package=jrsonnet --features=mimalloc,legacy-this-file" ];
+
+  buildInputs = [ makeWrapper ];
+
+  postInstall = ''
+    mv $out/bin/jrsonnet $out/bin/jrsonnet-release
+    wrapProgram $out/bin/jrsonnet-release --add-flags "--max-stack=200000 --os-stack=200000"
+  '';
+}