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

difftreelog

perf move std.assertEqual, std.find to native

Yaroslav Bolyukin2024-06-18parent: #541910c.patch.diff
in: master

4 files changed

modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -276,6 +276,18 @@
 }
 
 #[builtin]
+pub fn builtin_find(value: Val, arr: ArrValue) -> Result<Vec<usize>> {
+	let mut out = Vec::new();
+	for (i, ele) in arr.iter().enumerate() {
+		let ele = ele?;
+		if equals(&ele, &value)? {
+			out.push(i);
+		}
+	}
+	Ok(out)
+}
+
+#[builtin]
 pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {
 	builtin_member(arr, elem)
 }
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
91 ("any", builtin_any::INST),91 ("any", builtin_any::INST),
92 ("all", builtin_all::INST),92 ("all", builtin_all::INST),
93 ("member", builtin_member::INST),93 ("member", builtin_member::INST),
94 ("find", builtin_find::INST),
94 ("contains", builtin_contains::INST),95 ("contains", builtin_contains::INST),
95 ("count", builtin_count::INST),96 ("count", builtin_count::INST),
96 ("avg", builtin_avg::INST),97 ("avg", builtin_avg::INST),
213 ("get", builtin_get::INST),214 ("get", builtin_get::INST),
214 ("startsWith", builtin_starts_with::INST),215 ("startsWith", builtin_starts_with::INST),
215 ("endsWith", builtin_ends_with::INST),216 ("endsWith", builtin_ends_with::INST),
217 ("assertEqual", builtin_assert_equal::INST),
216 // Sets218 // Sets
217 ("setMember", builtin_set_member::INST),219 ("setMember", builtin_set_member::INST),
218 ("setInter", builtin_set_inter::INST),220 ("setInter", builtin_set_inter::INST),
modifiedcrates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -7,7 +7,7 @@
 	manifest::JsonFormat,
 	typed::{Either2, Either4},
 	val::{equals, ArrValue},
-	Context, Either, IStr, ObjValue, Thunk, Val,
+	Context, Either, IStr, ObjValue, ResultExt, Thunk, Val,
 };
 
 use crate::{extvar_source, Settings};
@@ -141,3 +141,14 @@
 		_ => bail!("both arguments should be of the same type"),
 	})
 }
+
+#[builtin]
+pub fn builtin_assert_equal(a: Val, b: Val) -> Result<bool> {
+	if equals(&a, &b)? {
+		return Ok(true);
+	}
+	let format = JsonFormat::std_to_json("  ".to_owned(), "\n", ": ");
+	let a = a.manifest(&format).description("<a> manifestification")?;
+	let b = b.manifest(&format).description("<b> manifestification")?;
+	bail!("assertion failed: A != B\nA: {a}\nB: {b}")
+}
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -11,12 +11,6 @@
     else
       { [k]: func(k, obj[k]) for k in std.objectFields(obj) },
 
-  assertEqual(a, b)::
-    if a == b then
-      true
-    else
-      error 'Assertion failed. ' + a + ' != ' + b,
-
   mergePatch(target, patch)::
     if std.isObject(patch) then
       local target_object =
@@ -44,10 +38,4 @@
   resolvePath(f, r)::
     local arr = std.split(f, '/');
     std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),
-
-  find(value, arr)::
-    if !std.isArray(arr) then
-      error 'find second parameter should be an array, got ' + std.type(arr)
-    else
-      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),
 }