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
5454
55 #[must_use]55 #[must_use]
56 pub fn map(self, mapper: FuncVal) -> Self {56 pub fn map(self, mapper: FuncVal) -> Self {
57 Self::new(MappedArray::new(self, mapper))57 Self::new(<MappedArray<false>>::new(self, mapper))
58 }58 }
59
60 #[must_use]
61 pub fn map_with_index(self, mapper: FuncVal) -> Self {
62 Self::new(<MappedArray<true>>::new(self, mapper))
63 }
5964
60 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {65 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
61 // TODO: ArrValue::Picked(inner, indexes) for large arrays66 // TODO: ArrValue::Picked(inner, indexes) for large arrays
modifiedcrates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth
430}430}
431431
432#[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));
476483
477 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 }
493500
494 impl ThunkValue for ArrayElement {501 impl<const WithIndex: bool> ThunkValue for ArrayElement<WithIndex> {
495 type Output = Val;502 type Output = Val;
496503
497 fn get(self: Box<Self>) -> Result<Self::Output> {504 fn get(self: Box<Self>) -> Result<Self::Output> {
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
61 arr.map(func)61 arr.map(func)
62}62}
63
64#[builtin]
65pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {
66 let arr = arr.to_array();
67 arr.map_with_index(func)
68}
6369
64#[builtin]70#[builtin]
65pub fn builtin_flatmap(71pub fn builtin_flatmap(
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
33
4 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',4 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',
55
6 mapWithIndex(func, arr)::
7 if !std.isFunction(func) then
8 error ('std.mapWithIndex first param must be function, got ' + std.type(func))
9 else if !std.isArray(arr) && !std.isString(arr) then
10 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))
11 else
12 std.makeArray(std.length(arr), function(i) func(i, arr[i])),
13
14 mapWithKey(func, obj)::6 mapWithKey(func, obj)::
15 if !std.isFunction(func) then7 if !std.isFunction(func) then
16 error ('std.mapWithKey first param must be function, got ' + std.type(func))8 error ('std.mapWithKey first param must be function, got ' + std.type(func))