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
before · crates/jrsonnet-evaluator/src/function/mod.rs
1use std::fmt::Debug;23pub use arglike::{ArgLike, ArgsLike, TlaArg};4use jrsonnet_gcmodule::{Cc, Trace};5use jrsonnet_interner::IStr;6pub use jrsonnet_macros::builtin;7use jrsonnet_parser::{Destruct, Expr, ExprLocation, LocExpr, ParamsDesc};89use self::{10	arglike::OptionalContext,11	builtin::{Builtin, StaticBuiltin},12	native::NativeDesc,13	parse::{parse_default_function_call, parse_function_call},14};15use crate::{evaluate, gc::TraceBox, typed::Any, Context, ContextBuilder, Result, Val};1617pub mod arglike;18pub mod builtin;19pub mod native;20pub mod parse;2122/// Function callsite location.23/// Either from other jsonnet code, specified by expression location, or from native (without location).24#[derive(Clone, Copy)]25pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);26impl<'l> CallLocation<'l> {27	/// Construct new location for calls coming from specified jsonnet expression location.28	pub const fn new(loc: &'l ExprLocation) -> Self {29		Self(Some(loc))30	}31}32impl CallLocation<'static> {33	/// Construct new location for calls coming from native code.34	pub const fn native() -> Self {35		Self(None)36	}37}3839/// Represents Jsonnet function defined in code.40#[derive(Debug, PartialEq, Trace)]41pub struct FuncDesc {42	/// # Example43	///44	/// In expressions like this, deducted to `a`, unspecified otherwise.45	/// ```jsonnet46	/// local a = function() ...47	/// local a() ...48	/// { a: function() ... }49	/// { a() = ... }50	/// ```51	pub name: IStr,52	/// Context, in which this function was evaluated.53	///54	/// # Example55	/// In56	/// ```jsonnet57	/// local a = 2;58	/// function() ...59	/// ```60	/// context will contain `a`.61	pub ctx: Context,6263	/// Function parameter definition64	pub params: ParamsDesc,65	/// Function body66	pub body: LocExpr,67}68impl FuncDesc {69	/// Create body context, but fill arguments without defaults with lazy error70	pub fn default_body_context(&self) -> Result<Context> {71		parse_default_function_call(self.ctx.clone(), &self.params)72	}7374	/// Create context, with which body code will run75	pub fn call_body_context(76		&self,77		call_ctx: Context,78		args: &dyn ArgsLike,79		tailstrict: bool,80	) -> Result<Context> {81		parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)82	}83}8485/// Represents a Jsonnet function value, including plain functions and user-provided builtins.86#[allow(clippy::module_name_repetitions)]87#[derive(Trace, Clone)]88pub enum FuncVal {89	/// Identity function, kept this way for comparsions.90	Id,91	/// Plain function implemented in jsonnet.92	Normal(Cc<FuncDesc>),93	/// Standard library function.94	StaticBuiltin(#[trace(skip)] &'static dyn StaticBuiltin),95	/// User-provided function.96	Builtin(Cc<TraceBox<dyn Builtin>>),97}9899impl Debug for FuncVal {100	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {101		match self {102			Self::Id => f.debug_tuple("Id").finish(),103			Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),104			Self::StaticBuiltin(arg0) => {105				f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()106			}107			Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),108		}109	}110}111112impl FuncVal {113	/// Amount of non-default required arguments114	pub fn params_len(&self) -> usize {115		match self {116			Self::Id => 1,117			Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),118			Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),119			Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),120		}121	}122	/// Function name, as defined in code.123	pub fn name(&self) -> IStr {124		match self {125			Self::Id => "id".into(),126			Self::Normal(normal) => normal.name.clone(),127			Self::StaticBuiltin(builtin) => builtin.name().into(),128			Self::Builtin(builtin) => builtin.name().into(),129		}130	}131	/// Call function using arguments evaluated in specified `call_ctx` [`Context`].132	///133	/// If `tailstrict` is specified - then arguments will be evaluated before being passed to function body.134	pub fn evaluate(135		&self,136		call_ctx: Context,137		loc: CallLocation<'_>,138		args: &dyn ArgsLike,139		tailstrict: bool,140	) -> Result<Val> {141		match self {142			Self::Id => {143				#[allow(clippy::unnecessary_wraps)]144				#[builtin]145				const fn builtin_id(v: Any) -> Result<Any> {146					Ok(v)147				}148				static ID: &builtin_id = &builtin_id {};149150				ID.call(call_ctx, loc, args)151			}152			Self::Normal(func) => {153				let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;154				evaluate(body_ctx, &func.body)155			}156			Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),157			Self::Builtin(b) => b.call(call_ctx, loc, args),158		}159	}160	pub fn evaluate_simple<A: ArgsLike + OptionalContext>(&self, args: &A) -> Result<Val> {161		self.evaluate(162			ContextBuilder::dangerous_empty_state().build(),163			CallLocation::native(),164			args,165			true,166		)167	}168	/// Convert jsonnet function to plain `Fn` value.169	pub fn into_native<D: NativeDesc>(self) -> D::Value {170		D::into_native(self)171	}172173	/// Is this function an indentity function.174	///175	/// Currently only works for builtin `std.id`, aka `Self::Id` value, and `function(x) x`.176	///177	/// This function should only be used for optimization, not for the conditional logic, i.e code should work with syntetic identity function too178	pub fn is_identity(&self) -> bool {179		match self {180			Self::Id => true,181			Self::Normal(desc) => {182				if desc.params.len() != 1 {183					return false;184				}185				let param = &desc.params[0];186				if param.1.is_some() {187					return false;188				}189				#[allow(clippy::infallible_destructuring_match)]190				let id = match &param.0 {191					Destruct::Full(id) => id,192					#[cfg(feature = "exp-destruct")]193					_ => return false,194				};195				&desc.body.0 as &Expr == &Expr::Var(id.clone())196			}197			_ => false,198		}199	}200	/// Identity function value.201	pub const fn identity() -> Self {202		Self::Id203	}204}
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"
+  '';
+}