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.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> {
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.jsonnetdiffbeforeafterboth1{2 local std = self,34 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',56 mapWithIndex(func, arr)::7 if !std.isFunction(func) then8 error ('std.mapWithIndex first param must be function, got ' + std.type(func))9 else if !std.isArray(arr) && !std.isString(arr) then10 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))11 else12 std.makeArray(std.length(arr), function(i) func(i, arr[i])),1314 mapWithKey(func, obj)::15 if !std.isFunction(func) then16 error ('std.mapWithKey first param must be function, got ' + std.type(func))17 else if !std.isObject(obj) then18 error ('std.mapWithKey second param must be object, got ' + std.type(obj))19 else20 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },2122 lines(arr)::23 std.join('\n', arr + ['']),2425 deepJoin(arr)::26 if std.isString(arr) then27 arr28 else if std.isArray(arr) then29 std.join('', [std.deepJoin(x) for x in arr])30 else31 error 'Expected string or array, got %s' % std.type(arr),3233 assertEqual(a, b)::34 if a == b then35 true36 else37 error 'Assertion failed. ' + a + ' != ' + b,3839 manifestIni(ini)::40 local body_lines(body) =41 std.join([], [42 local value_or_values = body[k];43 if std.isArray(value_or_values) then44 ['%s = %s' % [k, value] for value in value_or_values]45 else46 ['%s = %s' % [k, value_or_values]]4748 for k in std.objectFields(body)49 ]);5051 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),52 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],53 all_sections = [54 section_lines(k, ini.sections[k])55 for k in std.objectFields(ini.sections)56 ];57 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),5859 mergePatch(target, patch)::60 if std.isObject(patch) then61 local target_object =62 if std.isObject(target) then target else {};6364 local target_fields =65 if std.isObject(target_object) then std.objectFields(target_object) else [];6667 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];68 local both_fields = std.setUnion(target_fields, std.objectFields(patch));6970 {71 [k]:72 if !std.objectHas(patch, k) then73 target_object[k]74 else if !std.objectHas(target_object, k) then75 std.mergePatch(null, patch[k]) tailstrict76 else77 std.mergePatch(target_object[k], patch[k]) tailstrict78 for k in std.setDiff(both_fields, null_fields)79 }80 else81 patch,8283 resolvePath(f, r)::84 local arr = std.split(f, '/');85 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),8687 find(value, arr)::88 if !std.isArray(arr) then89 error 'find second parameter should be an array, got ' + std.type(arr)90 else91 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),92}