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

difftreelog

perf move deepJoin to native

Yaroslav Bolyukin2024-06-18parent: #0e22242.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/arrays.rs
1#![allow(non_snake_case)]23use jrsonnet_evaluator::{4	bail,5	function::{builtin, FuncVal},6	runtime_error,7	typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},8	val::{equals, ArrValue, IndexableVal},9	Either, IStr, ObjValueBuilder, Result, ResultExt, Thunk, Val,10};1112pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13	if let Some(on_empty) = on_empty {14		on_empty.evaluate()15	} else {16		bail!("expected non-empty array")17	}18}1920#[builtin]21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {22	if *sz == 0 {23		return Ok(ArrValue::empty());24	}25	func.evaluate_trivial().map_or_else(26		|| Ok(ArrValue::range_exclusive(0, *sz).map(func)),27		|trivial| {28			let mut out = Vec::with_capacity(*sz as usize);29			for _ in 0..*sz {30				out.push(trivial.clone());31			}32			Ok(ArrValue::eager(out))33		},34	)35}3637#[builtin]38pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {39	Ok(match what {40		Either2::A(s) => Val::string(s.repeat(count)),41		Either2::B(arr) => Val::Arr(42			ArrValue::repeated(arr, count)43				.ok_or_else(|| runtime_error!("repeated length overflow"))?,44		),45	})46}4748#[builtin]49pub fn builtin_slice(50	indexable: IndexableVal,51	index: Option<i32>,52	end: Option<i32>,53	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,54) -> Result<Val> {55	indexable.slice(index, end, step).map(Val::from)56}5758#[builtin]59pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {60	let arr = arr.to_array();61	arr.map(func)62}6364#[builtin]65pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {66	let arr = arr.to_array();67	arr.map_with_index(func)68}6970#[builtin]71pub fn builtin_flatmap(72	func: NativeFn<((Either![String, Val],), Val)>,73	arr: IndexableVal,74) -> Result<IndexableVal> {75	use std::fmt::Write;76	match arr {77		IndexableVal::Str(str) => {78			let mut out = String::new();79			for c in str.chars() {80				match func(Either2::A(c.to_string()))? {81					Val::Str(o) => write!(out, "{o}").unwrap(),82					Val::Null => continue,83					_ => bail!("in std.join all items should be strings"),84				};85			}86			Ok(IndexableVal::Str(out.into()))87		}88		IndexableVal::Arr(a) => {89			let mut out = Vec::new();90			for el in a.iter() {91				let el = el?;92				match func(Either2::B(el))? {93					Val::Arr(o) => {94						for oe in o.iter() {95							out.push(oe?);96						}97					}98					Val::Null => continue,99					_ => bail!("in std.join all items should be arrays"),100				};101			}102			Ok(IndexableVal::Arr(out.into()))103		}104	}105}106107#[builtin]108pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {109	arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))110}111112#[builtin]113pub fn builtin_filter_map(114	filter_func: FuncVal,115	map_func: FuncVal,116	arr: ArrValue,117) -> Result<ArrValue> {118	Ok(builtin_filter(filter_func, arr)?.map(map_func))119}120121#[builtin]122pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {123	let mut acc = init;124	for i in arr.iter() {125		acc = func.evaluate_simple(&(acc, i?), false)?;126	}127	Ok(acc)128}129130#[builtin]131pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {132	let mut acc = init;133	for i in arr.iter().rev() {134		acc = func.evaluate_simple(&(i?, acc), false)?;135	}136	Ok(acc)137}138139#[builtin]140pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {141	if to < from {142		return Ok(ArrValue::empty());143	}144	Ok(ArrValue::range_inclusive(from, to))145}146147#[builtin]148pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {149	use std::fmt::Write;150	Ok(match sep {151		IndexableVal::Arr(joiner_items) => {152			let mut out = Vec::new();153154			let mut first = true;155			for item in arr.iter() {156				let item = item?.clone();157				if let Val::Arr(items) = item {158					if !first {159						out.reserve(joiner_items.len());160						// TODO: extend161						for item in joiner_items.iter() {162							out.push(item?);163						}164					}165					first = false;166					out.reserve(items.len());167					for item in items.iter() {168						out.push(item?);169					}170				} else if matches!(item, Val::Null) {171					continue;172				} else {173					bail!("in std.join all items should be arrays");174				}175			}176177			IndexableVal::Arr(out.into())178		}179		IndexableVal::Str(sep) => {180			let mut out = String::new();181182			let mut first = true;183			for item in arr.iter() {184				let item = item?.clone();185				if let Val::Str(item) = item {186					if !first {187						out += &sep;188					}189					first = false;190					write!(out, "{item}").unwrap();191				} else if matches!(item, Val::Null) {192					continue;193				} else {194					bail!("in std.join all items should be strings");195				}196			}197198			IndexableVal::Str(out.into())199		}200	})201}202203#[builtin]204pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {205	builtin_join(206		IndexableVal::Str("\n".into()),207		ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])).into(),208	)209}210211#[builtin]212pub fn builtin_reverse(arr: ArrValue) -> ArrValue {213	arr.reversed()214}215216#[builtin]217pub fn builtin_any(arr: ArrValue) -> Result<bool> {218	for v in arr.iter() {219		let v = bool::from_untyped(v?)?;220		if v {221			return Ok(true);222		}223	}224	Ok(false)225}226227#[builtin]228pub fn builtin_all(arr: ArrValue) -> Result<bool> {229	for v in arr.iter() {230		let v = bool::from_untyped(v?)?;231		if !v {232			return Ok(false);233		}234	}235	Ok(true)236}237238#[builtin]239pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {240	match arr {241		IndexableVal::Str(str) => {242			let x: IStr = IStr::from_untyped(x)?;243			Ok(!x.is_empty() && str.contains(&*x))244		}245		IndexableVal::Arr(a) => {246			for item in a.iter() {247				let item = item?;248				if equals(&item, &x)? {249					return Ok(true);250				}251			}252			Ok(false)253		}254	}255}256257#[builtin]258pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {259	builtin_member(arr, elem)260}261262#[builtin]263pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {264	let mut count = 0;265	for item in arr.iter() {266		if equals(&item?, &x)? {267			count += 1;268		}269	}270	Ok(count)271}272273#[builtin]274pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {275	if arr.is_empty() {276		return eval_on_empty(onEmpty);277	}278	Ok(Val::try_num(arr.iter().sum::<f64>() / (arr.len() as f64))?)279}280281#[builtin]282pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {283	let newArrLeft = arr.clone().slice(None, Some(at), None);284	let newArrRight = arr.slice(Some(at + 1), None, None);285286	Ok(ArrValue::extended(newArrLeft, newArrRight))287}288289#[builtin]290pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {291	for (index, item) in arr.iter().enumerate() {292		if equals(&item?, &elem)? {293			return builtin_remove_at(arr.clone(), index as i32);294		}295	}296	Ok(arr)297}298299#[builtin]300pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {301	pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {302		if values.len() == 1 {303			return values[0].clone();304		} else if values.len() == 2 {305			return ArrValue::extended(values[0].clone(), values[1].clone());306		}307		let (a, b) = values.split_at(values.len() / 2);308		ArrValue::extended(flatten_inner(a), flatten_inner(b))309	}310	if arrs.is_empty() {311		return ArrValue::empty();312	} else if arrs.len() == 1 {313		return arrs.into_iter().next().expect("single");314	}315	flatten_inner(&arrs)316}317318#[builtin]319pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {320	fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {321		match value {322			Val::Arr(arr) => {323				for ele in arr.iter() {324					process(ele?, out)?;325				}326			}327			_ => out.push(value),328		}329		Ok(())330	}331	let mut out = Vec::new();332	process(value, &mut out)?;333	Ok(out)334}335336#[builtin]337pub fn builtin_prune(338	a: Val,339340	#[default(false)]341	#[cfg(feature = "exp-preserve-order")]342	preserve_order: bool,343) -> Result<Val> {344	fn is_content(val: &Val) -> bool {345		match val {346			Val::Null => false,347			Val::Arr(a) => !a.is_empty(),348			Val::Obj(o) => !o.is_empty(),349			_ => true,350		}351	}352	Ok(match a {353		Val::Arr(a) => {354			let mut out = Vec::new();355			for (i, ele) in a.iter().enumerate() {356				let ele = ele357					.and_then(|v| {358						builtin_prune(359							v,360							#[cfg(feature = "exp-preserve-order")]361							preserve_order,362						)363					})364					.with_description(|| format!("elem <{i}> pruning"))?;365				if is_content(&ele) {366					out.push(ele);367				}368			}369			Val::Arr(ArrValue::eager(out))370		}371		Val::Obj(o) => {372			let mut out = ObjValueBuilder::new();373			for (name, value) in o.iter(374				#[cfg(feature = "exp-preserve-order")]375				preserve_order,376			) {377				let value = value378					.and_then(|v| {379						builtin_prune(380							v,381							#[cfg(feature = "exp-preserve-order")]382							preserve_order,383						)384					})385					.with_description(|| format!("field <{name}> pruning"))?;386				if !is_content(&value) {387					continue;388				}389				out.field(name).value(value);390			}391			Val::Obj(out.build())392		}393		_ => a,394	})395}
after · crates/jrsonnet-stdlib/src/arrays.rs
1#![allow(non_snake_case)]23use jrsonnet_evaluator::{4	bail,5	function::{builtin, FuncVal},6	runtime_error,7	typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},8	val::{equals, ArrValue, IndexableVal},9	Either, IStr, ObjValueBuilder, Result, ResultExt, Thunk, Val,10};1112pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13	if let Some(on_empty) = on_empty {14		on_empty.evaluate()15	} else {16		bail!("expected non-empty array")17	}18}1920#[builtin]21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {22	if *sz == 0 {23		return Ok(ArrValue::empty());24	}25	func.evaluate_trivial().map_or_else(26		|| Ok(ArrValue::range_exclusive(0, *sz).map(func)),27		|trivial| {28			let mut out = Vec::with_capacity(*sz as usize);29			for _ in 0..*sz {30				out.push(trivial.clone());31			}32			Ok(ArrValue::eager(out))33		},34	)35}3637#[builtin]38pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {39	Ok(match what {40		Either2::A(s) => Val::string(s.repeat(count)),41		Either2::B(arr) => Val::Arr(42			ArrValue::repeated(arr, count)43				.ok_or_else(|| runtime_error!("repeated length overflow"))?,44		),45	})46}4748#[builtin]49pub fn builtin_slice(50	indexable: IndexableVal,51	index: Option<i32>,52	end: Option<i32>,53	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,54) -> Result<Val> {55	indexable.slice(index, end, step).map(Val::from)56}5758#[builtin]59pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {60	let arr = arr.to_array();61	arr.map(func)62}6364#[builtin]65pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {66	let arr = arr.to_array();67	arr.map_with_index(func)68}6970#[builtin]71pub fn builtin_flatmap(72	func: NativeFn<((Either![String, Val],), Val)>,73	arr: IndexableVal,74) -> Result<IndexableVal> {75	use std::fmt::Write;76	match arr {77		IndexableVal::Str(str) => {78			let mut out = String::new();79			for c in str.chars() {80				match func(Either2::A(c.to_string()))? {81					Val::Str(o) => write!(out, "{o}").unwrap(),82					Val::Null => continue,83					_ => bail!("in std.join all items should be strings"),84				};85			}86			Ok(IndexableVal::Str(out.into()))87		}88		IndexableVal::Arr(a) => {89			let mut out = Vec::new();90			for el in a.iter() {91				let el = el?;92				match func(Either2::B(el))? {93					Val::Arr(o) => {94						for oe in o.iter() {95							out.push(oe?);96						}97					}98					Val::Null => continue,99					_ => bail!("in std.join all items should be arrays"),100				};101			}102			Ok(IndexableVal::Arr(out.into()))103		}104	}105}106107#[builtin]108pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {109	arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))110}111112#[builtin]113pub fn builtin_filter_map(114	filter_func: FuncVal,115	map_func: FuncVal,116	arr: ArrValue,117) -> Result<ArrValue> {118	Ok(builtin_filter(filter_func, arr)?.map(map_func))119}120121#[builtin]122pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {123	let mut acc = init;124	for i in arr.iter() {125		acc = func.evaluate_simple(&(acc, i?), false)?;126	}127	Ok(acc)128}129130#[builtin]131pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {132	let mut acc = init;133	for i in arr.iter().rev() {134		acc = func.evaluate_simple(&(i?, acc), false)?;135	}136	Ok(acc)137}138139#[builtin]140pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {141	if to < from {142		return Ok(ArrValue::empty());143	}144	Ok(ArrValue::range_inclusive(from, to))145}146147#[builtin]148pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {149	use std::fmt::Write;150	Ok(match sep {151		IndexableVal::Arr(joiner_items) => {152			let mut out = Vec::new();153154			let mut first = true;155			for item in arr.iter() {156				let item = item?.clone();157				if let Val::Arr(items) = item {158					if !first {159						out.reserve(joiner_items.len());160						// TODO: extend161						for item in joiner_items.iter() {162							out.push(item?);163						}164					}165					first = false;166					out.reserve(items.len());167					for item in items.iter() {168						out.push(item?);169					}170				} else if matches!(item, Val::Null) {171					continue;172				} else {173					bail!("in std.join all items should be arrays");174				}175			}176177			IndexableVal::Arr(out.into())178		}179		IndexableVal::Str(sep) => {180			let mut out = String::new();181182			let mut first = true;183			for item in arr.iter() {184				let item = item?.clone();185				if let Val::Str(item) = item {186					if !first {187						out += &sep;188					}189					first = false;190					write!(out, "{item}").unwrap();191				} else if matches!(item, Val::Null) {192					continue;193				} else {194					bail!("in std.join all items should be strings");195				}196			}197198			IndexableVal::Str(out.into())199		}200	})201}202203#[builtin]204pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {205	builtin_join(206		IndexableVal::Str("\n".into()),207		ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),208	)209}210211pub fn deep_join_inner(out: &mut String, arr: IndexableVal) -> Result<()> {212	use std::fmt::Write;213	match arr {214		IndexableVal::Str(s) => write!(out, "{s}").expect("no error"),215		IndexableVal::Arr(arr) => {216			for ele in arr.iter() {217				let indexable = IndexableVal::from_untyped(ele?)?;218				deep_join_inner(out, indexable)?;219			}220		}221	}222	Ok(())223}224225#[builtin]226pub fn builtin_deep_join(arr: IndexableVal) -> Result<String> {227	let mut out = String::new();228	deep_join_inner(&mut out, arr)?;229	Ok(out)230}231232#[builtin]233pub fn builtin_reverse(arr: ArrValue) -> ArrValue {234	arr.reversed()235}236237#[builtin]238pub fn builtin_any(arr: ArrValue) -> Result<bool> {239	for v in arr.iter() {240		let v = bool::from_untyped(v?)?;241		if v {242			return Ok(true);243		}244	}245	Ok(false)246}247248#[builtin]249pub fn builtin_all(arr: ArrValue) -> Result<bool> {250	for v in arr.iter() {251		let v = bool::from_untyped(v?)?;252		if !v {253			return Ok(false);254		}255	}256	Ok(true)257}258259#[builtin]260pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {261	match arr {262		IndexableVal::Str(str) => {263			let x: IStr = IStr::from_untyped(x)?;264			Ok(!x.is_empty() && str.contains(&*x))265		}266		IndexableVal::Arr(a) => {267			for item in a.iter() {268				let item = item?;269				if equals(&item, &x)? {270					return Ok(true);271				}272			}273			Ok(false)274		}275	}276}277278#[builtin]279pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {280	builtin_member(arr, elem)281}282283#[builtin]284pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {285	let mut count = 0;286	for item in arr.iter() {287		if equals(&item?, &x)? {288			count += 1;289		}290	}291	Ok(count)292}293294#[builtin]295pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {296	if arr.is_empty() {297		return eval_on_empty(onEmpty);298	}299	Ok(Val::try_num(arr.iter().sum::<f64>() / (arr.len() as f64))?)300}301302#[builtin]303pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {304	let newArrLeft = arr.clone().slice(None, Some(at), None);305	let newArrRight = arr.slice(Some(at + 1), None, None);306307	Ok(ArrValue::extended(newArrLeft, newArrRight))308}309310#[builtin]311pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {312	for (index, item) in arr.iter().enumerate() {313		if equals(&item?, &elem)? {314			return builtin_remove_at(arr.clone(), index as i32);315		}316	}317	Ok(arr)318}319320#[builtin]321pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {322	pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {323		if values.len() == 1 {324			return values[0].clone();325		} else if values.len() == 2 {326			return ArrValue::extended(values[0].clone(), values[1].clone());327		}328		let (a, b) = values.split_at(values.len() / 2);329		ArrValue::extended(flatten_inner(a), flatten_inner(b))330	}331	if arrs.is_empty() {332		return ArrValue::empty();333	} else if arrs.len() == 1 {334		return arrs.into_iter().next().expect("single");335	}336	flatten_inner(&arrs)337}338339#[builtin]340pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {341	fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {342		match value {343			Val::Arr(arr) => {344				for ele in arr.iter() {345					process(ele?, out)?;346				}347			}348			_ => out.push(value),349		}350		Ok(())351	}352	let mut out = Vec::new();353	process(value, &mut out)?;354	Ok(out)355}356357#[builtin]358pub fn builtin_prune(359	a: Val,360361	#[default(false)]362	#[cfg(feature = "exp-preserve-order")]363	preserve_order: bool,364) -> Result<Val> {365	fn is_content(val: &Val) -> bool {366		match val {367			Val::Null => false,368			Val::Arr(a) => !a.is_empty(),369			Val::Obj(o) => !o.is_empty(),370			_ => true,371		}372	}373	Ok(match a {374		Val::Arr(a) => {375			let mut out = Vec::new();376			for (i, ele) in a.iter().enumerate() {377				let ele = ele378					.and_then(|v| {379						builtin_prune(380							v,381							#[cfg(feature = "exp-preserve-order")]382							preserve_order,383						)384					})385					.with_description(|| format!("elem <{i}> pruning"))?;386				if is_content(&ele) {387					out.push(ele);388				}389			}390			Val::Arr(ArrValue::eager(out))391		}392		Val::Obj(o) => {393			let mut out = ObjValueBuilder::new();394			for (name, value) in o.iter(395				#[cfg(feature = "exp-preserve-order")]396				preserve_order,397			) {398				let value = value399					.and_then(|v| {400						builtin_prune(401							v,402							#[cfg(feature = "exp-preserve-order")]403							preserve_order,404						)405					})406					.with_description(|| format!("field <{name}> pruning"))?;407				if !is_content(&value) {408					continue;409				}410				out.field(name).value(value);411			}412			Val::Obj(out.build())413		}414		_ => a,415	})416}
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -86,6 +86,7 @@
 		("range", builtin_range::INST),
 		("join", builtin_join::INST),
 		("lines", builtin_lines::INST),
+		("deepJoin", builtin_deep_join::INST),
 		("reverse", builtin_reverse::INST),
 		("any", builtin_any::INST),
 		("all", builtin_all::INST),
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -11,14 +11,6 @@
     else
       { [k]: func(k, obj[k]) for k in std.objectFields(obj) },
 
-  deepJoin(arr)::
-    if std.isString(arr) then
-      arr
-    else if std.isArray(arr) then
-      std.join('', [std.deepJoin(x) for x in arr])
-    else
-      error 'Expected string or array, got %s' % std.type(arr),
-
   assertEqual(a, b)::
     if a == b then
       true