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

difftreelog

perf faster std.map

Yaroslav Bolyukin2021-02-20parent: #643b007.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/build.rsdiffbeforeafterboth
40 name == "base64" || name == "foldl" || name == "foldr" ||40 name == "base64" || name == "foldl" || name == "foldr" ||
41 name == "sortImpl" || name == "format" || name == "range" ||41 name == "sortImpl" || name == "format" || name == "range" ||
42 name == "reverse" || name == "slice" || name == "mod" ||42 name == "reverse" || name == "slice" || name == "mod" ||
43 name == "strReplace"43 name == "strReplace" || name == "map"
44 )44 )
45 })45 })
46 .collect(),46 .collect(),
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
57 ("extVar".into(), builtin_ext_var),57 ("extVar".into(), builtin_ext_var),
58 ("native".into(), builtin_native),58 ("native".into(), builtin_native),
59 ("filter".into(), builtin_filter),59 ("filter".into(), builtin_filter),
60 ("map".into(), builtin_map),
60 ("foldl".into(), builtin_foldl),61 ("foldl".into(), builtin_foldl),
61 ("foldr".into(), builtin_foldr),62 ("foldr".into(), builtin_foldr),
62 ("sortImpl".into(), builtin_sort_impl),63 ("sortImpl".into(), builtin_sort_impl),
294 0, func: ty!(function) => Val::Func;295 0, func: ty!(function) => Val::Func;
295 1, arr: ty!(array) => Val::Arr;296 1, arr: ty!(array) => Val::Arr;
296 ], {297 ], {
297 let mut out = Vec::new();298 Ok(Val::Arr(arr.filter(|val| func
298 for item in arr.iter() {
299 let item = item?;
300 if func
301 .evaluate_values(context.clone(), &[item.clone()])?299 .evaluate_values(context.clone(), &[val.clone()])?
302 .try_cast_bool("filter predicate")? {300 .try_cast_bool("filter predicate"))?))
303 out.push(item);
304 }
305 }
306 Ok(Val::Arr(out.into()))
307 })301 })
308}302}
303
304fn builtin_map(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
305 parse_args!(context, "map", args, 2, [
306 0, func: ty!(function) => Val::Func;
307 1, arr: ty!(array) => Val::Arr;
308 ], {
309 Ok(Val::Arr(arr.map(|val| func
310 .evaluate_values(context.clone(), &[val]))?))
311 })
312}
309313
310fn builtin_foldl(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {314fn builtin_foldl(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
311 parse_args!(context, "foldl", args, 3, [315 parse_args!(context, "foldl", args, 3, [
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
283 }283 }
284 }284 }
285
286 pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {
287 let mut out = Vec::with_capacity(self.len());
288
289 for value in self.iter() {
290 out.push(mapper(value?)?);
291 }
292
293 Ok(Self::Eager(Rc::new(out)))
294 }
295
296 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
297 let mut out = Vec::with_capacity(self.len());
298
299 for value in self.iter() {
300 let value = value?;
301 if filter(&value)? {
302 out.push(value);
303 }
304 }
305
306 Ok(Self::Eager(Rc::new(out)))
307 }
285308
286 pub fn ptr_eq(a: &Self, b: &Self) -> bool {309 pub fn ptr_eq(a: &Self, b: &Self) -> bool {
287 match (a, b) {310 match (a, b) {