difftreelog
perf add string extension threshold
in: master
2 files changed
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth1use std::cmp::Ordering;23use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};45use crate::{6 arr::ArrValue,7 error::ErrorKind::*,8 evaluate,9 stdlib::std_format,10 throw,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 (Not, Bool(v)) => Bool(!v),21 (Minus, Num(n)) => Num(-*n),22 (BitNot, Num(n)) => Num(f64::from(!(*n as i32))),23 (op, o) => throw!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),24 })25}2627pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {28 use Val::*;29 Ok(match (a, b) {30 (Str(v1), Str(v2)) => Str(StrValue::concat(v1.clone(), v2.clone())),3132 // Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)33 (Num(a), Str(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),34 (Str(a), Num(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),3536 (Str(a), o) | (o, Str(a)) if a.is_empty() => {37 Val::Str(StrValue::Flat(o.clone().to_string()?))38 }39 (Str(a), o) => Str(StrValue::Flat(40 format!("{a}{}", o.clone().to_string()?).into(),41 )),42 (o, Str(a)) => Str(StrValue::Flat(43 format!("{}{a}", o.clone().to_string()?).into(),44 )),4546 (Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),47 (Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),4849 (Num(v1), Num(v2)) => Val::new_checked_num(v1 + v2)?,50 _ => throw!(BinaryOperatorDoesNotOperateOnValues(51 BinaryOpType::Add,52 a.value_type(),53 b.value_type(),54 )),55 })56}5758pub fn evaluate_mod_op(a: &Val, b: &Val) -> Result<Val> {59 use Val::*;60 match (a, b) {61 (Num(a), Num(b)) => {62 if *b == 0.0 {63 throw!(DivisionByZero)64 }65 Ok(Num(a % b))66 }67 (Str(str), vals) => {68 String::into_untyped(std_format(&str.clone().into_flat(), vals.clone())?)69 }70 (a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(71 BinaryOpType::Mod,72 a.value_type(),73 b.value_type()74 )),75 }76}7778pub fn evaluate_binary_op_special(79 ctx: Context,80 a: &LocExpr,81 op: BinaryOpType,82 b: &LocExpr,83) -> Result<Val> {84 use BinaryOpType::*;85 use Val::*;86 Ok(match (evaluate(ctx.clone(), a)?, op, b) {87 (Bool(true), Or, _o) => Val::Bool(true),88 (Bool(false), And, _o) => Val::Bool(false),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 (Arr(a), Arr(b)) => {99 let ai = a.iter();100 let bi = b.iter();101102 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 }108109 a.len().cmp(&b.len())110 }111 (_, _) => throw!(BinaryOperatorDoesNotOperateOnValues(112 op,113 a.value_type(),114 b.value_type()115 )),116 })117}118119pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {120 use BinaryOpType::*;121 use Val::*;122 Ok(match (a, op, b) {123 (a, Add, b) => evaluate_add_op(a, b)?,124125 (a, Eq, b) => Bool(equals(a, b)?),126 (a, Neq, b) => Bool(!equals(a, b)?),127128 (a, Lt, b) => Bool(evaluate_compare_op(a, b, Lt)?.is_lt()),129 (a, Gt, b) => Bool(evaluate_compare_op(a, b, Gt)?.is_gt()),130 (a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),131 (a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),132133 (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone().into_flat(), true)),134 (a, Mod, b) => evaluate_mod_op(a, b)?,135136 (Str(v1), Mul, Num(v2)) => Str(StrValue::Flat(v1.to_string().repeat(*v2 as usize).into())),137138 // Bool X Bool139 (Bool(a), And, Bool(b)) => Bool(*a && *b),140 (Bool(a), Or, Bool(b)) => Bool(*a || *b),141142 // Num X Num143 (Num(v1), Mul, Num(v2)) => Val::new_checked_num(v1 * v2)?,144 (Num(v1), Div, Num(v2)) => {145 if *v2 == 0.0 {146 throw!(DivisionByZero)147 }148 Val::new_checked_num(v1 / v2)?149 }150151 (Num(v1), Sub, Num(v2)) => Val::new_checked_num(v1 - v2)?,152153 (Num(v1), BitAnd, Num(v2)) => Num(f64::from((*v1 as i32) & (*v2 as i32))),154 (Num(v1), BitOr, Num(v2)) => Num(f64::from((*v1 as i32) | (*v2 as i32))),155 (Num(v1), BitXor, Num(v2)) => Num(f64::from((*v1 as i32) ^ (*v2 as i32))),156 (Num(v1), Lhs, Num(v2)) => {157 if *v2 < 0.0 {158 throw!("shift by negative exponent")159 }160 Num(f64::from((*v1 as i32) << (*v2 as i32)))161 }162 (Num(v1), Rhs, Num(v2)) => {163 if *v2 < 0.0 {164 throw!("shift by negative exponent")165 }166 Num(f64::from((*v1 as i32) >> (*v2 as i32)))167 }168169 _ => throw!(BinaryOperatorDoesNotOperateOnValues(170 op,171 a.value_type(),172 b.value_type(),173 )),174 })175}crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -199,34 +199,40 @@
}
impl StrValue {
pub fn concat(a: StrValue, b: StrValue) -> Self {
+ // TODO: benchmark for an optimal value, currently just a arbitrary choice
+ const STRING_EXTEND_THRESHOLD: usize = 100;
+
if a.is_empty() {
b
} else if b.is_empty() {
a
+ } else if a.len() + b.len() < STRING_EXTEND_THRESHOLD {
+ Self::Flat(format!("{a}{b}").into())
} else {
let len = a.len() + b.len();
Self::Tree(Rc::new((a, b, len)))
}
}
pub fn into_flat(self) -> IStr {
+ #[cold]
+ fn write_buf(s: &StrValue, out: &mut String) {
+ match s {
+ StrValue::Flat(f) => out.push_str(f),
+ StrValue::Tree(t) => {
+ write_buf(&t.0, out);
+ write_buf(&t.1, out);
+ }
+ }
+ }
match self {
StrValue::Flat(f) => f,
StrValue::Tree(_) => {
- let mut buf = String::new();
- self.into_flat_buf(&mut buf);
+ let mut buf = String::with_capacity(self.len());
+ write_buf(&self, &mut buf);
buf.into()
}
}
}
- fn into_flat_buf(&self, out: &mut String) {
- match self {
- StrValue::Flat(f) => out.push_str(f),
- StrValue::Tree(t) => {
- t.0.into_flat_buf(out);
- t.1.into_flat_buf(out);
- }
- }
- }
pub fn len(&self) -> usize {
match self {
StrValue::Flat(v) => v.len(),
@@ -236,7 +242,8 @@
pub fn is_empty(&self) -> bool {
match self {
Self::Flat(v) => v.is_empty(),
- _ => false,
+ // Can't create non-flat empty string
+ Self::Tree(_) => false,
}
}
}