Skip to content

Commit 8c99072

Browse files
Avishay Yagodajstewmon
Avishay Yagoda
authored andcommitted
Replacing ok_or() calls with ok_or_else(||) calls, to improve performance
(cherry picked from commit ebc5770)
1 parent 24f490a commit 8c99072

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

src/js_op.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ pub fn abstract_max(items: &Vec<&Value>) -> Result<f64, Error> {
434434
items
435435
.into_iter()
436436
.map(|v| {
437-
to_number(v).ok_or(Error::InvalidArgument {
437+
to_number(v).ok_or_else(|| Error::InvalidArgument {
438438
value: (*v).clone(),
439439
operation: "max".into(),
440440
reason: "Could not convert value to number".into(),
@@ -460,7 +460,7 @@ pub fn abstract_min(items: &Vec<&Value>) -> Result<f64, Error> {
460460
items
461461
.into_iter()
462462
.map(|v| {
463-
to_number(v).ok_or(Error::InvalidArgument {
463+
to_number(v).ok_or_else(|| Error::InvalidArgument {
464464
value: (*v).clone(),
465465
operation: "max".into(),
466466
reason: "Could not convert value to number".into(),
@@ -518,7 +518,7 @@ pub fn abstract_plus(first: &Value, second: &Value) -> Value {
518518
pub fn parse_float_add(vals: &Vec<&Value>) -> Result<f64, Error> {
519519
vals.into_iter()
520520
.map(|&v| {
521-
parse_float(v).ok_or(Error::InvalidArgument {
521+
parse_float(v).ok_or_else(|| Error::InvalidArgument {
522522
value: v.clone(),
523523
operation: "+".into(),
524524
reason: "Argument could not be converted to a float".into(),
@@ -541,7 +541,7 @@ pub fn parse_float_add(vals: &Vec<&Value>) -> Result<f64, Error> {
541541
pub fn parse_float_mul(vals: &Vec<&Value>) -> Result<f64, Error> {
542542
vals.into_iter()
543543
.map(|&v| {
544-
parse_float(v).ok_or(Error::InvalidArgument {
544+
parse_float(v).ok_or_else(|| Error::InvalidArgument {
545545
value: v.clone(),
546546
operation: "*".into(),
547547
reason: "Argument could not be converted to a float".into(),
@@ -629,7 +629,7 @@ pub fn abstract_mod(first: &Value, second: &Value) -> Result<f64, Error> {
629629
pub fn to_negative(val: &Value) -> Result<f64, Error> {
630630
to_number(val)
631631
.map(|v| -1.0 * v)
632-
.ok_or(Error::InvalidArgument {
632+
.ok_or_else(|| Error::InvalidArgument {
633633
value: val.clone(),
634634
operation: "to_negative".into(),
635635
reason: "Could not convert value to a number".into(),

src/op/data.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ impl<'a> TryFrom<Value> for KeyType<'a> {
2323
match value {
2424
Value::Null => Ok(Self::Null),
2525
Value::String(s) => Ok(Self::String(Cow::from(s))),
26-
Value::Number(n) => Ok(Self::Number(n.as_i64().ok_or(
26+
Value::Number(n) => Ok(Self::Number(n.as_i64().ok_or_else(|| {
2727
Error::InvalidVariableKey {
2828
value: Value::Number(n),
2929
reason: "Numeric keys must be valid integers".into(),
30-
},
31-
)?)),
30+
}
31+
})?)),
3232
_ => Err(Error::InvalidVariableKey {
3333
value: value.clone(),
3434
reason: "Variable keys must be strings, integers, or null".into(),
@@ -43,12 +43,12 @@ impl<'a> TryFrom<&'a Value> for KeyType<'a> {
4343
match value {
4444
Value::Null => Ok(Self::Null),
4545
Value::String(s) => Ok(Self::String(Cow::from(s))),
46-
Value::Number(n) => Ok(Self::Number(n.as_i64().ok_or(
46+
Value::Number(n) => Ok(Self::Number(n.as_i64().ok_or_else(|| {
4747
Error::InvalidVariableKey {
4848
value: value.clone(),
4949
reason: "Numeric keys must be valid integers".into(),
50-
},
51-
)?)),
50+
}
51+
})?)),
5252
_ => Err(Error::InvalidVariableKey {
5353
value: value.clone(),
5454
reason: "Variable keys must be strings, integers, or null".into(),
@@ -155,7 +155,7 @@ pub fn missing_some(data: &Value, args: &Vec<&Value>) -> Result<Value, Error> {
155155
Value::Number(n) => n.as_u64(),
156156
_ => None,
157157
}
158-
.ok_or(Error::InvalidArgument {
158+
.ok_or_else(|| Error::InvalidArgument {
159159
value: threshold_arg.clone(),
160160
operation: "missing_some".into(),
161161
reason: "missing_some threshold must be a valid, positive integer".into(),

src/op/mod.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,18 @@ fn op_from_map<'a, 'b, T: CommonOperator>(
501501

502502
// We've already validated the length to be one, so any error
503503
// here is super unexpected.
504-
let key = obj.keys().next().ok_or(Error::UnexpectedError(format!(
505-
"could not get first key from len(1) object: {:?}",
506-
obj
507-
)))?;
508-
let val = obj.get(key).ok_or(Error::UnexpectedError(format!(
509-
"could not get value for key '{}' from len(1) object: {:?}",
510-
key, obj
511-
)))?;
504+
let key = obj.keys().next().ok_or_else(|| {
505+
Error::UnexpectedError(format!(
506+
"could not get first key from len(1) object: {:?}",
507+
obj
508+
))
509+
})?;
510+
let val = obj.get(key).ok_or_else(|| {
511+
Error::UnexpectedError(format!(
512+
"could not get value for key '{}' from len(1) object: {:?}",
513+
key, obj
514+
))
515+
})?;
512516

513517
// See if the key is an operator. If it's not, return None.
514518
let op = match map.get(key.as_str()) {

src/value.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ impl<'a> Parsed<'a> {
2626
.or(LazyOperation::from_value(value)?.map(Self::LazyOperation))
2727
.or(DataOperation::from_value(value)?.map(Self::DataOperation))
2828
.or(Raw::from_value(value)?.map(Self::Raw))
29-
.ok_or(Error::UnexpectedError(format!(
30-
"Failed to parse Value {:?}",
31-
value
32-
)))
29+
.ok_or_else(|| {
30+
Error::UnexpectedError(format!("Failed to parse Value {:?}", value))
31+
})
3332
}
3433

3534
pub fn from_values(values: Vec<&'a Value>) -> Result<Vec<Self>, Error> {
@@ -106,10 +105,12 @@ pub fn to_number_value(number: f64) -> Result<Value, Error> {
106105
Ok(Value::Number(Number::from(number as i64)))
107106
} else {
108107
Number::from_f64(number)
109-
.ok_or(Error::UnexpectedError(format!(
110-
"Could not make JSON number from result {:?}",
111-
number
112-
)))
108+
.ok_or_else(|| {
109+
Error::UnexpectedError(format!(
110+
"Could not make JSON number from result {:?}",
111+
number
112+
))
113+
})
113114
.map(Value::Number)
114115
}
115116
}

0 commit comments

Comments
 (0)