Skip to content

Commit

Permalink
perf: implement std.prune in native
Browse files Browse the repository at this point in the history
  • Loading branch information
CertainLach committed Apr 7, 2024
1 parent 66d8cad commit d349b9e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 40 deletions.
8 changes: 4 additions & 4 deletions cmds/jrsonnet-fmt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,16 @@ impl Printable for SliceDesc {
impl Printable for Member {
fn print(&self, out: &mut PrintItems) {
match self {
Member::MemberBindStmt(b) => {
Self::MemberBindStmt(b) => {
p!(out, { b.obj_local() })
}
Member::MemberAssertStmt(ass) => {
Self::MemberAssertStmt(ass) => {
p!(out, { ass.assertion() })
}
Member::MemberFieldNormal(n) => {
Self::MemberFieldNormal(n) => {
p!(out, {n.field_name()} if(n.plus_token().is_some())({n.plus_token()}) {n.visibility()} str(" ") {n.expr()})
}
Member::MemberFieldMethod(m) => {
Self::MemberFieldMethod(m) => {
p!(out, {m.field_name()} {m.params_desc()} {m.visibility()} str(" ") {m.expr()})
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/jrsonnet-evaluator/src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{borrow::Cow, fmt::Write};

use crate::{bail, Result, State, Val};
use crate::{bail, Result, ResultExt, State, Val};

pub trait ManifestFormat {
fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;
Expand Down Expand Up @@ -235,7 +235,8 @@ fn manifest_json_ex_buf(
}
}
buf.push_str(cur_padding);
manifest_json_ex_buf(&item?, buf, cur_padding, options)?;
manifest_json_ex_buf(&item?, buf, cur_padding, options)
.with_description(|| format!("elem <{i}> manifestification"))?;
}
cur_padding.truncate(old_len);

Expand Down
8 changes: 4 additions & 4 deletions crates/jrsonnet-evaluator/src/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ struct ThisOverride {
}
impl ObjectLike for ThisOverride {
fn with_this(&self, _me: ObjValue, this: ObjValue) -> ObjValue {
ObjValue::new(ThisOverride {
ObjValue::new(Self {
inner: self.inner.clone(),
this,
})
Expand Down Expand Up @@ -398,7 +398,7 @@ impl ObjValue {
self.get_for(key, self.0.this().unwrap_or_else(|| self.clone()))
}

pub fn get_for(&self, key: IStr, this: ObjValue) -> Result<Option<Val>> {
pub fn get_for(&self, key: IStr, this: Self) -> Result<Option<Val>> {
self.0.get_for(key, this)
}

Expand All @@ -410,7 +410,7 @@ impl ObjValue {
Ok(value)
}

fn get_raw(&self, key: IStr, this: ObjValue) -> Result<Option<Val>> {
fn get_raw(&self, key: IStr, this: Self) -> Result<Option<Val>> {
self.0.get_for_uncached(key, this)
}

Expand All @@ -422,7 +422,7 @@ impl ObjValue {
// FIXME: Should it use `self.0.this()` in case of standalone super?
self.run_assertions_raw(self.clone())
}
fn run_assertions_raw(&self, this: ObjValue) -> Result<()> {
fn run_assertions_raw(&self, this: Self) -> Result<()> {
self.0.run_assertions_raw(this)
}

Expand Down
81 changes: 70 additions & 11 deletions crates/jrsonnet-stdlib/src/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use jrsonnet_evaluator::{
runtime_error,
typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},
val::{equals, ArrValue, IndexableVal},
Either, IStr, Result, Thunk, Val,
Either, IStr, ObjValueBuilder, Result, ResultExt, Thunk, Val,
};

pub(crate) fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {
Expand All @@ -22,15 +22,16 @@ pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Res
if *sz == 0 {
return Ok(ArrValue::empty());
}
if let Some(trivial) = func.evaluate_trivial() {
let mut out = Vec::with_capacity(*sz as usize);
for _ in 0..*sz {
out.push(trivial.clone())
}
Ok(ArrValue::eager(out))
} else {
Ok(ArrValue::range_exclusive(0, *sz).map(func))
}
func.evaluate_trivial().map_or_else(
|| Ok(ArrValue::range_exclusive(0, *sz).map(func)),
|trivial| {
let mut out = Vec::with_capacity(*sz as usize);
for _ in 0..*sz {
out.push(trivial.clone());
}
Ok(ArrValue::eager(out))
},
)
}

#[builtin]
Expand Down Expand Up @@ -180,7 +181,7 @@ pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {
out += &sep;
}
first = false;
write!(out, "{item}").unwrap()
write!(out, "{item}").unwrap();
} else if matches!(item, Val::Null) {
continue;
} else {
Expand Down Expand Up @@ -320,3 +321,61 @@ pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {
process(value, &mut out)?;
Ok(out)
}

#[builtin]
pub fn builtin_prune(
a: Val,
#[cfg(feature = "exp-preserve-order")] preserve_order: bool,
) -> Result<Val> {
fn is_content(val: &Val) -> bool {
match val {
Val::Null => false,
Val::Arr(a) => !a.is_empty(),
Val::Obj(o) => !o.is_empty(),
_ => true,
}
}
Ok(match a {
Val::Arr(a) => {
let mut out = Vec::new();
for (i, ele) in a.iter().enumerate() {
let ele = ele
.and_then(|v| {
builtin_prune(
v,
#[cfg(feature = "exp-preserve-order")]
preserve_order,
)
})
.with_description(|| format!("elem <{i}> pruning"))?;
if is_content(&ele) {
out.push(ele);
}
}
Val::Arr(ArrValue::eager(out))
}
Val::Obj(o) => {
let mut out = ObjValueBuilder::new();
for (name, value) in o.iter(
#[cfg(feature = "exp-preserve-order")]
preserve_order,
) {
let value = value
.and_then(|v| {
builtin_prune(
v,
#[cfg(feature = "exp-preserve-order")]
preserve_order,
)
})
.with_description(|| format!("field <{name}> pruning"))?;
if !is_content(&value) {
continue;
}
out.field(name).value(value);
}
Val::Obj(out.build())
}
_ => a,
})
}
1 change: 1 addition & 0 deletions crates/jrsonnet-stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {
("remove", builtin_remove::INST),
("flattenArrays", builtin_flatten_arrays::INST),
("flattenDeepArray", builtin_flatten_deep_array::INST),
("prune", builtin_prune::INST),
("filterMap", builtin_filter_map::INST),
// Math
("abs", builtin_abs::INST),
Expand Down
19 changes: 0 additions & 19 deletions crates/jrsonnet-stdlib/src/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -209,25 +209,6 @@
local arr = std.split(f, '/');
std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),

prune(a)::
local isContent(b) =
if b == null then
false
else if std.isArray(b) then
std.length(b) > 0
else if std.isObject(b) then
std.length(b) > 0
else
true;
if std.isArray(a) then
[std.prune(x) for x in a if isContent($.prune(x))]
else if std.isObject(a) then {
[x]: $.prune(a[x])
for x in std.objectFields(a)
if isContent(std.prune(a[x]))
} else
a,

find(value, arr)::
if !std.isArray(arr) then
error 'find second parameter should be an array, got ' + std.type(arr)
Expand Down

0 comments on commit d349b9e

Please sign in to comment.