difftreelog
perf move mapWithIndex to native
in: master
5 files changed
crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth545455 #[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 }5960 #[must_use]61 pub fn map_with_index(self, mapper: FuncVal) -> Self {62 Self::new(<MappedArray<true>>::new(self, mapper))63 }596460 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 arrayscrates/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.rsdiffbeforeafterboth61 arr.map(func)61 arr.map(func)62}62}6364#[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}636964#[builtin]70#[builtin]65pub fn builtin_flatmap(71pub fn builtin_flatmap(crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth78 ("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),crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth334 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',556 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)::6 mapWithKey(func, obj)::15 if !std.isFunction(func) then7 if !std.isFunction(func) then16 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))