git.delta.rocks / jrsonnet / refs/commits / 9af1f59feaab

difftreelog

perf move std.setInter to native

Yaroslav Bolyukin2023-04-08parent: #e88030d.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
42pub use misc::*;42pub use misc::*;
43mod sets;43mod sets;
44pub use sets::*;44pub use sets::*;
45mod compat;
46pub use compat::*;
4547
46pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {48pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {
47 let mut builder = ObjValueBuilder::new();49 let mut builder = ObjValueBuilder::new();
147 ("endsWith", builtin_ends_with::INST),149 ("endsWith", builtin_ends_with::INST),
148 // Sets150 // Sets
149 ("setMember", builtin_set_member::INST),151 ("setMember", builtin_set_member::INST),
152 ("setInter", builtin_set_inter::INST),
150 ]153 ]
151 .iter()154 .iter()
152 .cloned()155 .cloned()
modifiedcrates/jrsonnet-stdlib/src/sets.rsdiffbeforeafterboth
5 function::{builtin, FuncVal},5 function::{builtin, FuncVal},
6 operator::evaluate_compare_op,6 operator::evaluate_compare_op,
7 val::ArrValue,7 val::ArrValue,
8 Val,8 Thunk, Val,
9};9};
10use jrsonnet_gcmodule::Cc;
10use jrsonnet_parser::BinaryOpType;11use jrsonnet_parser::BinaryOpType;
1112
12#[builtin]13#[builtin]
13#[allow(non_snake_case)]14#[allow(non_snake_case)]
14pub fn builtin_set_member(x: Val, arr: ArrValue, keyF: Option<FuncVal>) -> Result<bool> {15pub fn builtin_set_member(x: Thunk<Val>, arr: ArrValue, keyF: Option<FuncVal>) -> Result<bool> {
15 let mut low = 0;16 let mut low = 0;
16 let mut high = arr.len();17 let mut high = arr.len();
1718
18 let keyF = keyF.unwrap_or(FuncVal::Id).into_native::<((Val,), Val)>();19 let keyF = keyF
20 .unwrap_or(FuncVal::Id)
21 .into_native::<((Thunk<Val>,), Val)>();
1922
20 let x = keyF(x)?;23 let x = keyF(x)?;
2124
22 while low < high {25 while low < high {
23 let middle = (high + low) / 2;26 let middle = (high + low) / 2;
27 let comp = keyF(arr.get_lazy(middle).expect("in bounds"))?;
24 match evaluate_compare_op(&arr.get(middle)?.expect("in bounds"), &x, BinaryOpType::Lt)? {28 match evaluate_compare_op(&comp, &x, BinaryOpType::Lt)? {
25 Ordering::Less => low = middle + 1,29 Ordering::Less => low = middle + 1,
26 Ordering::Equal => return Ok(true),30 Ordering::Equal => return Ok(true),
27 Ordering::Greater => high = middle,31 Ordering::Greater => high = middle,
30 Ok(false)34 Ok(false)
31}35}
36
37#[builtin]
38#[allow(non_snake_case)]
39pub fn builtin_set_inter(a: ArrValue, b: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {
40 let mut a = a.iter_lazy();
41 let mut b = b.iter_lazy();
42
43 let keyF = keyF
44 .unwrap_or(FuncVal::identity())
45 .into_native::<((Thunk<Val>,), Val)>();
46 let keyF = |v| keyF(v);
47
48 let mut av = a.next();
49 let mut bv = b.next();
50 let mut ak = av.clone().map(keyF).transpose()?;
51 let mut bk = bv.map(keyF).transpose()?;
52
53 let mut out = Vec::new();
54 while let (Some(ac), Some(bc)) = (&ak, &bk) {
55 match evaluate_compare_op(ac, bc, BinaryOpType::Lt)? {
56 Ordering::Less => {
57 av = a.next();
58 ak = av.clone().map(keyF).transpose()?;
59 }
60 Ordering::Greater => {
61 bv = b.next();
62 bk = bv.map(keyF).transpose()?;
63 }
64 Ordering::Equal => {
65 out.push(av.clone().expect("ak != None => av != None"));
66 av = a.next();
67 ak = av.clone().map(keyF).transpose()?;
68 bv = b.next();
69 bk = bv.map(keyF).transpose()?;
70 }
71 };
72 }
73 Ok(ArrValue::lazy(Cc::new(out)))
74}
3275
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
214 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;214 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;
215 aux(a, b, 0, 0, []),215 aux(a, b, 0, 0, []),
216216
217 setInter(a, b, keyF=id)::
218 local aux(a, b, i, j, acc) =
219 if i >= std.length(a) || j >= std.length(b) then
220 acc
221 else
222 if keyF(a[i]) == keyF(b[j]) then
223 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict
224 else if keyF(a[i]) < keyF(b[j]) then
225 aux(a, b, i + 1, j, acc) tailstrict
226 else
227 aux(a, b, i, j + 1, acc) tailstrict;
228 aux(a, b, 0, 0, []) tailstrict,
229
230 setDiff(a, b, keyF=id)::217 setDiff(a, b, keyF=id)::
231 local aux(a, b, i, j, acc) =218 local aux(a, b, i, j, acc) =
232 if i >= std.length(a) then219 if i >= std.length(a) then