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

difftreelog

perf implement std.prune in native

Yaroslav Bolyukin2024-04-07parent: #66d8cad.patch.diff
in: master

6 files changed

modifiedcmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth
356impl Printable for Member {356impl Printable for Member {
357 fn print(&self, out: &mut PrintItems) {357 fn print(&self, out: &mut PrintItems) {
358 match self {358 match self {
359 Member::MemberBindStmt(b) => {359 Self::MemberBindStmt(b) => {
360 p!(out, { b.obj_local() })360 p!(out, { b.obj_local() })
361 }361 }
362 Member::MemberAssertStmt(ass) => {362 Self::MemberAssertStmt(ass) => {
363 p!(out, { ass.assertion() })363 p!(out, { ass.assertion() })
364 }364 }
365 Member::MemberFieldNormal(n) => {365 Self::MemberFieldNormal(n) => {
366 p!(out, {n.field_name()} if(n.plus_token().is_some())({n.plus_token()}) {n.visibility()} str(" ") {n.expr()})366 p!(out, {n.field_name()} if(n.plus_token().is_some())({n.plus_token()}) {n.visibility()} str(" ") {n.expr()})
367 }367 }
368 Member::MemberFieldMethod(m) => {368 Self::MemberFieldMethod(m) => {
369 p!(out, {m.field_name()} {m.params_desc()} {m.visibility()} str(" ") {m.expr()})369 p!(out, {m.field_name()} {m.params_desc()} {m.visibility()} str(" ") {m.expr()})
370 }370 }
371 }371 }
modifiedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
1use std::{borrow::Cow, fmt::Write};1use std::{borrow::Cow, fmt::Write};
22
3use crate::{bail, Result, State, Val};3use crate::{bail, Result, ResultExt, State, Val};
44
5pub trait ManifestFormat {5pub trait ManifestFormat {
6 fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;6 fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;
236 }236 }
237 buf.push_str(cur_padding);237 buf.push_str(cur_padding);
238 manifest_json_ex_buf(&item?, buf, cur_padding, options)?;238 manifest_json_ex_buf(&item?, buf, cur_padding, options)
239 .with_description(|| format!("elem <{i}> manifestification"))?;
239 }240 }
240 cur_padding.truncate(old_len);241 cur_padding.truncate(old_len);
241242
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
278}278}
279impl ObjectLike for ThisOverride {279impl ObjectLike for ThisOverride {
280 fn with_this(&self, _me: ObjValue, this: ObjValue) -> ObjValue {280 fn with_this(&self, _me: ObjValue, this: ObjValue) -> ObjValue {
281 ObjValue::new(ThisOverride {281 ObjValue::new(Self {
282 inner: self.inner.clone(),282 inner: self.inner.clone(),
283 this,283 this,
284 })284 })
398 self.get_for(key, self.0.this().unwrap_or_else(|| self.clone()))398 self.get_for(key, self.0.this().unwrap_or_else(|| self.clone()))
399 }399 }
400400
401 pub fn get_for(&self, key: IStr, this: ObjValue) -> Result<Option<Val>> {401 pub fn get_for(&self, key: IStr, this: Self) -> Result<Option<Val>> {
402 self.0.get_for(key, this)402 self.0.get_for(key, this)
403 }403 }
404404
410 Ok(value)410 Ok(value)
411 }411 }
412412
413 fn get_raw(&self, key: IStr, this: ObjValue) -> Result<Option<Val>> {413 fn get_raw(&self, key: IStr, this: Self) -> Result<Option<Val>> {
414 self.0.get_for_uncached(key, this)414 self.0.get_for_uncached(key, this)
415 }415 }
416416
422 // FIXME: Should it use `self.0.this()` in case of standalone super?422 // FIXME: Should it use `self.0.this()` in case of standalone super?
423 self.run_assertions_raw(self.clone())423 self.run_assertions_raw(self.clone())
424 }424 }
425 fn run_assertions_raw(&self, this: ObjValue) -> Result<()> {425 fn run_assertions_raw(&self, this: Self) -> Result<()> {
426 self.0.run_assertions_raw(this)426 self.0.run_assertions_raw(this)
427 }427 }
428428
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
6 runtime_error,6 runtime_error,
7 typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},7 typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},
8 val::{equals, ArrValue, IndexableVal},8 val::{equals, ArrValue, IndexableVal},
9 Either, IStr, Result, Thunk, Val,9 Either, IStr, ObjValueBuilder, Result, ResultExt, Thunk, Val,
10};10};
1111
12pub(crate) fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {12pub(crate) fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {
22 if *sz == 0 {22 if *sz == 0 {
23 return Ok(ArrValue::empty());23 return Ok(ArrValue::empty());
24 }24 }
25 if let Some(trivial) = func.evaluate_trivial() {25 func.evaluate_trivial().map_or_else(
26 || Ok(ArrValue::range_exclusive(0, *sz).map(func)),
27 |trivial| {
26 let mut out = Vec::with_capacity(*sz as usize);28 let mut out = Vec::with_capacity(*sz as usize);
27 for _ in 0..*sz {29 for _ in 0..*sz {
28 out.push(trivial.clone())30 out.push(trivial.clone());
29 }31 }
30 Ok(ArrValue::eager(out))32 Ok(ArrValue::eager(out))
31 } else {33 },
32 Ok(ArrValue::range_exclusive(0, *sz).map(func))34 )
33 }
34}35}
3536
36#[builtin]37#[builtin]
180 out += &sep;181 out += &sep;
181 }182 }
182 first = false;183 first = false;
183 write!(out, "{item}").unwrap()184 write!(out, "{item}").unwrap();
184 } else if matches!(item, Val::Null) {185 } else if matches!(item, Val::Null) {
185 continue;186 continue;
186 } else {187 } else {
321 Ok(out)322 Ok(out)
322}323}
324
325#[builtin]
326pub fn builtin_prune(
327 a: Val,
328 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,
329) -> Result<Val> {
330 fn is_content(val: &Val) -> bool {
331 match val {
332 Val::Null => false,
333 Val::Arr(a) => !a.is_empty(),
334 Val::Obj(o) => !o.is_empty(),
335 _ => true,
336 }
337 }
338 Ok(match a {
339 Val::Arr(a) => {
340 let mut out = Vec::new();
341 for (i, ele) in a.iter().enumerate() {
342 let ele = ele
343 .and_then(|v| {
344 builtin_prune(
345 v,
346 #[cfg(feature = "exp-preserve-order")]
347 preserve_order,
348 )
349 })
350 .with_description(|| format!("elem <{i}> pruning"))?;
351 if is_content(&ele) {
352 out.push(ele);
353 }
354 }
355 Val::Arr(ArrValue::eager(out))
356 }
357 Val::Obj(o) => {
358 let mut out = ObjValueBuilder::new();
359 for (name, value) in o.iter(
360 #[cfg(feature = "exp-preserve-order")]
361 preserve_order,
362 ) {
363 let value = value
364 .and_then(|v| {
365 builtin_prune(
366 v,
367 #[cfg(feature = "exp-preserve-order")]
368 preserve_order,
369 )
370 })
371 .with_description(|| format!("field <{name}> pruning"))?;
372 if !is_content(&value) {
373 continue;
374 }
375 out.field(name).value(value);
376 }
377 Val::Obj(out.build())
378 }
379 _ => a,
380 })
381}
323382
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
92 ("remove", builtin_remove::INST),92 ("remove", builtin_remove::INST),
93 ("flattenArrays", builtin_flatten_arrays::INST),93 ("flattenArrays", builtin_flatten_arrays::INST),
94 ("flattenDeepArray", builtin_flatten_deep_array::INST),94 ("flattenDeepArray", builtin_flatten_deep_array::INST),
95 ("prune", builtin_prune::INST),
95 ("filterMap", builtin_filter_map::INST),96 ("filterMap", builtin_filter_map::INST),
96 // Math97 // Math
97 ("abs", builtin_abs::INST),98 ("abs", builtin_abs::INST),
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
209 local arr = std.split(f, '/');209 local arr = std.split(f, '/');
210 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),210 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),
211211
212 prune(a)::
213 local isContent(b) =
214 if b == null then
215 false
216 else if std.isArray(b) then
217 std.length(b) > 0
218 else if std.isObject(b) then
219 std.length(b) > 0
220 else
221 true;
222 if std.isArray(a) then
223 [std.prune(x) for x in a if isContent($.prune(x))]
224 else if std.isObject(a) then {
225 [x]: $.prune(a[x])
226 for x in std.objectFields(a)
227 if isContent(std.prune(a[x]))
228 } else
229 a,
230
231 find(value, arr)::212 find(value, arr)::
232 if !std.isArray(arr) then213 if !std.isArray(arr) then
233 error 'find second parameter should be an array, got ' + std.type(arr)214 error 'find second parameter should be an array, got ' + std.type(arr)