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
--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -740,6 +740,96 @@
 }
 // impl MappedArray
 
+#[derive(Trace, Debug)]
+pub struct RepeatedArrayInner {
+	data: ArrValue,
+	repeats: usize,
+	total_len: usize,
+}
+#[derive(Trace, Debug, Clone)]
+pub struct RepeatedArray(Cc<RepeatedArrayInner>);
+impl RepeatedArray {
+	pub fn new(data: ArrValue, repeats: usize) -> Option<Self> {
+		let total_len = data.len().checked_mul(repeats)?;
+		Some(Self(Cc::new(RepeatedArrayInner {
+			data,
+			repeats,
+			total_len,
+		})))
+	}
+	pub fn is_cheap(&self) -> bool {
+		self.0.data.is_cheap()
+	}
+}
+
+type RepeatedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+type RepeatedArrayLazyIter<'t> =
+	impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+type RepeatedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
+impl ArrayLike for RepeatedArray {
+	type Iter<'t> = RepeatedArrayIter<'t>;
+	type IterLazy<'t> = RepeatedArrayLazyIter<'t>;
+	type IterCheap<'t> = RepeatedArrayCheapIter<'t>;
+
+	fn len(&self) -> usize {
+		self.0.total_len
+	}
+
+	fn get(&self, index: usize) -> Result<Option<Val>> {
+		if index > self.0.total_len {
+			return Ok(None);
+		}
+		self.0.data.get(index % self.0.data.len())
+	}
+
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		if index > self.0.total_len {
+			return None;
+		}
+		self.0.data.get_lazy(index % self.0.data.len())
+	}
+
+	fn get_cheap(&self, index: usize) -> Option<Val> {
+		if index > self.0.total_len {
+			return None;
+		}
+		self.0.data.get_cheap(index % self.0.data.len())
+	}
+
+	fn evaluated(&self) -> Result<Vec<Val>> {
+		let mut data = self.0.data.evaluated()?;
+		let data_range = 0..data.len();
+		for _ in 1..self.0.repeats {
+			data.extend_from_within(data_range.clone());
+		}
+		Ok(data)
+	}
+
+	fn iter(&self) -> RepeatedArrayIter<'_> {
+		(0..self.0.total_len)
+			.map(|i| self.get(i))
+			.map(Result::transpose)
+			.map(Option::unwrap)
+	}
+
+	fn iter_lazy(&self) -> RepeatedArrayLazyIter<'_> {
+		(0..self.0.total_len)
+			.map(|i| self.get_lazy(i))
+			.map(Option::unwrap)
+	}
+
+	fn iter_cheap(&self) -> Option<RepeatedArrayCheapIter<'_>> {
+		if !self.0.data.is_cheap() {
+			return None;
+		}
+		Some(
+			(0..self.0.total_len)
+				.map(|i| self.get_cheap(i))
+				.map(Option::unwrap),
+		)
+	}
+}
+
 macro_rules! impl_iter_enum {
 	($n:ident => $v:ident) => {
 		pub enum $n<'t> {
@@ -752,6 +842,7 @@
 			Extended(Box<<ExtendedArray as ArrayLike>::$v<'t>>),
 			Reverse(Box<<ReverseArray as ArrayLike>::$v<'t>>),
 			Mapped(Box<<MappedArray as ArrayLike>::$v<'t>>),
+			Repeated(Box<<RepeatedArray as ArrayLike>::$v<'t>>),
 		}
 	};
 }
@@ -768,6 +859,7 @@
 			Self::Extended(e) => e.$m($($ident)*),
 			Self::Reverse(e) => e.$m($($ident)*),
 			Self::Mapped(e) => e.$m($($ident)*),
+			Self::Repeated(e) => e.$m($($ident)*),
 		}
 	};
 }
@@ -785,6 +877,7 @@
 			ArrValue::Extended(e) => $e::Extended(Box::new($($wrap!)?(e.$c()))),
 			ArrValue::Reverse(e) => $e::Reverse(Box::new($($wrap!)?(e.$c()))),
 			ArrValue::Mapped(e) => $e::Mapped(Box::new($($wrap!)?(e.$c()))),
+			ArrValue::Repeated(e) => $e::Repeated(Box::new($($wrap!)?(e.$c()))),
 		}
 	};
 }
@@ -827,6 +920,7 @@
 					}
 					Self::Reverse(e) => e.len(),
 					Self::Mapped(e) => e.len(),
+					Self::Repeated(e) => e.len(),
 				}
 			}
 		}
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
before · crates/jrsonnet-stdlib/src/arrays.rs
1use jrsonnet_evaluator::{2	error::Result,3	function::{builtin, FuncVal},4	throw,5	typed::{Any, BoundedUsize, Either2, NativeFn, Typed, VecVal},6	val::{equals, ArrValue, IndexableVal},7	Either, IStr, Val,8};9use jrsonnet_gcmodule::Cc;1011#[builtin]12pub fn builtin_make_array(sz: usize, func: NativeFn<((f64,), Any)>) -> Result<VecVal> {13	let mut out = Vec::with_capacity(sz);14	for i in 0..sz {15		out.push(func(i as f64)?.0);16	}17	Ok(VecVal(Cc::new(out)))18}1920#[builtin]21pub fn builtin_slice(22	indexable: IndexableVal,23	index: Option<BoundedUsize<0, { i32::MAX as usize }>>,24	end: Option<BoundedUsize<0, { i32::MAX as usize }>>,25	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,26) -> Result<Any> {27	indexable.slice(index, end, step).map(Val::from).map(Any)28}2930#[builtin]31pub fn builtin_map(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {32	Ok(arr.map(func))33}3435#[builtin]36pub fn builtin_flatmap(37	func: NativeFn<((Either![String, Any],), Any)>,38	arr: IndexableVal,39) -> Result<IndexableVal> {40	use std::fmt::Write;41	match arr {42		IndexableVal::Str(str) => {43			let mut out = String::new();44			for c in str.chars() {45				match func(Either2::A(c.to_string()))?.0 {46					Val::Str(o) => write!(out, "{o}").unwrap(),47					Val::Null => continue,48					_ => throw!("in std.join all items should be strings"),49				};50			}51			Ok(IndexableVal::Str(out.into()))52		}53		IndexableVal::Arr(a) => {54			let mut out = Vec::new();55			for el in a.iter() {56				let el = el?;57				match func(Either2::B(Any(el)))?.0 {58					Val::Arr(o) => {59						for oe in o.iter() {60							out.push(oe?);61						}62					}63					Val::Null => continue,64					_ => throw!("in std.join all items should be arrays"),65				};66			}67			Ok(IndexableVal::Arr(out.into()))68		}69	}70}7172#[builtin]73pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {74	arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(Any(val.clone()),))?))75}7677#[builtin]78pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {79	let mut acc = init.0;80	for i in arr.iter() {81		acc = func.evaluate_simple(&(Any(acc), Any(i?)))?;82	}83	Ok(Any(acc))84}8586#[builtin]87pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {88	let mut acc = init.0;89	for i in arr.iter().rev() {90		acc = func.evaluate_simple(&(Any(i?), Any(acc)))?;91	}92	Ok(Any(acc))93}9495#[builtin]96pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {97	if to < from {98		return Ok(ArrValue::empty());99	}100	Ok(ArrValue::range_inclusive(from, to))101}102103#[builtin]104pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {105	use std::fmt::Write;106	Ok(match sep {107		IndexableVal::Arr(joiner_items) => {108			let mut out = Vec::new();109110			let mut first = true;111			for item in arr.iter() {112				let item = item?.clone();113				if let Val::Arr(items) = item {114					if !first {115						out.reserve(joiner_items.len());116						// TODO: extend117						for item in joiner_items.iter() {118							out.push(item?);119						}120					}121					first = false;122					out.reserve(items.len());123					for item in items.iter() {124						out.push(item?);125					}126				} else if matches!(item, Val::Null) {127					continue;128				} else {129					throw!("in std.join all items should be arrays");130				}131			}132133			IndexableVal::Arr(out.into())134		}135		IndexableVal::Str(sep) => {136			let mut out = String::new();137138			let mut first = true;139			for item in arr.iter() {140				let item = item?.clone();141				if let Val::Str(item) = item {142					if !first {143						out += &sep;144					}145					first = false;146					write!(out, "{item}").unwrap()147				} else if matches!(item, Val::Null) {148					continue;149				} else {150					throw!("in std.join all items should be strings");151				}152			}153154			IndexableVal::Str(out.into())155		}156	})157}158159#[builtin]160pub fn builtin_reverse(value: ArrValue) -> Result<ArrValue> {161	Ok(value.reversed())162}163164#[builtin]165pub fn builtin_any(arr: ArrValue) -> Result<bool> {166	for v in arr.iter() {167		let v = bool::from_untyped(v?)?;168		if v {169			return Ok(true);170		}171	}172	Ok(false)173}174175#[builtin]176pub fn builtin_all(arr: ArrValue) -> Result<bool> {177	for v in arr.iter() {178		let v = bool::from_untyped(v?)?;179		if !v {180			return Ok(false);181		}182	}183	Ok(true)184}185186#[builtin]187pub fn builtin_member(arr: IndexableVal, x: Any) -> Result<bool> {188	match arr {189		IndexableVal::Str(str) => {190			let x: IStr = IStr::from_untyped(x.0)?;191			Ok(!x.is_empty() && str.contains(&*x))192		}193		IndexableVal::Arr(a) => {194			for item in a.iter() {195				let item = item?;196				if equals(&item, &x.0)? {197					return Ok(true);198				}199			}200			Ok(false)201		}202	}203}204205#[builtin]206pub fn builtin_count(arr: Vec<Any>, v: Any) -> Result<usize> {207	let mut count = 0;208	for item in &arr {209		if equals(&item.0, &v.0)? {210			count += 1;211		}212	}213	Ok(count)214}
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"
+  '';
+}