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

difftreelog

perf move mapWithIndex to native

Yaroslav Bolyukin2024-06-18parent: #d5d1e03.patch.diff
in: master

5 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
@@ -54,7 +54,12 @@
 
 	#[must_use]
 	pub fn map(self, mapper: FuncVal) -> Self {
-		Self::new(MappedArray::new(self, mapper))
+		Self::new(<MappedArray<false>>::new(self, mapper))
+	}
+
+	#[must_use]
+	pub fn map_with_index(self, mapper: FuncVal) -> Self {
+		Self::new(<MappedArray<true>>::new(self, mapper))
 	}
 
 	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
modifiedcrates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -430,12 +430,12 @@
 }
 
 #[derive(Trace, Debug, Clone)]
-pub struct MappedArray {
+pub struct MappedArray<const WithIndex: bool> {
 	inner: ArrValue,
 	cached: Cc<RefCell<Vec<ArrayThunk<()>>>>,
 	mapper: FuncVal,
 }
-impl MappedArray {
+impl<const WithIndex: bool> MappedArray<WithIndex> {
 	pub fn new(inner: ArrValue, mapper: FuncVal) -> Self {
 		let len = inner.len();
 		Self {
@@ -444,8 +444,15 @@
 			mapper,
 		}
 	}
+	fn evaluate(&self, index: usize, value: Val) -> Result<Val> {
+		if WithIndex {
+			self.mapper.evaluate_simple(&(index, value), false)
+		} else {
+			self.mapper.evaluate_simple(&(value,), false)
+		}
+	}
 }
-impl ArrayLike for MappedArray {
+impl<const WithIndex: bool> ArrayLike for MappedArray<WithIndex> {
 	fn len(&self) -> usize {
 		self.cached.borrow().len()
 	}
@@ -472,7 +479,7 @@
 			.get(index)
 			.transpose()
 			.expect("index checked")
-			.and_then(|r| self.mapper.evaluate_simple(&(r,), false));
+			.and_then(|r| self.evaluate(index, r));
 
 		let new_value = match val {
 			Ok(v) => v,
@@ -486,12 +493,12 @@
 	}
 	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
 		#[derive(Trace)]
-		struct ArrayElement {
-			arr_thunk: MappedArray,
+		struct ArrayElement<const WithIndex: bool> {
+			arr_thunk: MappedArray<WithIndex>,
 			index: usize,
 		}
 
-		impl ThunkValue for ArrayElement {
+		impl<const WithIndex: bool> ThunkValue for ArrayElement<WithIndex> {
 			type Output = Val;
 
 			fn get(self: Box<Self>) -> Result<Self::Output> {
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -62,6 +62,12 @@
 }
 
 #[builtin]
+pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {
+	let arr = arr.to_array();
+	arr.map_with_index(func)
+}
+
+#[builtin]
 pub fn builtin_flatmap(
 	func: NativeFn<((Either![String, Val],), Val)>,
 	arr: IndexableVal,
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
78 ("repeat", builtin_repeat::INST),78 ("repeat", builtin_repeat::INST),
79 ("slice", builtin_slice::INST),79 ("slice", builtin_slice::INST),
80 ("map", builtin_map::INST),80 ("map", builtin_map::INST),
81 ("mapWithIndex", builtin_map_with_index::INST),
81 ("flatMap", builtin_flatmap::INST),82 ("flatMap", builtin_flatmap::INST),
82 ("filter", builtin_filter::INST),83 ("filter", builtin_filter::INST),
83 ("foldl", builtin_foldl::INST),84 ("foldl", builtin_foldl::INST),
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -3,14 +3,6 @@
 
   thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though',
 
-  mapWithIndex(func, arr)::
-    if !std.isFunction(func) then
-      error ('std.mapWithIndex first param must be function, got ' + std.type(func))
-    else if !std.isArray(arr) && !std.isString(arr) then
-      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))
-    else
-      std.makeArray(std.length(arr), function(i) func(i, arr[i])),
-
   mapWithKey(func, obj)::
     if !std.isFunction(func) then
       error ('std.mapWithKey first param must be function, got ' + std.type(func))