difftreelog
fix build on stable
in: master
16 files changed
crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -26,16 +26,22 @@
/// This kind of arrays is generated by `std.range(start, end)` call, and used for loops.
Range(RangeArray),
/// Sliced array view.
- Slice(Box<SliceArray>),
+ Slice(Cc<SliceArray>),
/// Reversed array view.
/// Returned by `std.reverse(other)` call
- Reverse(Box<ReverseArray>),
+ Reverse(Cc<ReverseArray>),
/// Returned by `std.map` call
Mapped(MappedArray),
/// Returned by `std.repeat` call
Repeated(RepeatedArray),
}
+pub trait ArrayLikeIter<T>: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator {}
+impl<I, T> ArrayLikeIter<T> for I where
+ I: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator
+{
+}
+
impl ArrValue {
pub fn empty() -> Self {
Self::Range(RangeArray::empty())
@@ -123,7 +129,7 @@
return None;
}
- Some(Self::Slice(Box::new(SliceArray {
+ Some(Self::Slice(Cc::new(SliceArray {
inner: self,
from: from as u32,
to: to as u32,
@@ -160,24 +166,26 @@
pass!(self.get_lazy(index))
}
- /// Evaluate all array elements, returning new array.
- pub fn evaluatedcc(&self) -> Result<Cc<Vec<Val>>> {
- self.evaluated().map(Cc::new)
- }
- pub fn evaluated(&self) -> Result<Vec<Val>> {
- pass!(self.evaluated())
- }
-
- /// Iterate over elements, evaluating them.
+ #[cfg(feature = "nightly")]
pub fn iter(&self) -> UnknownArrayIter<'_> {
pass_iter_call!(self.iter => UnknownArrayIter)
}
+ #[cfg(not(feature = "nightly"))]
+ pub fn iter(&self) -> impl ArrayLikeIter<Result<Val>> + '_ {
+ (0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))
+ }
/// Iterate over elements, returning lazy values.
+ #[cfg(feature = "nightly")]
pub fn iter_lazy(&self) -> UnknownArrayIterLazy<'_> {
pass_iter_call!(self.iter_lazy => UnknownArrayIterLazy)
}
+ #[cfg(not(feature = "nightly"))]
+ pub fn iter_lazy(&self) -> impl ArrayLikeIter<Thunk<Val>> + '_ {
+ (0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))
+ }
+ #[cfg(feature = "nightly")]
pub fn iter_cheap(&self) -> Option<UnknownArrayIterCheap<'_>> {
macro_rules! question {
($v:expr) => {
@@ -187,10 +195,19 @@
Some(pass_iter_call!(self.iter_cheap in question => UnknownArrayIterCheap))
}
+ #[cfg(not(feature = "nightly"))]
+ pub fn iter_cheap(&self) -> Option<impl ArrayLikeIter<Val> + '_> {
+ if self.is_cheap() {
+ Some((0..self.len()).map(|i| self.get_cheap(i).expect("length and is_cheap checked")))
+ } else {
+ None
+ }
+ }
+
/// Return a reversed view on current array.
#[must_use]
pub fn reversed(self) -> Self {
- Self::Reverse(Box::new(ReverseArray(self)))
+ Self::Reverse(Cc::new(ReverseArray(self)))
}
pub fn ptr_eq(a: &Self, b: &Self) -> bool {
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth1use std::{2 cell::RefCell,3 iter::{self, Rev},4 mem::replace,5};67use jrsonnet_gcmodule::{Cc, Trace};8use jrsonnet_interner::IBytes;9use jrsonnet_parser::LocExpr;1011use super::ArrValue;12use crate::{13 error::ErrorKind::InfiniteRecursionDetected, evaluate, function::FuncVal, tb, typed::Any,14 val::ThunkValue, Context, Error, Result, Thunk, Val,15};1617pub trait ArrayLike {18 type Iter<'t>19 where20 Self: 't;21 type IterLazy<'t>22 where23 Self: 't;24 type IterCheap<'t>25 where26 Self: 't;2728 fn len(&self) -> usize;29 fn is_empty(&self) -> bool {30 self.len() == 031 }32 fn get(&self, index: usize) -> Result<Option<Val>>;33 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>>;34 fn get_cheap(&self, index: usize) -> Option<Val>;35 fn evaluated(&self) -> Result<Vec<Val>>;36 #[allow(clippy::iter_not_returning_iterator)]37 fn iter(&self) -> Self::Iter<'_>;38 fn iter_lazy(&self) -> Self::IterLazy<'_>;39 fn iter_cheap(&self) -> Option<Self::IterCheap<'_>>;40}4142#[derive(Debug, Clone, Trace)]43pub struct SliceArray {44 pub(crate) inner: ArrValue,45 pub(crate) from: u32,46 pub(crate) to: u32,47 pub(crate) step: u32,48}49type SliceArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;50type SliceArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;51type SliceArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;52impl ArrayLike for SliceArray {53 type Iter<'t> = SliceArrayIter<'t>;5455 type IterLazy<'t> = SliceArrayLazyIter<'t>;5657 type IterCheap<'t> = SliceArrayCheapIter<'t>;5859 fn len(&self) -> usize {60 iter::repeat(())61 .take((self.to - self.from) as usize)62 .step_by(self.step as usize)63 .count()64 }6566 fn get(&self, index: usize) -> Result<Option<Val>> {67 self.iter().nth(index).transpose()68 }6970 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {71 self.iter_lazy().nth(index)72 }7374 fn get_cheap(&self, index: usize) -> Option<Val> {75 self.iter_cheap()?.nth(index)76 }7778 fn evaluated(&self) -> Result<Vec<Val>> {79 self.iter().collect()80 }8182 fn iter(&self) -> SliceArrayIter<'_> {83 self.inner84 .iter()85 .skip(self.from as usize)86 .take((self.to - self.from) as usize)87 .step_by(self.step as usize)88 }8990 fn iter_lazy(&self) -> SliceArrayLazyIter<'_> {91 self.inner92 .iter_lazy()93 .skip(self.from as usize)94 .take((self.to - self.from) as usize)95 .step_by(self.step as usize)96 }9798 fn iter_cheap(&self) -> Option<SliceArrayCheapIter<'_>> {99 Some(100 self.inner101 .iter_cheap()?102 .skip(self.from as usize)103 .take((self.to - self.from) as usize)104 .step_by(self.step as usize),105 )106 }107}108109#[derive(Trace, Debug, Clone)]110pub struct BytesArray(pub IBytes);111type BytesArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;112type BytesArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;113type BytesArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;114impl ArrayLike for BytesArray {115 type Iter<'t> = BytesArrayIter<'t>;116117 type IterLazy<'t> = BytesArrayLazyIter<'t>;118119 type IterCheap<'t> = BytesArrayCheapIter<'t>;120121 fn len(&self) -> usize {122 self.0.len()123 }124125 fn get(&self, index: usize) -> Result<Option<Val>> {126 Ok(self.get_cheap(index))127 }128129 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {130 self.get_cheap(index).map(Thunk::evaluated)131 }132133 fn get_cheap(&self, index: usize) -> Option<Val> {134 self.0.get(index).map(|v| Val::Num(f64::from(*v)))135 }136137 fn evaluated(&self) -> Result<Vec<Val>> {138 self.iter().collect()139 }140141 fn iter(&self) -> BytesArrayIter<'_> {142 self.0.iter().map(|v| Ok(Val::Num(f64::from(*v))))143 }144145 fn iter_lazy(&self) -> BytesArrayLazyIter<'_> {146 self.0147 .iter()148 .map(|v| Thunk::evaluated(Val::Num(f64::from(*v))))149 }150151 fn iter_cheap(&self) -> Option<BytesArrayCheapIter<'_>> {152 Some(self.0.iter().map(|v| Val::Num(f64::from(*v))))153 }154}155156#[derive(Debug, Trace, Clone)]157enum ArrayThunk<T: 'static + Trace> {158 Computed(Val),159 Errored(Error),160 Waiting(T),161 Pending,162}163164#[derive(Debug, Trace)]165pub struct ExprArrayInner {166 ctx: Context,167 cached: RefCell<Vec<ArrayThunk<LocExpr>>>,168}169#[derive(Debug, Trace, Clone)]170pub struct ExprArray(pub Cc<ExprArrayInner>);171type ExprArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;172type ExprArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;173type ExprArrayCheapIter<'t> = iter::Empty<Val>;174impl ExprArray {175 pub fn new(ctx: Context, items: impl IntoIterator<Item = LocExpr>) -> Self {176 Self(Cc::new(ExprArrayInner {177 ctx,178 cached: RefCell::new(items.into_iter().map(ArrayThunk::Waiting).collect()),179 }))180 }181}182impl ArrayLike for ExprArray {183 type Iter<'t> = ExprArrayIter<'t>;184185 type IterLazy<'t> = ExprArrayLazyIter<'t>;186187 type IterCheap<'t> = ExprArrayCheapIter<'t>;188189 fn len(&self) -> usize {190 self.0.cached.borrow().len()191 }192 fn get(&self, index: usize) -> Result<Option<Val>> {193 if index >= self.len() {194 return Ok(None);195 }196 match &self.0.cached.borrow()[index] {197 ArrayThunk::Computed(c) => return Ok(Some(c.clone())),198 ArrayThunk::Errored(e) => return Err(e.clone()),199 ArrayThunk::Pending => return Err(InfiniteRecursionDetected.into()),200 ArrayThunk::Waiting(..) => {}201 };202203 let ArrayThunk::Waiting(expr) = replace(&mut self.0.cached.borrow_mut()[index], ArrayThunk::Pending) else {204 unreachable!()205 };206207 let new_value = match evaluate(self.0.ctx.clone(), &expr) {208 Ok(v) => v,209 Err(e) => {210 self.0.cached.borrow_mut()[index] = ArrayThunk::Errored(e.clone());211 return Err(e);212 }213 };214 self.0.cached.borrow_mut()[index] = ArrayThunk::Computed(new_value.clone());215 Ok(Some(new_value))216 }217 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {218 #[derive(Trace)]219 struct ArrayElement {220 arr_thunk: ExprArray,221 index: usize,222 }223224 impl ThunkValue for ArrayElement {225 type Output = Val;226227 fn get(self: Box<Self>) -> Result<Self::Output> {228 self.arr_thunk229 .get(self.index)230 .transpose()231 .expect("index checked")232 }233 }234235 if index >= self.len() {236 return None;237 }238 match &self.0.cached.borrow()[index] {239 ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())),240 ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())),241 ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}242 };243244 Some(Thunk::new(tb!(ArrayElement {245 arr_thunk: self.clone(),246 index,247 })))248 }249 fn get_cheap(&self, _index: usize) -> Option<Val> {250 None251 }252253 fn iter(&self) -> ExprArrayIter<'_> {254 (0..self.len()).map(|i| self.get(i).transpose().expect("index checked"))255 }256 fn iter_lazy(&self) -> ExprArrayLazyIter<'_> {257 (0..self.len()).map(|i| self.get_lazy(i).expect("index checked"))258 }259 fn iter_cheap(&self) -> Option<ExprArrayCheapIter<'_>> {260 None261 }262263 fn evaluated(&self) -> Result<Vec<Val>> {264 self.iter().collect()265 }266}267268#[derive(Trace, Debug, Clone)]269pub struct ExtendedArray {270 pub a: ArrValue,271 pub b: ArrValue,272 split: usize,273 len: usize,274}275type ExtendedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + 't;276type ExtendedArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + 't;277type ExtendedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + 't;278impl ExtendedArray {279 pub fn new(a: ArrValue, b: ArrValue) -> Self {280 let a_len = a.len();281 let b_len = b.len();282 Self {283 a,284 b,285 split: a_len,286 len: a_len.checked_add(b_len).expect("too large array value"),287 }288 }289}290impl ArrayLike for ExtendedArray {291 type Iter<'t> = ExtendedArrayIter<'t>;292293 type IterLazy<'t> = ExtendedArrayLazyIter<'t>;294295 type IterCheap<'t> = ExtendedArrayCheapIter<'t>;296297 fn get(&self, index: usize) -> Result<Option<Val>> {298 if self.split > index {299 self.a.get(index)300 } else {301 self.b.get(index - self.split)302 }303 }304 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {305 if self.split > index {306 self.a.get_lazy(index)307 } else {308 self.b.get_lazy(index - self.split)309 }310 }311312 fn len(&self) -> usize {313 self.len314 }315316 fn get_cheap(&self, index: usize) -> Option<Val> {317 if self.split > index {318 self.a.get_cheap(index)319 } else {320 self.b.get_cheap(index - self.split)321 }322 }323324 fn evaluated(&self) -> Result<Vec<Val>> {325 let mut out = self.a.evaluated()?;326 out.extend(self.b.evaluated()?.into_iter());327 Ok(out)328 }329330 fn iter(&self) -> ExtendedArrayIter<'_> {331 self.a.iter().chain(self.b.iter())332 }333 fn iter_lazy(&self) -> ExtendedArrayLazyIter<'_> {334 self.a.iter_lazy().chain(self.b.iter_lazy())335 }336 fn iter_cheap(&self) -> Option<ExtendedArrayCheapIter<'_>> {337 let a = self.a.iter_cheap()?;338 let b = self.b.iter_cheap()?;339 Some(a.chain(b))340 }341}342343#[derive(Trace, Debug, Clone)]344pub struct LazyArray(pub Cc<Vec<Thunk<Val>>>);345type LazyArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;346type LazyArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;347type LazyArrayCheapIter<'t> = iter::Empty<Val>;348impl ArrayLike for LazyArray {349 type Iter<'t> = LazyArrayIter<'t>;350351 type IterLazy<'t> = LazyArrayLazyIter<'t>;352353 type IterCheap<'t> = LazyArrayCheapIter<'t>;354355 fn len(&self) -> usize {356 self.0.len()357 }358 fn get(&self, index: usize) -> Result<Option<Val>> {359 let Some(v) = self.0.get(index) else {360 return Ok(None);361 };362 v.evaluate().map(Some)363 }364 fn get_cheap(&self, _index: usize) -> Option<Val> {365 None366 }367 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {368 self.0.get(index).cloned()369 }370 fn evaluated(&self) -> Result<Vec<Val>> {371 let mut out = Vec::with_capacity(self.len());372 for i in self.0.iter() {373 out.push(i.evaluate()?);374 }375 Ok(out)376 }377 fn iter(&self) -> LazyArrayIter<'_> {378 self.0.iter().map(Thunk::evaluate)379 }380 fn iter_lazy(&self) -> LazyArrayLazyIter<'_> {381 self.0.iter().cloned()382 }383 fn iter_cheap(&self) -> Option<LazyArrayCheapIter<'_>> {384 None385 }386}387388#[derive(Trace, Debug, Clone)]389pub struct EagerArray(pub Cc<Vec<Val>>);390type EagerArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;391type EagerArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;392type EagerArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;393impl ArrayLike for EagerArray {394 type Iter<'t> = EagerArrayIter<'t>;395396 type IterLazy<'t> = EagerArrayLazyIter<'t>;397398 type IterCheap<'t> = EagerArrayCheapIter<'t>;399400 fn len(&self) -> usize {401 self.0.len()402 }403404 fn get(&self, index: usize) -> Result<Option<Val>> {405 Ok(self.0.get(index).cloned())406 }407408 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {409 self.0.get(index).cloned().map(Thunk::evaluated)410 }411412 fn get_cheap(&self, index: usize) -> Option<Val> {413 self.0.get(index).cloned()414 }415416 fn evaluated(&self) -> Result<Vec<Val>> {417 Ok((*self.0).clone())418 }419420 fn iter(&self) -> EagerArrayIter<'_> {421 self.0.iter().cloned().map(Ok)422 }423424 fn iter_lazy(&self) -> EagerArrayLazyIter<'_> {425 self.0.iter().cloned().map(Thunk::evaluated)426 }427428 fn iter_cheap(&self) -> Option<EagerArrayCheapIter<'_>> {429 Some(self.0.iter().cloned())430 }431}432433/// Inclusive range type434#[derive(Debug, Trace, Clone, PartialEq, Eq)]435pub struct RangeArray {436 start: i32,437 end: i32,438}439struct RangeIter {440 start: i32,441 end: i32,442}443impl RangeIter {444 fn finished(&self) -> bool {445 self.end < self.start446 }447 fn finish(&mut self) {448 self.start = 0;449 self.end = -1;450 }451}452impl Iterator for RangeIter {453 type Item = i32;454455 fn next(&mut self) -> Option<Self::Item> {456 if self.finished() {457 return None;458 }459 let v = self.start;460 if v == self.end {461 self.finish();462 } else {463 self.start = v + 1;464 }465 Some(v)466 }467 fn nth(&mut self, n: usize) -> Option<Self::Item> {468 let v = (self.start as usize) + n;469 if v > self.end as usize {470 self.finish();471 None472 } else {473 self.start = v as i32;474 self.next()475 }476 }477 fn size_hint(&self) -> (usize, Option<usize>) {478 let len = self.len();479 (len, Some(len))480 }481}482impl DoubleEndedIterator for RangeIter {483 fn next_back(&mut self) -> Option<Self::Item> {484 if self.finished() {485 return None;486 }487 let v = self.end;488 if v == self.start {489 self.finish();490 } else {491 self.end = v - 1;492 }493 Some(v)494 }495 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {496 let v = (self.end as usize) - n;497 if v < self.start as usize {498 self.finish();499 None500 } else {501 self.end = v as i32;502 self.next_back()503 }504 }505}506impl ExactSizeIterator for RangeIter {507 fn len(&self) -> usize {508 if self.finished() {509 0510 } else {511 (self.end as isize - self.start as isize + 1) as usize512 }513 }514}515impl RangeArray {516 pub fn empty() -> Self {517 Self::new_exclusive(0, 0)518 }519 pub fn new_exclusive(start: i32, end: i32) -> Self {520 end.checked_sub(1)521 .map_or_else(Self::empty, |end| Self { start, end })522 }523 pub fn new_inclusive(start: i32, end: i32) -> Self {524 Self { start, end }525 }526 fn range(&self) -> RangeIter {527 RangeIter {528 start: self.start,529 end: self.end,530 }531 }532}533534type RangeArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;535type RangeArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;536type RangeArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;537impl ArrayLike for RangeArray {538 type Iter<'t> = RangeArrayIter<'t>;539540 type IterLazy<'t> = RangeArrayLazyIter<'t>;541542 type IterCheap<'t> = RangeArrayCheapIter<'t>;543544 fn len(&self) -> usize {545 self.range().len()546 }547 fn is_empty(&self) -> bool {548 self.range().finished()549 }550551 fn get(&self, index: usize) -> Result<Option<Val>> {552 Ok(self.get_cheap(index))553 }554555 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {556 self.get_cheap(index).map(Thunk::evaluated)557 }558559 fn get_cheap(&self, index: usize) -> Option<Val> {560 self.range().nth(index).map(|i| Val::Num(f64::from(i)))561 }562563 fn evaluated(&self) -> Result<Vec<Val>> {564 Ok(self.range().map(|i| Val::Num(f64::from(i))).collect())565 }566567 fn iter(&self) -> RangeArrayIter<'_> {568 self.range().map(|i| Ok(Val::Num(f64::from(i))))569 }570571 fn iter_lazy(&self) -> RangeArrayLazyIter<'_> {572 self.range()573 .map(|i| Thunk::evaluated(Val::Num(f64::from(i))))574 }575576 fn iter_cheap(&self) -> Option<RangeArrayCheapIter<'_>> {577 Some(self.range().map(|i| Val::Num(f64::from(i))))578 }579}580581#[derive(Debug, Trace, Clone)]582pub struct ReverseArray(pub ArrValue);583impl ArrayLike for ReverseArray {584 type Iter<'t> = Rev<UnknownArrayIter<'t>>;585586 type IterLazy<'t> = Rev<UnknownArrayIterLazy<'t>>;587588 type IterCheap<'t> = Rev<UnknownArrayIterCheap<'t>>;589590 fn len(&self) -> usize {591 self.0.len()592 }593594 fn get(&self, index: usize) -> Result<Option<Val>> {595 self.0.get(self.0.len() - index - 1)596 }597598 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {599 self.0.get_lazy(self.0.len() - index - 1)600 }601602 fn get_cheap(&self, index: usize) -> Option<Val> {603 self.0.get_cheap(self.0.len() - index - 1)604 }605606 fn evaluated(&self) -> Result<Vec<Val>> {607 let mut v = self.0.evaluated()?;608 v.reverse();609 Ok(v)610 }611612 fn iter(&self) -> Rev<UnknownArrayIter<'_>> {613 self.0.iter().rev()614 }615616 fn iter_lazy(&self) -> Rev<UnknownArrayIterLazy<'_>> {617 self.0.iter_lazy().rev()618 }619620 fn iter_cheap(&self) -> Option<Rev<UnknownArrayIterCheap<'_>>> {621 Some(self.0.iter_cheap()?.rev())622 }623}624625#[derive(Trace, Debug)]626pub struct MappedArrayInner {627 inner: ArrValue,628 cached: RefCell<Vec<ArrayThunk<()>>>,629 mapper: FuncVal,630}631#[derive(Trace, Debug, Clone)]632pub struct MappedArray(Cc<MappedArrayInner>);633impl MappedArray {634 pub fn new(inner: ArrValue, mapper: FuncVal) -> Self {635 let len = inner.len();636 Self(Cc::new(MappedArrayInner {637 inner,638 cached: RefCell::new(vec![ArrayThunk::Waiting(()); len]),639 mapper,640 }))641 }642}643type MappedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;644type MappedArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;645type MappedArrayCheapIter<'t> = iter::Empty<Val>;646impl ArrayLike for MappedArray {647 type Iter<'t> = MappedArrayIter<'t>;648 type IterLazy<'t> = MappedArrayLazyIter<'t>;649 type IterCheap<'t> = MappedArrayCheapIter<'t>;650651 fn len(&self) -> usize {652 self.0.cached.borrow().len()653 }654655 fn get(&self, index: usize) -> Result<Option<Val>> {656 if index >= self.len() {657 return Ok(None);658 }659 match &self.0.cached.borrow()[index] {660 ArrayThunk::Computed(c) => return Ok(Some(c.clone())),661 ArrayThunk::Errored(e) => return Err(e.clone()),662 ArrayThunk::Pending => return Err(InfiniteRecursionDetected.into()),663 ArrayThunk::Waiting(..) => {}664 };665666 let ArrayThunk::Waiting(_) = replace(&mut self.0.cached.borrow_mut()[index], ArrayThunk::Pending) else {667 unreachable!()668 };669670 let val = self671 .0672 .inner673 .get(index)674 .transpose()675 .expect("index checked")676 .and_then(|r| self.0.mapper.evaluate_simple(&(Any(r),)));677678 let new_value = match val {679 Ok(v) => v,680 Err(e) => {681 self.0.cached.borrow_mut()[index] = ArrayThunk::Errored(e.clone());682 return Err(e);683 }684 };685 self.0.cached.borrow_mut()[index] = ArrayThunk::Computed(new_value.clone());686 Ok(Some(new_value))687 }688 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {689 #[derive(Trace)]690 struct ArrayElement {691 arr_thunk: MappedArray,692 index: usize,693 }694695 impl ThunkValue for ArrayElement {696 type Output = Val;697698 fn get(self: Box<Self>) -> Result<Self::Output> {699 self.arr_thunk700 .get(self.index)701 .transpose()702 .expect("index checked")703 }704 }705706 if index >= self.len() {707 return None;708 }709 match &self.0.cached.borrow()[index] {710 ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())),711 ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())),712 ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}713 };714715 Some(Thunk::new(tb!(ArrayElement {716 arr_thunk: self.clone(),717 index,718 })))719 }720721 fn get_cheap(&self, _index: usize) -> Option<Val> {722 None723 }724725 fn evaluated(&self) -> Result<Vec<Val>> {726 self.iter().collect()727 }728729 fn iter(&self) -> MappedArrayIter<'_> {730 (0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))731 }732733 fn iter_lazy(&self) -> MappedArrayLazyIter<'_> {734 (0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))735 }736737 fn iter_cheap(&self) -> Option<Self::IterCheap<'_>> {738 None739 }740}741// impl MappedArray742743#[derive(Trace, Debug)]744pub struct RepeatedArrayInner {745 data: ArrValue,746 repeats: usize,747 total_len: usize,748}749#[derive(Trace, Debug, Clone)]750pub struct RepeatedArray(Cc<RepeatedArrayInner>);751impl RepeatedArray {752 pub fn new(data: ArrValue, repeats: usize) -> Option<Self> {753 let total_len = data.len().checked_mul(repeats)?;754 Some(Self(Cc::new(RepeatedArrayInner {755 data,756 repeats,757 total_len,758 })))759 }760 pub fn is_cheap(&self) -> bool {761 self.0.data.is_cheap()762 }763}764765type RepeatedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;766type RepeatedArrayLazyIter<'t> =767 impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;768type RepeatedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;769impl ArrayLike for RepeatedArray {770 type Iter<'t> = RepeatedArrayIter<'t>;771 type IterLazy<'t> = RepeatedArrayLazyIter<'t>;772 type IterCheap<'t> = RepeatedArrayCheapIter<'t>;773774 fn len(&self) -> usize {775 self.0.total_len776 }777778 fn get(&self, index: usize) -> Result<Option<Val>> {779 if index > self.0.total_len {780 return Ok(None);781 }782 self.0.data.get(index % self.0.data.len())783 }784785 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {786 if index > self.0.total_len {787 return None;788 }789 self.0.data.get_lazy(index % self.0.data.len())790 }791792 fn get_cheap(&self, index: usize) -> Option<Val> {793 if index > self.0.total_len {794 return None;795 }796 self.0.data.get_cheap(index % self.0.data.len())797 }798799 fn evaluated(&self) -> Result<Vec<Val>> {800 let mut data = self.0.data.evaluated()?;801 let data_range = 0..data.len();802 for _ in 1..self.0.repeats {803 data.extend_from_within(data_range.clone());804 }805 Ok(data)806 }807808 fn iter(&self) -> RepeatedArrayIter<'_> {809 (0..self.0.total_len)810 .map(|i| self.get(i))811 .map(Result::transpose)812 .map(Option::unwrap)813 }814815 fn iter_lazy(&self) -> RepeatedArrayLazyIter<'_> {816 (0..self.0.total_len)817 .map(|i| self.get_lazy(i))818 .map(Option::unwrap)819 }820821 fn iter_cheap(&self) -> Option<RepeatedArrayCheapIter<'_>> {822 if !self.0.data.is_cheap() {823 return None;824 }825 Some(826 (0..self.0.total_len)827 .map(|i| self.get_cheap(i))828 .map(Option::unwrap),829 )830 }831}832833macro_rules! impl_iter_enum {834 ($n:ident => $v:ident) => {835 pub enum $n<'t> {836 Bytes(<BytesArray as ArrayLike>::$v<'t>),837 Expr(<ExprArray as ArrayLike>::$v<'t>),838 Lazy(<LazyArray as ArrayLike>::$v<'t>),839 Eager(<EagerArray as ArrayLike>::$v<'t>),840 Range(<RangeArray as ArrayLike>::$v<'t>),841 Slice(Box<<SliceArray as ArrayLike>::$v<'t>>),842 Extended(Box<<ExtendedArray as ArrayLike>::$v<'t>>),843 Reverse(Box<<ReverseArray as ArrayLike>::$v<'t>>),844 Mapped(Box<<MappedArray as ArrayLike>::$v<'t>>),845 Repeated(Box<<RepeatedArray as ArrayLike>::$v<'t>>),846 }847 };848}849850macro_rules! pass {851 ($t:ident.$m:ident($($ident:ident),*)) => {852 match $t {853 Self::Bytes(e) => e.$m($($ident)*),854 Self::Expr(e) => e.$m($($ident)*),855 Self::Lazy(e) => e.$m($($ident)*),856 Self::Eager(e) => e.$m($($ident)*),857 Self::Range(e) => e.$m($($ident)*),858 Self::Slice(e) => e.$m($($ident)*),859 Self::Extended(e) => e.$m($($ident)*),860 Self::Reverse(e) => e.$m($($ident)*),861 Self::Mapped(e) => e.$m($($ident)*),862 Self::Repeated(e) => e.$m($($ident)*),863 }864 };865}866pub(super) use pass;867868macro_rules! pass_iter_call {869 ($t:ident.$c:ident $(in $wrap:ident)? => $e:ident) => {870 match $t {871 ArrValue::Bytes(e) => $e::Bytes($($wrap!)?(e.$c())),872 ArrValue::Lazy(e) => $e::Lazy($($wrap!)?(e.$c())),873 ArrValue::Expr(e) => $e::Expr($($wrap!)?(e.$c())),874 ArrValue::Eager(e) => $e::Eager($($wrap!)?(e.$c())),875 ArrValue::Range(e) => $e::Range($($wrap!)?(e.$c())),876 ArrValue::Slice(e) => $e::Slice(Box::new($($wrap!)?(e.$c()))),877 ArrValue::Extended(e) => $e::Extended(Box::new($($wrap!)?(e.$c()))),878 ArrValue::Reverse(e) => $e::Reverse(Box::new($($wrap!)?(e.$c()))),879 ArrValue::Mapped(e) => $e::Mapped(Box::new($($wrap!)?(e.$c()))),880 ArrValue::Repeated(e) => $e::Repeated(Box::new($($wrap!)?(e.$c()))),881 }882 };883}884pub(super) use pass_iter_call;885886macro_rules! impl_iter {887 ($t:ident => $out:ty) => {888 impl Iterator for $t<'_> {889 type Item = $out;890891 fn next(&mut self) -> Option<Self::Item> {892 pass!(self.next())893 }894 fn nth(&mut self, count: usize) -> Option<Self::Item> {895 pass!(self.nth(count))896 }897 fn size_hint(&self) -> (usize, Option<usize>) {898 pass!(self.size_hint())899 }900 }901 impl DoubleEndedIterator for $t<'_> {902 fn next_back(&mut self) -> Option<Self::Item> {903 pass!(self.next_back())904 }905 fn nth_back(&mut self, count: usize) -> Option<Self::Item> {906 pass!(self.nth_back(count))907 }908 }909 impl ExactSizeIterator for $t<'_> {910 fn len(&self) -> usize {911 match self {912 Self::Bytes(e) => e.len(),913 Self::Expr(e) => e.len(),914 Self::Lazy(e) => e.len(),915 Self::Eager(e) => e.len(),916 Self::Range(e) => e.len(),917 Self::Slice(e) => e.len(),918 Self::Extended(e) => {919 e.size_hint().1.expect("overflow is checked in constructor")920 }921 Self::Reverse(e) => e.len(),922 Self::Mapped(e) => e.len(),923 Self::Repeated(e) => e.len(),924 }925 }926 }927 };928}929930impl_iter_enum!(UnknownArrayIter => Iter);931impl_iter_enum!(UnknownArrayIterLazy => IterLazy);932impl_iter_enum!(UnknownArrayIterCheap => IterCheap);933impl_iter!(UnknownArrayIter => Result<Val>);934impl_iter!(UnknownArrayIterLazy => Thunk<Val>);935impl_iter!(UnknownArrayIterCheap => Val);1//! Those implementations are a bit sketchy, as this is mostly performance experiments2//! of not yet finished nightly rust features34use std::{cell::RefCell, iter, mem::replace};56use jrsonnet_gcmodule::{Cc, Trace};7use jrsonnet_interner::IBytes;8use jrsonnet_parser::LocExpr;910use super::{ArrValue, ArrayLikeIter};11use crate::{12 error::ErrorKind::InfiniteRecursionDetected, evaluate, function::FuncVal, tb, typed::Any,13 val::ThunkValue, Context, Error, Result, Thunk, Val,14};1516pub trait ArrayLike: Sized + Into<ArrValue> {17 #[cfg(feature = "nightly")]18 type Iter<'t>19 where20 Self: 't;21 #[cfg(feature = "nightly")]22 type IterLazy<'t>23 where24 Self: 't;25 #[cfg(feature = "nightly")]26 type IterCheap<'t>27 where28 Self: 't;2930 fn len(&self) -> usize;31 fn is_empty(&self) -> bool {32 self.len() == 033 }34 fn get(&self, index: usize) -> Result<Option<Val>>;35 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>>;36 fn get_cheap(&self, index: usize) -> Option<Val>;37 #[cfg(feature = "nightly")]38 #[allow(clippy::iter_not_returning_iterator)]39 fn iter(&self) -> Self::Iter<'_>;40 #[cfg(feature = "nightly")]41 fn iter_lazy(&self) -> Self::IterLazy<'_>;42 #[cfg(feature = "nightly")]43 fn iter_cheap(&self) -> Option<Self::IterCheap<'_>>;4445 fn reverse(self) -> ArrValue {46 ArrValue::Reverse(Cc::new(ReverseArray(self.into())))47 }48}4950#[derive(Debug, Clone, Trace)]51pub struct SliceArray {52 pub(crate) inner: ArrValue,53 pub(crate) from: u32,54 pub(crate) to: u32,55 pub(crate) step: u32,56}5758impl SliceArray {59 #[cfg(not(feature = "nightly"))]60 fn iter(&self) -> impl Iterator<Item = Result<Val>> + '_ {61 self.inner62 .iter()63 .skip(self.from as usize)64 .take((self.to - self.from) as usize)65 .step_by(self.step as usize)66 }6768 #[cfg(not(feature = "nightly"))]69 fn iter_lazy(&self) -> impl Iterator<Item = Thunk<Val>> + '_ {70 self.inner71 .iter_lazy()72 .skip(self.from as usize)73 .take((self.to - self.from) as usize)74 .step_by(self.step as usize)75 }7677 #[cfg(not(feature = "nightly"))]78 fn iter_cheap(&self) -> Option<impl ArrayLikeIter<Val> + '_> {79 Some(80 self.inner81 .iter_cheap()?82 .skip(self.from as usize)83 .take((self.to - self.from) as usize)84 .step_by(self.step as usize),85 )86 }87}88#[cfg(feature = "nightly")]89type SliceArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;90#[cfg(feature = "nightly")]91type SliceArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;92#[cfg(feature = "nightly")]93type SliceArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;94impl ArrayLike for SliceArray {95 #[cfg(feature = "nightly")]96 type Iter<'t> = SliceArrayIter<'t>;97 #[cfg(feature = "nightly")]98 type IterLazy<'t> = SliceArrayLazyIter<'t>;99 #[cfg(feature = "nightly")]100 type IterCheap<'t> = SliceArrayCheapIter<'t>;101102 fn len(&self) -> usize {103 iter::repeat(())104 .take((self.to - self.from) as usize)105 .step_by(self.step as usize)106 .count()107 }108109 fn get(&self, index: usize) -> Result<Option<Val>> {110 self.iter().nth(index).transpose()111 }112113 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {114 self.iter_lazy().nth(index)115 }116117 fn get_cheap(&self, index: usize) -> Option<Val> {118 self.iter_cheap()?.nth(index)119 }120121 #[cfg(feature = "nightly")]122 fn iter(&self) -> SliceArrayIter<'_> {123 self.inner124 .iter()125 .skip(self.from as usize)126 .take((self.to - self.from) as usize)127 .step_by(self.step as usize)128 }129130 #[cfg(feature = "nightly")]131 fn iter_lazy(&self) -> SliceArrayLazyIter<'_> {132 self.inner133 .iter_lazy()134 .skip(self.from as usize)135 .take((self.to - self.from) as usize)136 .step_by(self.step as usize)137 }138139 #[cfg(feature = "nightly")]140 fn iter_cheap(&self) -> Option<SliceArrayCheapIter<'_>> {141 Some(142 self.inner143 .iter_cheap()?144 .skip(self.from as usize)145 .take((self.to - self.from) as usize)146 .step_by(self.step as usize),147 )148 }149}150impl From<SliceArray> for ArrValue {151 fn from(value: SliceArray) -> Self {152 Self::Slice(Cc::new(value))153 }154}155156#[derive(Trace, Debug, Clone)]157pub struct BytesArray(pub IBytes);158#[cfg(feature = "nightly")]159type BytesArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;160#[cfg(feature = "nightly")]161type BytesArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;162#[cfg(feature = "nightly")]163type BytesArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;164impl ArrayLike for BytesArray {165 #[cfg(feature = "nightly")]166 type Iter<'t> = BytesArrayIter<'t>;167 #[cfg(feature = "nightly")]168 type IterLazy<'t> = BytesArrayLazyIter<'t>;169 #[cfg(feature = "nightly")]170 type IterCheap<'t> = BytesArrayCheapIter<'t>;171172 fn len(&self) -> usize {173 self.0.len()174 }175176 fn get(&self, index: usize) -> Result<Option<Val>> {177 Ok(self.get_cheap(index))178 }179180 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {181 self.get_cheap(index).map(Thunk::evaluated)182 }183184 fn get_cheap(&self, index: usize) -> Option<Val> {185 self.0.get(index).map(|v| Val::Num(f64::from(*v)))186 }187188 #[cfg(feature = "nightly")]189 fn iter(&self) -> BytesArrayIter<'_> {190 self.0.iter().map(|v| Ok(Val::Num(f64::from(*v))))191 }192193 #[cfg(feature = "nightly")]194 fn iter_lazy(&self) -> BytesArrayLazyIter<'_> {195 self.0196 .iter()197 .map(|v| Thunk::evaluated(Val::Num(f64::from(*v))))198 }199200 #[cfg(feature = "nightly")]201 fn iter_cheap(&self) -> Option<BytesArrayCheapIter<'_>> {202 Some(self.0.iter().map(|v| Val::Num(f64::from(*v))))203 }204}205impl From<BytesArray> for ArrValue {206 fn from(value: BytesArray) -> Self {207 ArrValue::Bytes(value)208 }209}210211#[derive(Debug, Trace, Clone)]212enum ArrayThunk<T: 'static + Trace> {213 Computed(Val),214 Errored(Error),215 Waiting(T),216 Pending,217}218219#[derive(Debug, Trace)]220pub struct ExprArrayInner {221 ctx: Context,222 cached: RefCell<Vec<ArrayThunk<LocExpr>>>,223}224#[derive(Debug, Trace, Clone)]225pub struct ExprArray(pub Cc<ExprArrayInner>);226impl ExprArray {227 pub fn new(ctx: Context, items: impl IntoIterator<Item = LocExpr>) -> Self {228 Self(Cc::new(ExprArrayInner {229 ctx,230 cached: RefCell::new(items.into_iter().map(ArrayThunk::Waiting).collect()),231 }))232 }233}234#[cfg(feature = "nightly")]235type ExprArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;236#[cfg(feature = "nightly")]237type ExprArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;238#[cfg(feature = "nightly")]239type ExprArrayCheapIter<'t> = iter::Empty<Val>;240impl ArrayLike for ExprArray {241 #[cfg(feature = "nightly")]242 type Iter<'t> = ExprArrayIter<'t>;243 #[cfg(feature = "nightly")]244 type IterLazy<'t> = ExprArrayLazyIter<'t>;245 #[cfg(feature = "nightly")]246 type IterCheap<'t> = ExprArrayCheapIter<'t>;247248 fn len(&self) -> usize {249 self.0.cached.borrow().len()250 }251 fn get(&self, index: usize) -> Result<Option<Val>> {252 if index >= self.len() {253 return Ok(None);254 }255 match &self.0.cached.borrow()[index] {256 ArrayThunk::Computed(c) => return Ok(Some(c.clone())),257 ArrayThunk::Errored(e) => return Err(e.clone()),258 ArrayThunk::Pending => return Err(InfiniteRecursionDetected.into()),259 ArrayThunk::Waiting(..) => {}260 };261262 let ArrayThunk::Waiting(expr) = replace(&mut self.0.cached.borrow_mut()[index], ArrayThunk::Pending) else {263 unreachable!()264 };265266 let new_value = match evaluate(self.0.ctx.clone(), &expr) {267 Ok(v) => v,268 Err(e) => {269 self.0.cached.borrow_mut()[index] = ArrayThunk::Errored(e.clone());270 return Err(e);271 }272 };273 self.0.cached.borrow_mut()[index] = ArrayThunk::Computed(new_value.clone());274 Ok(Some(new_value))275 }276 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {277 #[derive(Trace)]278 struct ArrayElement {279 arr_thunk: ExprArray,280 index: usize,281 }282283 impl ThunkValue for ArrayElement {284 type Output = Val;285286 fn get(self: Box<Self>) -> Result<Self::Output> {287 self.arr_thunk288 .get(self.index)289 .transpose()290 .expect("index checked")291 }292 }293294 if index >= self.len() {295 return None;296 }297 match &self.0.cached.borrow()[index] {298 ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())),299 ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())),300 ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}301 };302303 Some(Thunk::new(tb!(ArrayElement {304 arr_thunk: self.clone(),305 index,306 })))307 }308 fn get_cheap(&self, _index: usize) -> Option<Val> {309 None310 }311312 #[cfg(feature = "nightly")]313 fn iter(&self) -> ExprArrayIter<'_> {314 (0..self.len()).map(|i| self.get(i).transpose().expect("index checked"))315 }316 #[cfg(feature = "nightly")]317 fn iter_lazy(&self) -> ExprArrayLazyIter<'_> {318 (0..self.len()).map(|i| self.get_lazy(i).expect("index checked"))319 }320 #[cfg(feature = "nightly")]321 fn iter_cheap(&self) -> Option<Self::IterCheap<'_>> {322 None323 }324}325impl From<ExprArray> for ArrValue {326 fn from(value: ExprArray) -> Self {327 Self::Expr(value)328 }329}330331#[derive(Trace, Debug, Clone)]332pub struct ExtendedArray {333 pub a: ArrValue,334 pub b: ArrValue,335 split: usize,336 len: usize,337}338#[cfg(feature = "nightly")]339340type ExtendedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;341#[cfg(feature = "nightly")]342type ExtendedArrayLazyIter<'t> =343 impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;344#[cfg(feature = "nightly")]345type ExtendedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;346impl ExtendedArray {347 pub fn new(a: ArrValue, b: ArrValue) -> Self {348 let a_len = a.len();349 let b_len = b.len();350 Self {351 a,352 b,353 split: a_len,354 len: a_len.checked_add(b_len).expect("too large array value"),355 }356 }357}358359struct WithExactSize<I>(I, usize);360impl<I, T> Iterator for WithExactSize<I>361where362 I: Iterator<Item = T>,363{364 type Item = T;365366 fn next(&mut self) -> Option<Self::Item> {367 self.0.next()368 }369 fn nth(&mut self, n: usize) -> Option<Self::Item> {370 self.0.nth(n)371 }372 fn size_hint(&self) -> (usize, Option<usize>) {373 (self.1, Some(self.1))374 }375}376impl<I> DoubleEndedIterator for WithExactSize<I>377where378 I: DoubleEndedIterator,379{380 fn next_back(&mut self) -> Option<Self::Item> {381 self.0.next_back()382 }383 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {384 self.0.nth_back(n)385 }386}387impl<I> ExactSizeIterator for WithExactSize<I>388where389 I: Iterator,390{391 fn len(&self) -> usize {392 self.1393 }394}395impl ArrayLike for ExtendedArray {396 #[cfg(feature = "nightly")]397 type Iter<'t> = ExtendedArrayIter<'t>;398 #[cfg(feature = "nightly")]399 type IterLazy<'t> = ExtendedArrayLazyIter<'t>;400 #[cfg(feature = "nightly")]401 type IterCheap<'t> = ExtendedArrayCheapIter<'t>;402403 fn get(&self, index: usize) -> Result<Option<Val>> {404 if self.split > index {405 self.a.get(index)406 } else {407 self.b.get(index - self.split)408 }409 }410 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {411 if self.split > index {412 self.a.get_lazy(index)413 } else {414 self.b.get_lazy(index - self.split)415 }416 }417418 fn len(&self) -> usize {419 self.len420 }421422 fn get_cheap(&self, index: usize) -> Option<Val> {423 if self.split > index {424 self.a.get_cheap(index)425 } else {426 self.b.get_cheap(index - self.split)427 }428 }429430 #[cfg(feature = "nightly")]431 fn iter(&self) -> ExtendedArrayIter<'_> {432 WithExactSize(self.a.iter().chain(self.b.iter()), self.len)433 }434 #[cfg(feature = "nightly")]435 fn iter_lazy(&self) -> ExtendedArrayLazyIter<'_> {436 WithExactSize(self.a.iter_lazy().chain(self.b.iter_lazy()), self.len)437 }438 #[cfg(feature = "nightly")]439 fn iter_cheap(&self) -> Option<ExtendedArrayCheapIter<'_>> {440 let a = self.a.iter_cheap()?;441 let b = self.b.iter_cheap()?;442 Some(WithExactSize(a.chain(b), self.len))443 }444}445impl From<ExtendedArray> for ArrValue {446 fn from(value: ExtendedArray) -> Self {447 Self::Extended(Cc::new(value))448 }449}450451#[derive(Trace, Debug, Clone)]452pub struct LazyArray(pub Cc<Vec<Thunk<Val>>>);453#[cfg(feature = "nightly")]454type LazyArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;455#[cfg(feature = "nightly")]456type LazyArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;457#[cfg(feature = "nightly")]458type LazyArrayCheapIter<'t> = iter::Empty<Val>;459impl ArrayLike for LazyArray {460 #[cfg(feature = "nightly")]461 type Iter<'t> = LazyArrayIter<'t>;462463 #[cfg(feature = "nightly")]464 type IterLazy<'t> = LazyArrayLazyIter<'t>;465466 #[cfg(feature = "nightly")]467 type IterCheap<'t> = LazyArrayCheapIter<'t>;468469 fn len(&self) -> usize {470 self.0.len()471 }472 fn get(&self, index: usize) -> Result<Option<Val>> {473 let Some(v) = self.0.get(index) else {474 return Ok(None);475 };476 v.evaluate().map(Some)477 }478 fn get_cheap(&self, _index: usize) -> Option<Val> {479 None480 }481 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {482 self.0.get(index).cloned()483 }484 #[cfg(feature = "nightly")]485 fn iter(&self) -> LazyArrayIter<'_> {486 self.0.iter().map(Thunk::evaluate)487 }488 #[cfg(feature = "nightly")]489 fn iter_lazy(&self) -> LazyArrayLazyIter<'_> {490 self.0.iter().cloned()491 }492 #[cfg(feature = "nightly")]493 fn iter_cheap(&self) -> Option<LazyArrayCheapIter<'_>> {494 None495 }496}497impl From<LazyArray> for ArrValue {498 fn from(value: LazyArray) -> Self {499 Self::Lazy(value)500 }501}502503#[derive(Trace, Debug, Clone)]504pub struct EagerArray(pub Cc<Vec<Val>>);505#[cfg(feature = "nightly")]506type EagerArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;507#[cfg(feature = "nightly")]508type EagerArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;509#[cfg(feature = "nightly")]510type EagerArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;511impl ArrayLike for EagerArray {512 #[cfg(feature = "nightly")]513 type Iter<'t> = EagerArrayIter<'t>;514515 #[cfg(feature = "nightly")]516 type IterLazy<'t> = EagerArrayLazyIter<'t>;517518 #[cfg(feature = "nightly")]519 type IterCheap<'t> = EagerArrayCheapIter<'t>;520521 fn len(&self) -> usize {522 self.0.len()523 }524525 fn get(&self, index: usize) -> Result<Option<Val>> {526 Ok(self.0.get(index).cloned())527 }528529 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {530 self.0.get(index).cloned().map(Thunk::evaluated)531 }532533 fn get_cheap(&self, index: usize) -> Option<Val> {534 self.0.get(index).cloned()535 }536537 #[cfg(feature = "nightly")]538 fn iter(&self) -> EagerArrayIter<'_> {539 self.0.iter().cloned().map(Ok)540 }541542 #[cfg(feature = "nightly")]543 fn iter_lazy(&self) -> EagerArrayLazyIter<'_> {544 self.0.iter().cloned().map(Thunk::evaluated)545 }546547 #[cfg(feature = "nightly")]548 fn iter_cheap(&self) -> Option<EagerArrayCheapIter<'_>> {549 Some(self.0.iter().cloned())550 }551}552impl From<EagerArray> for ArrValue {553 fn from(value: EagerArray) -> Self {554 Self::Eager(value)555 }556}557558/// Inclusive range type559#[derive(Debug, Trace, Clone, PartialEq, Eq)]560pub struct RangeArray {561 start: i32,562 end: i32,563}564impl RangeArray {565 pub fn empty() -> Self {566 Self::new_exclusive(0, 0)567 }568 pub fn new_exclusive(start: i32, end: i32) -> Self {569 end.checked_sub(1)570 .map_or_else(Self::empty, |end| Self { start, end })571 }572 pub fn new_inclusive(start: i32, end: i32) -> Self {573 Self { start, end }574 }575 fn range(&self) -> impl Iterator<Item = i32> + ExactSizeIterator + DoubleEndedIterator {576 WithExactSize(577 self.start..=self.end,578 (self.end as usize)579 .wrapping_sub(self.start as usize)580 .wrapping_add(1),581 )582 }583}584585#[cfg(feature = "nightly")]586type RangeArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;587#[cfg(feature = "nightly")]588type RangeArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;589#[cfg(feature = "nightly")]590type RangeArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;591impl ArrayLike for RangeArray {592 #[cfg(feature = "nightly")]593 type Iter<'t> = RangeArrayIter<'t>;594595 #[cfg(feature = "nightly")]596 type IterLazy<'t> = RangeArrayLazyIter<'t>;597598 #[cfg(feature = "nightly")]599 type IterCheap<'t> = RangeArrayCheapIter<'t>;600601 fn len(&self) -> usize {602 self.range().len()603 }604 fn is_empty(&self) -> bool {605 self.range().len() == 0606 }607608 fn get(&self, index: usize) -> Result<Option<Val>> {609 Ok(self.get_cheap(index))610 }611612 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {613 self.get_cheap(index).map(Thunk::evaluated)614 }615616 fn get_cheap(&self, index: usize) -> Option<Val> {617 self.range().nth(index).map(|i| Val::Num(f64::from(i)))618 }619620 #[cfg(feature = "nightly")]621 fn iter(&self) -> RangeArrayIter<'_> {622 self.range().map(|i| Ok(Val::Num(f64::from(i))))623 }624625 #[cfg(feature = "nightly")]626 fn iter_lazy(&self) -> RangeArrayLazyIter<'_> {627 self.range()628 .map(|i| Thunk::evaluated(Val::Num(f64::from(i))))629 }630631 #[cfg(feature = "nightly")]632 fn iter_cheap(&self) -> Option<RangeArrayCheapIter<'_>> {633 Some(self.range().map(|i| Val::Num(f64::from(i))))634 }635}636impl From<RangeArray> for ArrValue {637 fn from(value: RangeArray) -> Self {638 Self::Range(value)639 }640}641642#[derive(Debug, Trace, Clone)]643pub struct ReverseArray(pub ArrValue);644impl ArrayLike for ReverseArray {645 #[cfg(feature = "nightly")]646 type Iter<'t> = iter::Rev<UnknownArrayIter<'t>>;647648 #[cfg(feature = "nightly")]649 type IterLazy<'t> = iter::Rev<UnknownArrayIterLazy<'t>>;650651 #[cfg(feature = "nightly")]652 type IterCheap<'t> = iter::Rev<UnknownArrayIterCheap<'t>>;653654 fn len(&self) -> usize {655 self.0.len()656 }657658 fn get(&self, index: usize) -> Result<Option<Val>> {659 self.0.get(self.0.len() - index - 1)660 }661662 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {663 self.0.get_lazy(self.0.len() - index - 1)664 }665666 fn get_cheap(&self, index: usize) -> Option<Val> {667 self.0.get_cheap(self.0.len() - index - 1)668 }669670 #[cfg(feature = "nightly")]671 fn iter(&self) -> iter::Rev<UnknownArrayIter<'_>> {672 self.0.iter().rev()673 }674675 #[cfg(feature = "nightly")]676 fn iter_lazy(&self) -> iter::Rev<UnknownArrayIterLazy<'_>> {677 self.0.iter_lazy().rev()678 }679680 #[cfg(feature = "nightly")]681 fn iter_cheap(&self) -> Option<iter::Rev<UnknownArrayIterCheap<'_>>> {682 Some(self.0.iter_cheap()?.rev())683 }684 fn reverse(self) -> ArrValue {685 self.0686 }687}688impl From<ReverseArray> for ArrValue {689 fn from(value: ReverseArray) -> Self {690 Self::Reverse(Cc::new(value))691 }692}693694#[derive(Trace, Debug)]695pub struct MappedArrayInner {696 inner: ArrValue,697 cached: RefCell<Vec<ArrayThunk<()>>>,698 mapper: FuncVal,699}700#[derive(Trace, Debug, Clone)]701pub struct MappedArray(Cc<MappedArrayInner>);702impl MappedArray {703 pub fn new(inner: ArrValue, mapper: FuncVal) -> Self {704 let len = inner.len();705 Self(Cc::new(MappedArrayInner {706 inner,707 cached: RefCell::new(vec![ArrayThunk::Waiting(()); len]),708 mapper,709 }))710 }711}712#[cfg(feature = "nightly")]713type MappedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;714#[cfg(feature = "nightly")]715type MappedArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;716#[cfg(feature = "nightly")]717type MappedArrayCheapIter<'t> = iter::Empty<Val>;718impl ArrayLike for MappedArray {719 #[cfg(feature = "nightly")]720 type Iter<'t> = MappedArrayIter<'t>;721 #[cfg(feature = "nightly")]722 type IterLazy<'t> = MappedArrayLazyIter<'t>;723 #[cfg(feature = "nightly")]724 type IterCheap<'t> = MappedArrayCheapIter<'t>;725726 fn len(&self) -> usize {727 self.0.cached.borrow().len()728 }729730 fn get(&self, index: usize) -> Result<Option<Val>> {731 if index >= self.len() {732 return Ok(None);733 }734 match &self.0.cached.borrow()[index] {735 ArrayThunk::Computed(c) => return Ok(Some(c.clone())),736 ArrayThunk::Errored(e) => return Err(e.clone()),737 ArrayThunk::Pending => return Err(InfiniteRecursionDetected.into()),738 ArrayThunk::Waiting(..) => {}739 };740741 let ArrayThunk::Waiting(_) = replace(&mut self.0.cached.borrow_mut()[index], ArrayThunk::Pending) else {742 unreachable!()743 };744745 let val = self746 .0747 .inner748 .get(index)749 .transpose()750 .expect("index checked")751 .and_then(|r| self.0.mapper.evaluate_simple(&(Any(r),)));752753 let new_value = match val {754 Ok(v) => v,755 Err(e) => {756 self.0.cached.borrow_mut()[index] = ArrayThunk::Errored(e.clone());757 return Err(e);758 }759 };760 self.0.cached.borrow_mut()[index] = ArrayThunk::Computed(new_value.clone());761 Ok(Some(new_value))762 }763 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {764 #[derive(Trace)]765 struct ArrayElement {766 arr_thunk: MappedArray,767 index: usize,768 }769770 impl ThunkValue for ArrayElement {771 type Output = Val;772773 fn get(self: Box<Self>) -> Result<Self::Output> {774 self.arr_thunk775 .get(self.index)776 .transpose()777 .expect("index checked")778 }779 }780781 if index >= self.len() {782 return None;783 }784 match &self.0.cached.borrow()[index] {785 ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())),786 ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())),787 ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}788 };789790 Some(Thunk::new(tb!(ArrayElement {791 arr_thunk: self.clone(),792 index,793 })))794 }795796 fn get_cheap(&self, _index: usize) -> Option<Val> {797 None798 }799800 #[cfg(feature = "nightly")]801 fn iter(&self) -> MappedArrayIter<'_> {802 (0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))803 }804805 #[cfg(feature = "nightly")]806 fn iter_lazy(&self) -> MappedArrayLazyIter<'_> {807 (0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))808 }809810 #[cfg(feature = "nightly")]811 fn iter_cheap(&self) -> Option<Self::IterCheap<'_>> {812 None813 }814}815impl From<MappedArray> for ArrValue {816 fn from(value: MappedArray) -> Self {817 Self::Mapped(value)818 }819}820821#[derive(Trace, Debug)]822pub struct RepeatedArrayInner {823 data: ArrValue,824 repeats: usize,825 total_len: usize,826}827#[derive(Trace, Debug, Clone)]828pub struct RepeatedArray(Cc<RepeatedArrayInner>);829impl RepeatedArray {830 pub fn new(data: ArrValue, repeats: usize) -> Option<Self> {831 let total_len = data.len().checked_mul(repeats)?;832 Some(Self(Cc::new(RepeatedArrayInner {833 data,834 repeats,835 total_len,836 })))837 }838 pub fn is_cheap(&self) -> bool {839 self.0.data.is_cheap()840 }841}842843#[cfg(feature = "nightly")]844type RepeatedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;845#[cfg(feature = "nightly")]846type RepeatedArrayLazyIter<'t> =847 impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;848#[cfg(feature = "nightly")]849type RepeatedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;850impl ArrayLike for RepeatedArray {851 #[cfg(feature = "nightly")]852 type Iter<'t> = RepeatedArrayIter<'t>;853 #[cfg(feature = "nightly")]854 type IterLazy<'t> = RepeatedArrayLazyIter<'t>;855 #[cfg(feature = "nightly")]856 type IterCheap<'t> = RepeatedArrayCheapIter<'t>;857858 fn len(&self) -> usize {859 self.0.total_len860 }861862 fn get(&self, index: usize) -> Result<Option<Val>> {863 if index > self.0.total_len {864 return Ok(None);865 }866 self.0.data.get(index % self.0.data.len())867 }868869 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {870 if index > self.0.total_len {871 return None;872 }873 self.0.data.get_lazy(index % self.0.data.len())874 }875876 fn get_cheap(&self, index: usize) -> Option<Val> {877 if index > self.0.total_len {878 return None;879 }880 self.0.data.get_cheap(index % self.0.data.len())881 }882883 #[cfg(feature = "nightly")]884 fn iter(&self) -> RepeatedArrayIter<'_> {885 (0..self.0.total_len)886 .map(|i| self.get(i))887 .map(Result::transpose)888 .map(Option::unwrap)889 }890891 #[cfg(feature = "nightly")]892 fn iter_lazy(&self) -> RepeatedArrayLazyIter<'_> {893 (0..self.0.total_len)894 .map(|i| self.get_lazy(i))895 .map(Option::unwrap)896 }897898 #[cfg(feature = "nightly")]899 fn iter_cheap(&self) -> Option<RepeatedArrayCheapIter<'_>> {900 if !self.0.data.is_cheap() {901 return None;902 }903 Some(904 (0..self.0.total_len)905 .map(|i| self.get_cheap(i))906 .map(Option::unwrap),907 )908 }909}910impl From<RepeatedArray> for ArrValue {911 fn from(value: RepeatedArray) -> Self {912 Self::Repeated(value)913 }914}915916#[cfg(feature = "nightly")]917macro_rules! impl_iter_enum {918 ($n:ident => $v:ident) => {919 pub enum $n<'t> {920 Bytes(<BytesArray as ArrayLike>::$v<'t>),921 Expr(<ExprArray as ArrayLike>::$v<'t>),922 Lazy(<LazyArray as ArrayLike>::$v<'t>),923 Eager(<EagerArray as ArrayLike>::$v<'t>),924 Range(<RangeArray as ArrayLike>::$v<'t>),925 Slice(Box<<SliceArray as ArrayLike>::$v<'t>>),926 Extended(Box<<ExtendedArray as ArrayLike>::$v<'t>>),927 Reverse(Box<<ReverseArray as ArrayLike>::$v<'t>>),928 Mapped(Box<<MappedArray as ArrayLike>::$v<'t>>),929 Repeated(Box<<RepeatedArray as ArrayLike>::$v<'t>>),930 }931 };932}933934macro_rules! pass {935 ($t:ident.$m:ident($($ident:ident),*)) => {936 match $t {937 Self::Bytes(e) => e.$m($($ident)*),938 Self::Expr(e) => e.$m($($ident)*),939 Self::Lazy(e) => e.$m($($ident)*),940 Self::Eager(e) => e.$m($($ident)*),941 Self::Range(e) => e.$m($($ident)*),942 Self::Slice(e) => e.$m($($ident)*),943 Self::Extended(e) => e.$m($($ident)*),944 Self::Reverse(e) => e.$m($($ident)*),945 Self::Mapped(e) => e.$m($($ident)*),946 Self::Repeated(e) => e.$m($($ident)*),947 }948 };949}950pub(super) use pass;951952#[cfg(feature = "nightly")]953macro_rules! pass_iter_call {954 ($t:ident.$c:ident $(in $wrap:ident)? => $e:ident) => {955 match $t {956 ArrValue::Bytes(e) => $e::Bytes($($wrap!)?(e.$c())),957 ArrValue::Lazy(e) => $e::Lazy($($wrap!)?(e.$c())),958 ArrValue::Expr(e) => $e::Expr($($wrap!)?(e.$c())),959 ArrValue::Eager(e) => $e::Eager($($wrap!)?(e.$c())),960 ArrValue::Range(e) => $e::Range($($wrap!)?(e.$c())),961 ArrValue::Slice(e) => $e::Slice(Box::new($($wrap!)?(e.$c()))),962 ArrValue::Extended(e) => $e::Extended(Box::new($($wrap!)?(e.$c()))),963 ArrValue::Reverse(e) => $e::Reverse(Box::new($($wrap!)?(e.$c()))),964 ArrValue::Mapped(e) => $e::Mapped(Box::new($($wrap!)?(e.$c()))),965 ArrValue::Repeated(e) => $e::Repeated(Box::new($($wrap!)?(e.$c()))),966 }967 };968}969#[cfg(feature = "nightly")]970pub(super) use pass_iter_call;971972#[cfg(feature = "nightly")]973macro_rules! impl_iter {974 ($t:ident => $out:ty) => {975 impl Iterator for $t<'_> {976 type Item = $out;977978 fn next(&mut self) -> Option<Self::Item> {979 pass!(self.next())980 }981 fn nth(&mut self, count: usize) -> Option<Self::Item> {982 pass!(self.nth(count))983 }984 fn size_hint(&self) -> (usize, Option<usize>) {985 pass!(self.size_hint())986 }987 }988 impl DoubleEndedIterator for $t<'_> {989 fn next_back(&mut self) -> Option<Self::Item> {990 pass!(self.next_back())991 }992 fn nth_back(&mut self, count: usize) -> Option<Self::Item> {993 pass!(self.nth_back(count))994 }995 }996 impl ExactSizeIterator for $t<'_> {997 fn len(&self) -> usize {998 match self {999 Self::Bytes(e) => e.len(),1000 Self::Expr(e) => e.len(),1001 Self::Lazy(e) => e.len(),1002 Self::Eager(e) => e.len(),1003 Self::Range(e) => e.len(),1004 Self::Slice(e) => e.len(),1005 Self::Extended(e) => {1006 e.size_hint().1.expect("overflow is checked in constructor")1007 }1008 Self::Reverse(e) => e.len(),1009 Self::Mapped(e) => e.len(),1010 Self::Repeated(e) => e.len(),1011 }1012 }1013 }1014 };1015}10161017#[cfg(feature = "nightly")]1018impl_iter_enum!(UnknownArrayIter => Iter);1019#[cfg(feature = "nightly")]1020impl_iter!(UnknownArrayIter => Result<Val>);1021#[cfg(feature = "nightly")]1022impl_iter_enum!(UnknownArrayIterLazy => IterLazy);1023#[cfg(feature = "nightly")]1024impl_iter!(UnknownArrayIterLazy => Thunk<Val>);1025#[cfg(feature = "nightly")]1026impl_iter_enum!(UnknownArrayIterCheap => IterCheap);1027#[cfg(feature = "nightly")]1028impl_iter!(UnknownArrayIterCheap => Val);crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -414,7 +414,7 @@
#[allow(clippy::too_many_lines)]
pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {
use Expr::*;
- if let Some(trivial) = evaluate_trivial(&expr) {
+ if let Some(trivial) = evaluate_trivial(expr) {
return Ok(trivial);
}
let LocExpr(expr, loc) = expr;
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -96,16 +96,37 @@
(Str(a), Str(b)) => a.cmp(b),
(Num(a), Num(b)) => a.partial_cmp(b).expect("jsonnet numbers are non NaN"),
(Arr(a), Arr(b)) => {
- let ai = a.iter();
- let bi = b.iter();
+ if let (Some(ai), Some(bi)) = (a.iter_cheap(), b.iter_cheap()) {
+ for (a, b) in ai.zip(bi) {
+ let ord = evaluate_compare_op(&a, &b, op)?;
+ if !ord.is_eq() {
+ return Ok(ord);
+ }
+ }
+ } else {
+ {
+ let ai = a.iter();
+ let bi = b.iter();
- for (a, b) in ai.zip(bi) {
- let ord = evaluate_compare_op(&a?, &b?, op)?;
- if !ord.is_eq() {
- return Ok(ord);
+ for (a, b) in ai.zip(bi) {
+ let ord = evaluate_compare_op(&a?, &b?, op)?;
+ if !ord.is_eq() {
+ return Ok(ord);
+ }
+ }
}
- }
+ // {
+ // let ai = a.iter_expl();
+ // let bi = b.iter_expl();
+ // for (a, b) in ai.zip(bi) {
+ // let ord = evaluate_compare_op(&a?, &b?, op)?;
+ // if !ord.is_eq() {
+ // return Ok(ord);
+ // }
+ // }
+ // }
+ }
a.len().cmp(&b.len())
}
(_, _) => throw!(BinaryOperatorDoesNotOperateOnValues(
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -1,6 +1,5 @@
//! jsonnet interpreter implementation
-#![cfg_attr(feature = "nightly", feature(thread_local))]
-#![feature(type_alias_impl_trait)]
+#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(
clippy::all,
crates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -13,9 +13,9 @@
|| format!("std.format of {str}"),
|| {
Ok(match vals {
- Val::Arr(vals) => format_arr(&str, &vals.evaluatedcc()?)?,
- Val::Obj(obj) => format_obj(&str, &obj)?,
- o => format_arr(&str, &[o])?,
+ Val::Arr(vals) => format_arr(str, &vals.iter().collect::<Result<Vec<_>>>()?)?,
+ Val::Obj(obj) => format_obj(str, &obj)?,
+ o => format_arr(str, &[o])?,
})
},
)
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -278,19 +278,19 @@
}
/// Specialization, provides faster `TryFrom<VecVal>` for Val
-pub struct VecVal(pub Cc<Vec<Val>>);
+pub struct VecVal(pub Vec<Val>);
impl Typed for VecVal {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);
fn into_untyped(value: Self) -> Result<Val> {
- Ok(Val::Arr(ArrValue::eager(value.0)))
+ Ok(Val::Arr(ArrValue::eager(Cc::new(value.0))))
}
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
- Val::Arr(a) => Ok(Self(a.evaluatedcc()?)),
+ Val::Arr(a) => Ok(Self(a.iter().collect::<Result<Vec<_>>>()?)),
_ => unreachable!(),
}
}
crates/jrsonnet-parser/src/source.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/source.rs
+++ b/crates/jrsonnet-parser/src/source.rs
@@ -33,8 +33,8 @@
}
fn dyn_eq(&self, other: &dyn $T) -> bool {
let Some(other) = other.as_any().downcast_ref::<Self>() else {
- return false
- };
+ return false
+ };
let this = <Self as $T>::as_any(self)
.downcast_ref::<Self>()
.expect("restricted by impl");
crates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/objects.rs
+++ b/crates/jrsonnet-stdlib/src/objects.rs
@@ -5,7 +5,6 @@
val::{StrValue, Val},
IStr, ObjValue,
};
-use jrsonnet_gcmodule::Cc;
#[builtin]
pub fn builtin_object_fields_ex(
@@ -20,12 +19,12 @@
#[cfg(feature = "exp-preserve-order")]
preserve_order,
);
- Ok(VecVal(Cc::new(
+ Ok(VecVal(
out.into_iter()
.map(StrValue::Flat)
.map(Val::Str)
.collect::<Vec<_>>(),
- )))
+ ))
}
#[builtin]
crates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/sort.rs
+++ b/crates/jrsonnet-stdlib/src/sort.rs
@@ -50,13 +50,12 @@
}
/// * `key_getter` - None, if identity sort required
-pub fn sort(ctx: Context, values: Cc<Vec<Val>>, key_getter: FuncVal) -> Result<Cc<Vec<Val>>> {
+pub fn sort(ctx: Context, mut values: Vec<Val>, key_getter: FuncVal) -> Result<Vec<Val>> {
if values.len() <= 1 {
return Ok(values);
}
if key_getter.is_identity() {
// Fast path, identity key getter
- let mut values = (*values).clone();
let sort_type = get_sort_type(&mut values, |k| k)?;
match sort_type {
SortKeyType::Number => values.sort_unstable_by_key(|v| match v {
@@ -69,7 +68,7 @@
}),
SortKeyType::Unknown => unreachable!(),
};
- Ok(Cc::new(values))
+ Ok(values)
} else {
// Slow path, user provided key getter
let mut vk = Vec::with_capacity(values.len());
@@ -96,7 +95,7 @@
}),
SortKeyType::Unknown => unreachable!(),
};
- Ok(Cc::new(vk.into_iter().map(|v| v.0).collect()))
+ Ok(vk.into_iter().map(|v| v.0).collect())
}
}
@@ -106,9 +105,9 @@
if arr.len() <= 1 {
return Ok(arr);
}
- Ok(ArrValue::eager(super::sort::sort(
+ Ok(ArrValue::eager(Cc::new(super::sort::sort(
ctx,
- arr.evaluatedcc()?,
+ arr.iter().collect::<Result<Vec<_>>>()?,
keyF.unwrap_or_else(FuncVal::identity),
- )?))
+ )?)))
}
crates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -6,7 +6,6 @@
val::{ArrValue, StrValue},
Either, IStr, Val,
};
-use jrsonnet_gcmodule::Cc;
#[builtin]
pub const fn builtin_codepoint(str: char) -> Result<u32> {
@@ -31,7 +30,7 @@
#[builtin]
pub fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {
use Either2::*;
- Ok(VecVal(Cc::new(match maxsplits {
+ Ok(VecVal(match maxsplits {
A(n) => str
.splitn(n + 1, &c as &str)
.map(|s| Val::Str(StrValue::Flat(s.into())))
@@ -40,7 +39,7 @@
.split(&c as &str)
.map(|s| Val::Str(StrValue::Flat(s.into())))
.collect(),
- })))
+ }))
}
#[builtin]
flake.lockdiffbeforeafterboth--- a/flake.lock
+++ b/flake.lock
@@ -15,28 +15,13 @@
"type": "github"
}
},
- "flake-utils_2": {
- "locked": {
- "lastModified": 1659877975,
- "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
- "owner": "numtide",
- "repo": "flake-utils",
- "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
- "type": "github"
- },
- "original": {
- "owner": "numtide",
- "repo": "flake-utils",
- "type": "github"
- }
- },
"nixpkgs": {
"locked": {
- "lastModified": 1668090223,
- "narHash": "sha256-Bynlfyf/LsQJ+CJ//1TGmA7eiCzqk95bz+bxyP39xYY=",
+ "lastModified": 1670089411,
+ "narHash": "sha256-iiW+L7iN8At8s98qb2h1P8Z0BVTZLqY8KHpfZuM7ULQ=",
"owner": "nixos",
"repo": "nixpkgs",
- "rev": "1f6b98281191b50ba987cabd5bf3068870c26789",
+ "rev": "ffa4eb958a435e9833bda0fdfc834e87232aa879",
"type": "github"
},
"original": {
@@ -45,22 +30,6 @@
"type": "github"
}
},
- "nixpkgs_2": {
- "locked": {
- "lastModified": 1665296151,
- "narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=",
- "owner": "NixOS",
- "repo": "nixpkgs",
- "rev": "14ccaaedd95a488dd7ae142757884d8e125b3363",
- "type": "github"
- },
- "original": {
- "owner": "NixOS",
- "ref": "nixpkgs-unstable",
- "repo": "nixpkgs",
- "type": "github"
- }
- },
"root": {
"inputs": {
"flake-utils": "flake-utils",
@@ -70,15 +39,19 @@
},
"rust-overlay": {
"inputs": {
- "flake-utils": "flake-utils_2",
- "nixpkgs": "nixpkgs_2"
+ "flake-utils": [
+ "flake-utils"
+ ],
+ "nixpkgs": [
+ "nixpkgs"
+ ]
},
"locked": {
- "lastModified": 1668048396,
- "narHash": "sha256-SUWQlSa/H5XKPeuF9XmWzmwIJrgK42Lak6/1jBAwyd0=",
+ "lastModified": 1670034122,
+ "narHash": "sha256-EqmuOKucPWtMvCZtHraHr3Q3bgVszq1x2PoZtQkUuEk=",
"owner": "oxalica",
"repo": "rust-overlay",
- "rev": "859fefb532bb957f51a9b5e8e3ba2e48394c9353",
+ "rev": "a0d5773275ecd4f141d792d3a0376277c0fc0b65",
"type": "github"
},
"original": {
flake.nixdiffbeforeafterboth--- a/flake.nix
+++ b/flake.nix
@@ -3,7 +3,11 @@
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
- rust-overlay.url = "github:oxalica/rust-overlay";
+ rust-overlay = {
+ url = "github:oxalica/rust-overlay";
+ inputs.nixpkgs.follows = "nixpkgs";
+ inputs.flake-utils.follows = "flake-utils";
+ };
};
outputs = { nixpkgs, flake-utils, rust-overlay, ... }:
flake-utils.lib.eachDefaultSystem (system:
@@ -12,7 +16,7 @@
inherit system;
overlays = [ rust-overlay.overlays.default ];
};
- rust = ((pkgs.rustChannelOf { date = "2022-11-10"; channel = "nightly"; }).default.override {
+ rust = ((pkgs.rustChannelOf { date = "2022-11-19"; channel = "nightly"; }).default.override {
extensions = [ "rust-src" "miri" ];
});
in
@@ -29,6 +33,13 @@
cargo = rust;
};
};
+ jrsonnet-nightly = pkgs.callPackage ./nix/jrsonnet.nix {
+ rustPlatform = pkgs.makeRustPlatform {
+ rustc = rust;
+ cargo = rust;
+ };
+ withNightlyFeatures = true;
+ };
jrsonnet-release = pkgs.callPackage ./nix/jrsonnet-release.nix {
rustPlatform = pkgs.makeRustPlatform {
rustc = rust;
@@ -37,29 +48,48 @@
};
benchmarks = pkgs.callPackage ./nix/benchmarks.nix {
- inherit go-jsonnet sjsonnet jsonnet jrsonnet jrsonnet-release;
+ inherit go-jsonnet sjsonnet jsonnet;
+ jrsonnetVariants = [
+ { drv = jrsonnet; name = "current"; }
+ { drv = jrsonnet-nightly; name = "current-nightly"; }
+ ];
};
benchmarks-quick = pkgs.callPackage ./nix/benchmarks.nix {
- inherit go-jsonnet sjsonnet jsonnet jrsonnet jrsonnet-release;
+ inherit go-jsonnet sjsonnet jsonnet;
quick = true;
+ jrsonnetVariants = [
+ { drv = jrsonnet; name = "current"; }
+ { drv = jrsonnet-nightly; name = "current-nightly"; }
+ ];
};
benchmarks-against-release = pkgs.callPackage ./nix/benchmarks.nix {
- inherit go-jsonnet sjsonnet jsonnet jrsonnet jrsonnet-release;
- againstRelease = true;
+ inherit go-jsonnet sjsonnet jsonnet;
+ jrsonnetVariants = [
+ { drv = jrsonnet; name = "current"; }
+ { drv = jrsonnet-nightly; name = "current-nightly"; }
+ { drv = jrsonnet-release; name = "before-str-extend"; }
+ ];
};
benchmarks-quick-against-release = pkgs.callPackage ./nix/benchmarks.nix {
- inherit go-jsonnet sjsonnet jsonnet jrsonnet jrsonnet-release;
+ inherit go-jsonnet sjsonnet jsonnet;
quick = true;
- againstRelease = true;
+ jrsonnetVariants = [
+ { drv = jrsonnet; name = "current"; }
+ { drv = jrsonnet-nightly; name = "current-nightly"; }
+ { drv = jrsonnet-release; name = "before-str-extend"; }
+ ];
};
};
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs;[
rust
cargo-edit
+ cargo-asm
lld
hyperfine
valgrind
+ kcachegrind
+ graphviz
];
};
}
nix/benchmarks.nixdiffbeforeafterboth--- a/nix/benchmarks.nix
+++ b/nix/benchmarks.nix
@@ -4,15 +4,16 @@
, cacert
, stdenv
, fetchFromGitHub
-, jrsonnet
-, jrsonnet-release
, go-jsonnet
, sjsonnet
, jsonnet
, hyperfine
, quick ? false
-, againstRelease ? false
+, jrsonnetVariants
}:
+
+with lib;
+
let
jsonnetBench = fetchFromGitHub {
rev = "v0.19.1";
@@ -65,13 +66,12 @@
unpackPhase = "true";
buildInputs = [
- jrsonnet
go-jsonnet
sjsonnet
jsonnet
hyperfine
- ] ++ (if againstRelease then [ jrsonnet-release ] else [ ]);
+ ];
installPhase =
let
@@ -81,47 +81,48 @@
echo >> $out
echo "### ${name}" >> $out
echo >> $out
- ${if skipGo != "" then ''
+ ${optionalString (skipGo != "") ''
echo "> Note: No results for Go, ${skipGo}" >> $out
echo >> $out
- '' else ""}
- ${if skipScala != "" then ''
+ ''}
+ ${optionalString (skipScala != "") ''
echo "> Note: No results for Scala, ${skipScala}" >> $out
echo >> $out
- '' else ""}
- ${if skipCpp != "" then ''
+ ''}
+ ${optionalString (skipCpp != "") ''
echo "> Note: No results for C++, ${skipCpp}" >> $out
echo >> $out
- '' else ""}
- ${if !quick then ''
+ ''}
+ ${optionalString (!quick && !omitSource) ''
echo "<details>" >> $out
echo "<summary>Source</summary>" >> $out
echo >> $out
echo "\`\`\`jsonnet" >> $out
- ${if pathIsGenerator then "echo \"// Generator source\" >> $out" else ""}
- ${if omitSource then "echo \"// Omitted: too large\" >> $out" else "cat ${path} >> $out"}
+ ${optionalString pathIsGenerator "echo \"// Generator source\" >> $out"}
+ cat ${path} >> $out
echo >> $out
echo "\`\`\`" >> $out
echo "</details>" >> $out
echo >> $out
- '' else ""}
+ ''}
path=${path}
- ${if pathIsGenerator then ''
- jrsonnet $path > generated.jsonnet
+ ${optionalString pathIsGenerator ''
+ go-jsonnet $path > generated.jsonnet
path=generated.jsonnet
- '' else ""}
- hyperfine -N -w4 --output=pipe --style=basic --export-markdown result.md \
- "jrsonnet $path ${if vendor != "" then "-J${vendor}" else ""}" -n "Rust" \
- ${if againstRelease then "\"jrsonnet-release $path ${if vendor != "" then "-J${vendor}" else ""}\" -n \"Rust (released)\"" else "" } \
- ${if skipGo == "" then "\"go-jsonnet $path ${if vendor != "" then "-J ${vendor}" else ""}\" -n \"Go\"" else "" } \
- ${if skipScala == "" then "\"sjsonnet $path ${if vendor != "" then "-J ${vendor}" else ""}\" -n \"Scala\"" else "" } \
- ${if skipCpp == "" then "\"jsonnet $path ${if vendor != "" then "-J ${vendor}" else ""}\" -n \"C++\"" else "" }
+ ''}
+ hyperfine -N -w4 -m20 --output=pipe --style=basic --export-markdown result.md \
+ ${concatStringsSep " " (forEach jrsonnetVariants (variant:
+ "\"${variant.drv}/bin/jrsonnet $path ${optionalString (vendor != "") "-J${vendor}"}\" -n \"Rust (${variant.name})\""
+ ))} \
+ ${optionalString (skipGo == "") "\"go-jsonnet $path ${optionalString (vendor != "") "-J ${vendor}"}\" -n \"Go\""} \
+ ${optionalString (skipScala == "") "\"sjsonnet $path ${optionalString (vendor != "") "-J ${vendor}"}\" -n \"Scala\""} \
+ ${optionalString (skipCpp == "") "\"jsonnet $path ${optionalString (vendor != "") "-J ${vendor}"}\" -n \"C++\""}
cat result.md >> $out
'';
in
''
touch $out
- ${if !quick then ''
+ ${optionalString (!quick) ''
cat ${./benchmarks.md} >> $out
echo >> $out
@@ -156,43 +157,43 @@
echo >> $out
echo >> $out
- '' else ""}
+ ''}
echo "## Real world" >> $out
- ${mkBench {name = "Graalvm CI"; path = "${graalvmBench}/ci.jsonnet"; skipCpp = "takes longer than a hour";}}
- ${mkBench {name = "Kube-prometheus manifests"; vendor = "${kubePrometheusBench}/vendor"; path = "${kubePrometheusBench}/example.jsonnet"; skipCpp = skipSlow;}}
+ ${mkBench {name = "Graalvm CI"; path = "${graalvmBench}/ci.jsonnet"; skipCpp = "takes longer than a hour"; skipGo = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "Kube-prometheus manifests"; vendor = "${kubePrometheusBench}/vendor"; path = "${kubePrometheusBench}/example.jsonnet"; skipCpp = skipSlow; skipGo = skipSlow; skipScala = skipSlow;}}
echo >> $out
echo "## Benchmarks from C++ jsonnet (/perf_tests)" >> $out
- ${mkBench {name = "Large string join"; path = "${jsonnetBench}/perf_tests/large_string_join.jsonnet";}}
- ${mkBench {name = "Large string template"; omitSource = true; path = "${jsonnetBench}/perf_tests/large_string_template.jsonnet"; skipGo = "fails with os stack size exhausion"; skipCpp = skipSlow;}}
- ${mkBench {name = "Realistic 1"; path = "${jsonnetBench}/perf_tests/realistic1.jsonnet"; skipGo = skipSlow; skipCpp = skipSlow;}}
- ${mkBench {name = "Realistic 2"; path = "${jsonnetBench}/perf_tests/realistic2.jsonnet"; skipGo = skipSlow; skipCpp = skipSlow;}}
+ ${mkBench {name = "Large string join"; path = "${jsonnetBench}/perf_tests/large_string_join.jsonnet"; skipScala = skipSlow;}}
+ ${mkBench {name = "Large string template"; omitSource = true; path = "${jsonnetBench}/perf_tests/large_string_template.jsonnet"; skipGo = "fails with os stack size exhausion"; skipCpp = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "Realistic 1"; path = "${jsonnetBench}/perf_tests/realistic1.jsonnet"; skipGo = skipSlow; skipCpp = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "Realistic 2"; path = "${jsonnetBench}/perf_tests/realistic2.jsonnet"; skipGo = skipSlow; skipCpp = skipSlow; skipScala = skipSlow;}}
echo >> $out
echo "## Benchmarks from C++ jsonnet (/benchmarks)" >> $out
- ${mkBench {name = "Tail call"; path = "${jsonnetBench}/benchmarks/bench.01.jsonnet";}}
- ${mkBench {name = "Inheritance recursion"; path = "${jsonnetBench}/benchmarks/bench.02.jsonnet"; skipCpp = skipSlow;}}
- ${mkBench {name = "Simple recursive call"; path = "${jsonnetBench}/benchmarks/bench.03.jsonnet";}}
- ${mkBench {name = "Foldl string concat"; path = "${jsonnetBench}/benchmarks/bench.04.jsonnet";}}
+ ${mkBench {name = "Tail call"; path = "${jsonnetBench}/benchmarks/bench.01.jsonnet"; skipScala = skipSlow;}}
+ ${mkBench {name = "Inheritance recursion"; path = "${jsonnetBench}/benchmarks/bench.02.jsonnet"; skipCpp = skipSlow; skipGo = skipSlow;}}
+ ${mkBench {name = "Simple recursive call"; path = "${jsonnetBench}/benchmarks/bench.03.jsonnet"; skipScala = skipSlow; skipGo = skipSlow;}}
+ ${mkBench {name = "Foldl string concat"; path = "${jsonnetBench}/benchmarks/bench.04.jsonnet"; skipCpp = skipSlow; skipScala = skipSlow;}}
${mkBench {name = "Array sorts"; path = "${jsonnetBench}/benchmarks/bench.06.jsonnet"; skipScala = "std.reverse is not implemented"; skipCpp = skipSlow;}}
- ${mkBench {name = "Lazy array"; path = "${jsonnetBench}/benchmarks/bench.07.jsonnet";}}
- ${mkBench {name = "Inheritance function recursion"; path = "${jsonnetBench}/benchmarks/bench.08.jsonnet";}}
- ${mkBench {name = "String strips"; path = "${jsonnetBench}/benchmarks/bench.09.jsonnet"; skipCpp = skipSlow;}}
- ${mkBench {name = "Big object"; path = "${jsonnetBench}/benchmarks/gen_big_object.jsonnet"; pathIsGenerator = true;}}
+ ${mkBench {name = "Lazy array"; path = "${jsonnetBench}/benchmarks/bench.07.jsonnet"; skipGo = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "Inheritance function recursion"; path = "${jsonnetBench}/benchmarks/bench.08.jsonnet"; skipCpp = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "String strips"; path = "${jsonnetBench}/benchmarks/bench.09.jsonnet"; skipCpp = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "Big object"; path = "${jsonnetBench}/benchmarks/gen_big_object.jsonnet"; pathIsGenerator = true; skipScala = skipSlow;}}
echo >> $out
echo "## Benchmarks from Go jsonnet (builtins)" >> $out
- ${mkBench {name = "std.base64"; path = "${goJsonnetBench}/base64.jsonnet"; skipCpp = skipSlow;}}
- ${mkBench {name = "std.base64Decode"; path = "${goJsonnetBench}/base64Decode.jsonnet"; skipCpp = skipSlow;}}
- ${mkBench {name = "std.base64DecodeBytes"; path = "${goJsonnetBench}/base64DecodeBytes.jsonnet"; skipCpp = skipSlow;}}
- ${mkBench {name = "std.base64 (byte array)"; path = "${goJsonnetBench}/base64_byte_array.jsonnet"; skipCpp = skipSlow;}}
- ${mkBench {name = "std.foldl"; path = "${goJsonnetBench}/foldl.jsonnet";}}
- ${mkBench {name = "std.manifestJsonEx"; path = "${goJsonnetBench}/manifestJsonEx.jsonnet";}}
- ${mkBench {name = "std.manifestTomlEx"; path = "${goJsonnetBench}/manifestTomlEx.jsonnet"; skipScala = "std.manifestTomlEx is not implemented";}}
- ${mkBench {name = "std.parseInt"; path = "${goJsonnetBench}/parseInt.jsonnet";}}
- ${mkBench {name = "std.reverse"; path = "${goJsonnetBench}/reverse.jsonnet"; skipScala = "std.reverse is not implemented";}}
- ${mkBench {name = "std.substr"; path = "${goJsonnetBench}/substr.jsonnet";}}
+ ${mkBench {name = "std.base64"; path = "${goJsonnetBench}/base64.jsonnet"; skipCpp = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "std.base64Decode"; path = "${goJsonnetBench}/base64Decode.jsonnet"; skipCpp = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "std.base64DecodeBytes"; path = "${goJsonnetBench}/base64DecodeBytes.jsonnet"; skipCpp = skipSlow; skipGo = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "std.base64 (byte array)"; path = "${goJsonnetBench}/base64_byte_array.jsonnet"; skipCpp = skipSlow; skipGo = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "std.foldl"; path = "${goJsonnetBench}/foldl.jsonnet"; skipScala = skipSlow;}}
+ ${mkBench {name = "std.manifestJsonEx"; path = "${goJsonnetBench}/manifestJsonEx.jsonnet"; skipScala = skipSlow; skipCpp = skipSlow;}}
+ ${mkBench {name = "std.manifestTomlEx"; path = "${goJsonnetBench}/manifestTomlEx.jsonnet"; skipScala = "std.manifestTomlEx is not implemented"; skipCpp=skipSlow;}}
+ ${mkBench {name = "std.parseInt"; path = "${goJsonnetBench}/parseInt.jsonnet"; skipScala = skipSlow; skipCpp = skipSlow;}}
+ ${mkBench {name = "std.reverse"; path = "${goJsonnetBench}/reverse.jsonnet"; skipScala = "std.reverse is not implemented"; skipCpp = skipSlow; skipGo = skipSlow;}}
+ ${mkBench {name = "std.substr"; path = "${goJsonnetBench}/substr.jsonnet"; skipScala = skipSlow;}}
${mkBench {name = "Comparsion for array"; path = "${goJsonnetBench}/comparison.jsonnet"; skipScala = "array comparsion is not implemented"; skipCpp = skipSlow;}}
- ${mkBench {name = "Comparsion for primitives"; path = "${goJsonnetBench}/comparison2.jsonnet"; skipCpp = "can't run: uses up to 192GB of RAM";}}
+ ${mkBench {name = "Comparsion for primitives"; path = "${goJsonnetBench}/comparison2.jsonnet"; skipCpp = "can't run: uses up to 192GB of RAM"; skipGo = skipSlow; skipScala = skipSlow;}}
'';
}
nix/jrsonnet-release.nixdiffbeforeafterboth--- a/nix/jrsonnet-release.nix
+++ b/nix/jrsonnet-release.nix
@@ -3,15 +3,15 @@
rustPlatform.buildRustPackage rec {
pname = "jrsonnet";
- version = "d32fe45b8ed28fb39b5359a704922922368af1c0";
+ version = "before-str-extend";
src = fetchFromGitHub {
owner = "CertainLach";
repo = pname;
- rev = version;
+ rev = "d32fe45b8ed28fb39b5359a704922922368af1c0";
hash = "sha256-R9Xt36bYS5upVDzt8hEifwmfocXpJbIKwvxkoJNEGVc=";
};
- cargoHash = "sha256-V+KGWeNlUnelofaGzufNPLGDyxazoFrjZ/n391VYYws=";
+ cargoHash = "sha256-j2sUIzvK66jn8ajmMsXXHstw79jCLog93XCQj1qjAN8=";
cargoTestFlags = [ "--package=jrsonnet --features=mimalloc,legacy-this-file" ];
cargoBuildFlags = [ "--package=jrsonnet --features=mimalloc,legacy-this-file" ];
@@ -19,7 +19,6 @@
buildInputs = [ makeWrapper ];
postInstall = ''
- mv $out/bin/jrsonnet $out/bin/jrsonnet-release
- wrapProgram $out/bin/jrsonnet-release --add-flags "--max-stack=200000 --os-stack=200000"
+ wrapProgram $out/bin/jrsonnet --add-flags "--max-stack=200000 --os-stack=200000"
'';
}
nix/jrsonnet.nixdiffbeforeafterboth--- a/nix/jrsonnet.nix
+++ b/nix/jrsonnet.nix
@@ -1,4 +1,12 @@
-{ lib, fetchFromGitHub, rustPlatform, runCommand, makeWrapper }:
+{ lib
+, fetchFromGitHub
+, rustPlatform
+, runCommand
+, makeWrapper
+, withNightlyFeatures ? false
+}:
+
+with lib;
let
filteredSrc = builtins.path {
@@ -18,10 +26,12 @@
rustPlatform.buildRustPackage rec {
inherit src;
pname = "jrsonnet";
- version = "git";
+ version = "current${optionalString withNightlyFeatures "-nightly"}";
- cargoTestFlags = [ "--features=mimalloc,legacy-this-file,nightly" ];
- cargoBuildFlags = [ "--features=mimalloc,legacy-this-file,nightly" ];
+ cargoTestFlags = [
+ "--features=mimalloc,legacy-this-file${optionalString withNightlyFeatures ",nightly"}"
+ ];
+ cargoBuildFlags = cargoTestFlags;
buildInputs = [ makeWrapper ];