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
--- a/cmds/jrsonnet-fmt/src/main.rs
+++ b/cmds/jrsonnet-fmt/src/main.rs
@@ -356,16 +356,16 @@
 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()})
 			}
 		}
modifiedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -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<()>;
@@ -235,7 +235,8 @@
 						}
 					}
 					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);
 
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -278,7 +278,7 @@
 }
 impl ObjectLike for ThisOverride {
 	fn with_this(&self, _me: ObjValue, this: ObjValue) -> ObjValue {
-		ObjValue::new(ThisOverride {
+		ObjValue::new(Self {
 			inner: self.inner.clone(),
 			this,
 		})
@@ -398,7 +398,7 @@
 		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)
 	}
 
@@ -410,7 +410,7 @@
 		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)
 	}
 
@@ -422,7 +422,7 @@
 		// 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)
 	}
 
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -6,7 +6,7 @@
 	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> {
@@ -21,16 +21,17 @@
 pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {
 	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]
@@ -180,7 +181,7 @@
 						out += &sep;
 					}
 					first = false;
-					write!(out, "{item}").unwrap()
+					write!(out, "{item}").unwrap();
 				} else if matches!(item, Val::Null) {
 					continue;
 				} else {
@@ -320,3 +321,61 @@
 	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,
+	})
+}
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -92,6 +92,7 @@
 		("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),
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,3  local id = std.id,45  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',67  lstripChars(str, chars)::8    if std.length(str) > 0 && std.member(chars, str[0]) then9      std.lstripChars(str[1:], chars)10    else11      str,1213  rstripChars(str, chars)::14    local len = std.length(str);15    if len > 0 && std.member(chars, str[len - 1]) then16      std.rstripChars(str[:len - 1], chars)17    else18      str,1920  stripChars(str, chars)::21    std.lstripChars(std.rstripChars(str, chars), chars),2223  splitLimitR(str, c, maxsplits)::24    if maxsplits == -1 then25      std.splitLimit(str, c, -1)26    else27      local revStr(str) = std.join('', std.reverse(std.stringChars(str)));28      std.map(function(e) revStr(e), std.reverse(std.splitLimit(revStr(str), revStr(c), maxsplits))),2930  split(str, c):: std.splitLimit(str, c, -1),3132  mapWithIndex(func, arr)::33    if !std.isFunction(func) then34      error ('std.mapWithIndex first param must be function, got ' + std.type(func))35    else if !std.isArray(arr) && !std.isString(arr) then36      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))37    else38      std.makeArray(std.length(arr), function(i) func(i, arr[i])),3940  mapWithKey(func, obj)::41    if !std.isFunction(func) then42      error ('std.mapWithKey first param must be function, got ' + std.type(func))43    else if !std.isObject(obj) then44      error ('std.mapWithKey second param must be object, got ' + std.type(obj))45    else46      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },4748  lines(arr)::49    std.join('\n', arr + ['']),5051  deepJoin(arr)::52    if std.isString(arr) then53      arr54    else if std.isArray(arr) then55      std.join('', [std.deepJoin(x) for x in arr])56    else57      error 'Expected string or array, got %s' % std.type(arr),5859  assertEqual(a, b)::60    if a == b then61      true62    else63      error 'Assertion failed. ' + a + ' != ' + b,6465  clamp(x, minVal, maxVal)::66    if x < minVal then minVal67    else if x > maxVal then maxVal68    else x,6970  manifestIni(ini)::71    local body_lines(body) =72      std.join([], [73        local value_or_values = body[k];74        if std.isArray(value_or_values) then75          ['%s = %s' % [k, value] for value in value_or_values]76        else77          ['%s = %s' % [k, value_or_values]]7879        for k in std.objectFields(body)80      ]);8182    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),83          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],84          all_sections = [85      section_lines(k, ini.sections[k])86      for k in std.objectFields(ini.sections)87    ];88    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),8990  manifestToml(value):: std.manifestTomlEx(value, '  '),9192  escapeStringPython(str)::93    std.escapeStringJson(str),9495  escapeStringBash(str_)::96    local str = std.toString(str_);97    local trans(ch) =98      if ch == "'" then99        "'\"'\"'"100      else101        ch;102    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),103104  escapeStringDollars(str_)::105    local str = std.toString(str_);106    local trans(ch) =107      if ch == '$' then108        '$$'109      else110        ch;111    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),112113  local xml_escapes = {114    '<': '&lt;',115    '>': '&gt;',116    '&': '&amp;',117    '"': '&quot;',118    "'": '&apos;',119  },120121  escapeStringXML(str_)::122    local str = std.toString(str_);123    std.join('', [std.get(xml_escapes, ch, ch) for ch in std.stringChars(str)]),124125  manifestJson(value):: std.manifestJsonEx(value, '    ') tailstrict,126127  manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),128129  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true, quote_keys=true)::130    if !std.isArray(value) then131      error 'manifestYamlStream only takes arrays, got ' + std.type(value)132    else133      '---\n' + std.join(134        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object, quote_keys) for e in value]135      ) + if c_document_end then '\n...\n' else '\n',136137  manifestPython(v)::138    if std.isObject(v) then139      local fields = [140        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]141        for k in std.objectFields(v)142      ];143      '{%s}' % [std.join(', ', fields)]144    else if std.isArray(v) then145      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]146    else if std.isString(v) then147      '%s' % [std.escapeStringPython(v)]148    else if std.isFunction(v) then149      error 'cannot manifest function'150    else if std.isNumber(v) then151      std.toString(v)152    else if v == true then153      'True'154    else if v == false then155      'False'156    else if v == null then157      'None',158159  manifestPythonVars(conf)::160    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];161    std.join('\n', vars + ['']),162163  manifestXmlJsonml(value)::164    if !std.isArray(value) then165      error 'Expected a JSONML value (an array), got %s' % std.type(value)166    else167      local aux(v) =168        if std.isString(v) then169          v170        else171          local tag = v[0];172          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);173          local attrs = if has_attrs then v[1] else {};174          local children = if has_attrs then v[2:] else v[1:];175          local attrs_str =176            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);177          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);178179      aux(value),180181  mergePatch(target, patch)::182    if std.isObject(patch) then183      local target_object =184        if std.isObject(target) then target else {};185186      local target_fields =187        if std.isObject(target_object) then std.objectFields(target_object) else [];188189      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];190      local both_fields = std.setUnion(target_fields, std.objectFields(patch));191192      {193        [k]:194          if !std.objectHas(patch, k) then195            target_object[k]196          else if !std.objectHas(target_object, k) then197            std.mergePatch(null, patch[k]) tailstrict198          else199            std.mergePatch(target_object[k], patch[k]) tailstrict200        for k in std.setDiff(both_fields, null_fields)201      }202    else203      patch,204205  get(o, f, default=null, inc_hidden=true)::206    if std.objectHasEx(o, f, inc_hidden) then o[f] else default,207208  resolvePath(f, r)::209    local arr = std.split(f, '/');210    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),211212  prune(a)::213    local isContent(b) =214      if b == null then215        false216      else if std.isArray(b) then217        std.length(b) > 0218      else if std.isObject(b) then219        std.length(b) > 0220      else221        true;222    if std.isArray(a) then223      [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    } else229      a,230231  find(value, arr)::232    if !std.isArray(arr) then233      error 'find second parameter should be an array, got ' + std.type(arr)234    else235      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),236237  // Compat238  __compare_array(arr1, arr2)::239    assert std.isArray(arr1) && std.isArray(arr2);240    std.__compare(arr1, arr2),241  __array_less(arr1, arr2):: std.__compare_array(arr1, arr2) == -1,242  __array_greater(arr1, arr2):: std.__compare_array(arr1, arr2) == 1,243  __array_less_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) <= 0,244  __array_greater_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) >= 0,245}