difftreelog
perf move mapWithIndex to native
in: master
5 files changed
crates/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> {
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth430}430}431431432#[derive(Trace, Debug, Clone)]432#[derive(Trace, Debug, Clone)]433pub struct MappedArray {433pub struct MappedArray<const WithIndex: bool> {434 inner: ArrValue,434 inner: ArrValue,435 cached: Cc<RefCell<Vec<ArrayThunk<()>>>>,435 cached: Cc<RefCell<Vec<ArrayThunk<()>>>>,436 mapper: FuncVal,436 mapper: FuncVal,437}437}438impl MappedArray {438impl<const WithIndex: bool> MappedArray<WithIndex> {439 pub fn new(inner: ArrValue, mapper: FuncVal) -> Self {439 pub fn new(inner: ArrValue, mapper: FuncVal) -> Self {440 let len = inner.len();440 let len = inner.len();441 Self {441 Self {444 mapper,444 mapper,445 }445 }446 }446 }447 fn evaluate(&self, index: usize, value: Val) -> Result<Val> {448 if WithIndex {449 self.mapper.evaluate_simple(&(index, value), false)450 } else {451 self.mapper.evaluate_simple(&(value,), false)452 }453 }447}454}448impl ArrayLike for MappedArray {455impl<const WithIndex: bool> ArrayLike for MappedArray<WithIndex> {449 fn len(&self) -> usize {456 fn len(&self) -> usize {450 self.cached.borrow().len()457 self.cached.borrow().len()451 }458 }472 .get(index)479 .get(index)473 .transpose()480 .transpose()474 .expect("index checked")481 .expect("index checked")475 .and_then(|r| self.mapper.evaluate_simple(&(r,), false));482 .and_then(|r| self.evaluate(index, r));476483477 let new_value = match val {484 let new_value = match val {478 Ok(v) => v,485 Ok(v) => v,486 }493 }487 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {494 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {488 #[derive(Trace)]495 #[derive(Trace)]489 struct ArrayElement {496 struct ArrayElement<const WithIndex: bool> {490 arr_thunk: MappedArray,497 arr_thunk: MappedArray<WithIndex>,491 index: usize,498 index: usize,492 }499 }493500494 impl ThunkValue for ArrayElement {501 impl<const WithIndex: bool> ThunkValue for ArrayElement<WithIndex> {495 type Output = Val;502 type Output = Val;496503497 fn get(self: Box<Self>) -> Result<Self::Output> {504 fn get(self: Box<Self>) -> Result<Self::Output> {crates/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,
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -78,6 +78,7 @@
("repeat", builtin_repeat::INST),
("slice", builtin_slice::INST),
("map", builtin_map::INST),
+ ("mapWithIndex", builtin_map_with_index::INST),
("flatMap", builtin_flatmap::INST),
("filter", builtin_filter::INST),
("foldl", builtin_foldl::INST),
crates/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))