difftreelog
perf inline NumValue methods
in: master
3 files changed
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth1use std::cmp::Ordering;23use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};45use crate::{6 arr::ArrValue,7 bail,8 error::ErrorKind::*,9 evaluate,10 stdlib::std_format,11 typed::Typed,12 val::{equals, StrValue},13 Context, Result, Val,14};1516pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {17 use UnaryOpType::*;18 use Val::*;19 Ok(match (op, b) {20 (Plus, Num(n)) => Val::Num(*n),21 (Minus, Num(n)) => Val::try_num(-n.get())?,22 (Not, Bool(v)) => Bool(!v),23 (BitNot, Num(n)) => Val::try_num(!(n.get() as i64) as f64)?,24 (op, o) => bail!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),25 })26}2728pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {29 use Val::*;30 Ok(match (a, b) {31 (Str(v1), Str(v2)) => Str(StrValue::concat(v1.clone(), v2.clone())),3233 (Num(a), Str(b)) => Val::string(format!("{a}{b}")),34 (Str(a), Num(b)) => Val::string(format!("{a}{b}")),3536 (Str(a), o) | (o, Str(a)) if a.is_empty() => Val::string(o.clone().to_string()?),37 (Str(a), o) => Val::string(format!("{a}{}", o.clone().to_string()?)),38 (o, Str(a)) => Val::string(format!("{}{a}", o.clone().to_string()?)),3940 (Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),41 (Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),4243 (Num(v1), Num(v2)) => Val::try_num(v1.get() + v2.get())?,44 #[cfg(feature = "exp-bigint")]45 (BigInt(a), BigInt(b)) => BigInt(Box::new(&**a + &**b)),46 _ => bail!(BinaryOperatorDoesNotOperateOnValues(47 BinaryOpType::Add,48 a.value_type(),49 b.value_type(),50 )),51 })52}5354pub fn evaluate_mod_op(a: &Val, b: &Val) -> Result<Val> {55 use Val::*;56 match (a, b) {57 (Num(a), Num(b)) => {58 if b.get() == 0.0 {59 bail!(DivisionByZero)60 }61 Ok(Val::try_num(a.get() % b.get())?)62 }63 (Str(str), vals) => {64 String::into_untyped(std_format(&str.clone().into_flat(), vals.clone())?)65 }66 (a, b) => bail!(BinaryOperatorDoesNotOperateOnValues(67 BinaryOpType::Mod,68 a.value_type(),69 b.value_type()70 )),71 }72}7374pub fn evaluate_binary_op_special(75 ctx: Context,76 a: &LocExpr,77 op: BinaryOpType,78 b: &LocExpr,79) -> Result<Val> {80 use BinaryOpType::*;81 use Val::*;82 Ok(match (evaluate(ctx.clone(), a)?, op, b) {83 (Bool(true), Or, _o) => Val::Bool(true),84 (Bool(false), And, _o) => Val::Bool(false),85 #[cfg(feature = "exp-null-coaelse")]86 (Null, NullCoaelse, eb) => evaluate(ctx, eb)?,87 #[cfg(feature = "exp-null-coaelse")]88 (a, NullCoaelse, _o) => a,89 (a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(ctx, eb)?)?,90 })91}9293pub fn evaluate_compare_op(a: &Val, b: &Val, op: BinaryOpType) -> Result<Ordering> {94 use Val::*;95 Ok(match (a, b) {96 (Str(a), Str(b)) => a.cmp(b),97 (Num(a), Num(b)) => a.partial_cmp(b).expect("jsonnet numbers are non NaN"),98 #[cfg(feature = "exp-bigint")]99 (BigInt(a), BigInt(b)) => a.cmp(b),100 (Arr(a), Arr(b)) => {101 if let (Some(ai), Some(bi)) = (a.iter_cheap(), b.iter_cheap()) {102 for (a, b) in ai.zip(bi) {103 let ord = evaluate_compare_op(&a, &b, op)?;104 if !ord.is_eq() {105 return Ok(ord);106 }107 }108 } else {109 let ai = a.iter();110 let bi = b.iter();111112 for (a, b) in ai.zip(bi) {113 let ord = evaluate_compare_op(&a?, &b?, op)?;114 if !ord.is_eq() {115 return Ok(ord);116 }117 }118 }119 a.len().cmp(&b.len())120 }121 (_, _) => bail!(BinaryOperatorDoesNotOperateOnValues(122 op,123 a.value_type(),124 b.value_type()125 )),126 })127}128129pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {130 use BinaryOpType::*;131 use Val::*;132 Ok(match (a, op, b) {133 (a, Add, b) => evaluate_add_op(a, b)?,134135 (a, Eq, b) => Bool(equals(a, b)?),136 (a, Neq, b) => Bool(!equals(a, b)?),137138 (a, Lt, b) => Bool(evaluate_compare_op(a, b, Lt)?.is_lt()),139 (a, Gt, b) => Bool(evaluate_compare_op(a, b, Gt)?.is_gt()),140 (a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),141 (a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),142143 (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone().into_flat(), true)),144 (a, Mod, b) => evaluate_mod_op(a, b)?,145146 (Str(v1), Mul, Num(v2)) => Val::string(v1.to_string().repeat(v2.get() as usize)),147148 // Bool X Bool149 (Bool(a), And, Bool(b)) => Bool(*a && *b),150 (Bool(a), Or, Bool(b)) => Bool(*a || *b),151152 // Num X Num153 (Num(v1), Mul, Num(v2)) => Val::try_num(v1.get() * v2.get())?,154 (Num(v1), Div, Num(v2)) => {155 if v2.get() == 0.0 {156 bail!(DivisionByZero)157 }158 Val::try_num(v1.get() / v2.get())?159 }160161 (Num(v1), Sub, Num(v2)) => Val::try_num(v1.get() - v2.get())?,162163 (Num(v1), BitAnd, Num(v2)) => Val::try_num((v1.get() as i64 & v2.get() as i64) as f64)?,164 (Num(v1), BitOr, Num(v2)) => Val::try_num((v1.get() as i64 | v2.get() as i64) as f64)?,165 (Num(v1), BitXor, Num(v2)) => Val::try_num((v1.get() as i64 ^ v2.get() as i64) as f64)?,166 (Num(v1), Lhs, Num(v2)) => {167 if v2.get() < 0.0 {168 bail!("shift by negative exponent")169 }170 let exp = ((v2.get() as i64) & 63) as u32;171 Val::try_num((v1.get() as i64).wrapping_shl(exp) as f64)?172 }173 (Num(v1), Rhs, Num(v2)) => {174 if v2.get() < 0.0 {175 bail!("shift by negative exponent")176 }177 let exp = ((v2.get() as i64) & 63) as u32;178 Val::try_num((v1.get() as i64).wrapping_shr(exp) as f64)?179 }180181 // Bigint X Bigint182 #[cfg(feature = "exp-bigint")]183 (BigInt(a), Mul, BigInt(b)) => BigInt(Box::new(&**a * &**b)),184 #[cfg(feature = "exp-bigint")]185 (BigInt(a), Sub, BigInt(b)) => BigInt(Box::new(&**a - &**b)),186187 _ => bail!(BinaryOperatorDoesNotOperateOnValues(188 op,189 a.value_type(),190 b.value_type(),191 )),192 })193}1use std::cmp::Ordering;23use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};45use crate::{6 arr::ArrValue,7 bail,8 error::ErrorKind::*,9 evaluate,10 stdlib::std_format,11 typed::Typed,12 val::{equals, StrValue},13 Context, Result, Val,14};1516pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {17 use UnaryOpType::*;18 use Val::*;19 Ok(match (op, b) {20 (Plus, Num(n)) => Val::Num(*n),21 (Minus, Num(n)) => Val::try_num(-n.get())?,22 (Not, Bool(v)) => Bool(!v),23 (BitNot, Num(n)) => Val::try_num(!(n.get() as i64) as f64)?,24 (op, o) => bail!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),25 })26}2728pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {29 use Val::*;30 Ok(match (a, b) {31 (Str(v1), Str(v2)) => Str(StrValue::concat(v1.clone(), v2.clone())),3233 (Num(a), Str(b)) => Val::string(format!("{a}{b}")),34 (Str(a), Num(b)) => Val::string(format!("{a}{b}")),3536 (Str(a), o) | (o, Str(a)) if a.is_empty() => Val::string(o.clone().to_string()?),37 (Str(a), o) => Val::string(format!("{a}{}", o.clone().to_string()?)),38 (o, Str(a)) => Val::string(format!("{}{a}", o.clone().to_string()?)),3940 (Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),41 (Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),4243 (Num(v1), Num(v2)) => Val::try_num(v1.get() + v2.get())?,44 #[cfg(feature = "exp-bigint")]45 (BigInt(a), BigInt(b)) => BigInt(Box::new(&**a + &**b)),46 _ => bail!(BinaryOperatorDoesNotOperateOnValues(47 BinaryOpType::Add,48 a.value_type(),49 b.value_type(),50 )),51 })52}5354pub fn evaluate_mod_op(a: &Val, b: &Val) -> Result<Val> {55 use Val::*;56 match (a, b) {57 (Num(a), Num(b)) => {58 if b.get() == 0.0 {59 bail!(DivisionByZero)60 }61 Ok(Val::try_num(a.get() % b.get())?)62 }63 (Str(str), vals) => {64 String::into_untyped(std_format(&str.clone().into_flat(), vals.clone())?)65 }66 (a, b) => bail!(BinaryOperatorDoesNotOperateOnValues(67 BinaryOpType::Mod,68 a.value_type(),69 b.value_type()70 )),71 }72}7374pub fn evaluate_binary_op_special(75 ctx: Context,76 a: &LocExpr,77 op: BinaryOpType,78 b: &LocExpr,79) -> Result<Val> {80 use BinaryOpType::*;81 use Val::*;82 Ok(match (evaluate(ctx.clone(), a)?, op, b) {83 (Bool(true), Or, _o) => Val::Bool(true),84 (Bool(false), And, _o) => Val::Bool(false),85 #[cfg(feature = "exp-null-coaelse")]86 (Null, NullCoaelse, eb) => evaluate(ctx, eb)?,87 #[cfg(feature = "exp-null-coaelse")]88 (a, NullCoaelse, _o) => a,89 (a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(ctx, eb)?)?,90 })91}9293pub fn evaluate_compare_op(a: &Val, b: &Val, op: BinaryOpType) -> Result<Ordering> {94 use Val::*;95 Ok(match (a, b) {96 (Str(a), Str(b)) => a.cmp(b),97 (Num(a), Num(b)) => a.cmp(b),98 #[cfg(feature = "exp-bigint")]99 (BigInt(a), BigInt(b)) => a.cmp(b),100 (Arr(a), Arr(b)) => {101 if let (Some(ai), Some(bi)) = (a.iter_cheap(), b.iter_cheap()) {102 for (a, b) in ai.zip(bi) {103 let ord = evaluate_compare_op(&a, &b, op)?;104 if !ord.is_eq() {105 return Ok(ord);106 }107 }108 } else {109 let ai = a.iter();110 let bi = b.iter();111112 for (a, b) in ai.zip(bi) {113 let ord = evaluate_compare_op(&a?, &b?, op)?;114 if !ord.is_eq() {115 return Ok(ord);116 }117 }118 }119 a.len().cmp(&b.len())120 }121 (_, _) => bail!(BinaryOperatorDoesNotOperateOnValues(122 op,123 a.value_type(),124 b.value_type()125 )),126 })127}128129pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {130 use BinaryOpType::*;131 use Val::*;132 Ok(match (a, op, b) {133 (a, Add, b) => evaluate_add_op(a, b)?,134135 (a, Eq, b) => Bool(equals(a, b)?),136 (a, Neq, b) => Bool(!equals(a, b)?),137138 (a, Lt, b) => Bool(evaluate_compare_op(a, b, Lt)?.is_lt()),139 (a, Gt, b) => Bool(evaluate_compare_op(a, b, Gt)?.is_gt()),140 (a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),141 (a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),142143 (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone().into_flat(), true)),144 (a, Mod, b) => evaluate_mod_op(a, b)?,145146 (Str(v1), Mul, Num(v2)) => Val::string(v1.to_string().repeat(v2.get() as usize)),147148 // Bool X Bool149 (Bool(a), And, Bool(b)) => Bool(*a && *b),150 (Bool(a), Or, Bool(b)) => Bool(*a || *b),151152 // Num X Num153 (Num(v1), Mul, Num(v2)) => Val::try_num(v1.get() * v2.get())?,154 (Num(v1), Div, Num(v2)) => {155 if v2.get() == 0.0 {156 bail!(DivisionByZero)157 }158 Val::try_num(v1.get() / v2.get())?159 }160161 (Num(v1), Sub, Num(v2)) => Val::try_num(v1.get() - v2.get())?,162163 (Num(v1), BitAnd, Num(v2)) => Val::try_num((v1.get() as i64 & v2.get() as i64) as f64)?,164 (Num(v1), BitOr, Num(v2)) => Val::try_num((v1.get() as i64 | v2.get() as i64) as f64)?,165 (Num(v1), BitXor, Num(v2)) => Val::try_num((v1.get() as i64 ^ v2.get() as i64) as f64)?,166 (Num(v1), Lhs, Num(v2)) => {167 if v2.get() < 0.0 {168 bail!("shift by negative exponent")169 }170 let exp = ((v2.get() as i64) & 63) as u32;171 Val::try_num((v1.get() as i64).wrapping_shl(exp) as f64)?172 }173 (Num(v1), Rhs, Num(v2)) => {174 if v2.get() < 0.0 {175 bail!("shift by negative exponent")176 }177 let exp = ((v2.get() as i64) & 63) as u32;178 Val::try_num((v1.get() as i64).wrapping_shr(exp) as f64)?179 }180181 // Bigint X Bigint182 #[cfg(feature = "exp-bigint")]183 (BigInt(a), Mul, BigInt(b)) => BigInt(Box::new(&**a * &**b)),184 #[cfg(feature = "exp-bigint")]185 (BigInt(a), Sub, BigInt(b)) => BigInt(Box::new(&**a - &**b)),186187 _ => bail!(BinaryOperatorDoesNotOperateOnValues(188 op,189 a.value_type(),190 b.value_type(),191 )),192 })193}crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -409,6 +409,7 @@
}
Some(Self(v))
}
+ #[inline]
pub const fn get(&self) -> f64 {
self.0
}
@@ -420,13 +421,15 @@
}
impl Eq for NumValue {}
impl Ord for NumValue {
+ #[inline]
fn cmp(&self, other: &Self) -> Ordering {
// Can't use `total_cmp`: its behavior for `-0` and `0`
// is not following wanted.
- self.0.partial_cmp(&other.0).expect("NaNs are disallowed")
+ unsafe { self.0.partial_cmp(&other.0).unwrap_unchecked() }
}
}
impl PartialOrd for NumValue {
+ #[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
@@ -439,6 +442,7 @@
impl Deref for NumValue {
type Target = f64;
+ #[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
@@ -446,6 +450,7 @@
macro_rules! impl_num {
($($ty:ty),+) => {$(
impl From<$ty> for NumValue {
+ #[inline]
fn from(value: $ty) -> Self {
Self(value.into())
}
@@ -473,6 +478,7 @@
($($ty:ty),+) => {$(
impl TryFrom<$ty> for NumValue {
type Error = ConvertNumValueError;
+ #[inline]
fn try_from(value: $ty) -> Result<Self, ConvertNumValueError> {
use crate::typed::conversions::{MIN_SAFE_INTEGER, MAX_SAFE_INTEGER};
let value = value as f64;
@@ -492,6 +498,7 @@
impl TryFrom<f64> for NumValue {
type Error = ConvertNumValueError;
+ #[inline]
fn try_from(value: f64) -> Result<Self, Self::Error> {
Self::new(value).ok_or(ConvertNumValueError::NonFinite)
}
@@ -499,6 +506,7 @@
impl TryFrom<f32> for NumValue {
type Error = ConvertNumValueError;
+ #[inline]
fn try_from(value: f32) -> Result<Self, Self::Error> {
Self::new(f64::from(value)).ok_or(ConvertNumValueError::NonFinite)
}
nix/jrsonnet.nixdiffbeforeafterboth--- a/nix/jrsonnet.nix
+++ b/nix/jrsonnet.nix
@@ -17,7 +17,7 @@
pname = "jrsonnet";
version = "current${optionalString withNightlyFeatures "-nightly"}${optionalString withExperimentalFeatures "-experimental"}";
- cargoExtraArgs = "--locked --features=mimalloc,legacy-this-file${optionalString withNightlyFeatures ",nightly"}${optionalString withExperimentalFeatures ",experimental"}";
+ cargoExtraArgs = "--locked --features=mimalloc${optionalString withNightlyFeatures ",nightly"}${optionalString withExperimentalFeatures ",experimental"}";
nativeBuildInputs = [makeWrapper];