Skip to content

Commit

Permalink
Remove 'Arc' around u128 and i128 in ValueRepr
Browse files Browse the repository at this point in the history
The largest variant is already at least 16 bytes, so the `Arc` only adds
an allocation without gain
  • Loading branch information
SergioBenitez committed Nov 9, 2022
1 parent ac738f1 commit c11507c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions minijinja/src/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ impl<'a> Key<'a> {
ValueRepr::U64(v) => TryFrom::try_from(v)
.map(Key::I64)
.map_err(|_| ErrorKind::NonKey.into()),
ValueRepr::U128(ref v) => TryFrom::try_from(**v)
ValueRepr::U128(v) => TryFrom::try_from(v)
.map(Key::I64)
.map_err(|_| ErrorKind::NonKey.into()),
ValueRepr::I64(v) => Ok(Key::I64(v)),
ValueRepr::I128(ref v) => TryFrom::try_from(**v)
ValueRepr::I128(v) => TryFrom::try_from(v)
.map(Key::I64)
.map_err(|_| ErrorKind::NonKey.into()),
ValueRepr::F64(x) => {
Expand Down
8 changes: 4 additions & 4 deletions minijinja/src/value/argtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ impl From<()> for Value {
impl From<i128> for Value {
#[inline(always)]
fn from(val: i128) -> Self {
ValueRepr::I128(Arc::new(val)).into()
ValueRepr::I128(val).into()
}
}

impl From<u128> for Value {
#[inline(always)]
fn from(val: u128) -> Self {
ValueRepr::U128(Arc::new(val)).into()
ValueRepr::U128(val).into()
}
}

Expand Down Expand Up @@ -339,8 +339,8 @@ macro_rules! primitive_int_try_from {
ValueRepr::U64(val) => val,
// for the intention here see Key::from_borrowed_value
ValueRepr::F64(val) if (val as i64 as f64 == val) => val as i64,
ValueRepr::I128(ref val) => **val,
ValueRepr::U128(ref val) => **val,
ValueRepr::I128(val) => val,
ValueRepr::U128(val) => val,
});
}
}
Expand Down
16 changes: 8 additions & 8 deletions minijinja/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ pub(crate) enum ValueRepr {
F64(f64),
Char(char),
None,
U128(Arc<u128>),
I128(Arc<i128>),
U128(u128),
I128(i128),
String(Arc<String>, StringType),
Bytes(Arc<Vec<u8>>),
Seq(Arc<Vec<Value>>),
Expand Down Expand Up @@ -482,9 +482,9 @@ impl Value {
match self.0 {
ValueRepr::Bool(val) => val,
ValueRepr::U64(x) => x != 0,
ValueRepr::U128(ref x) => **x != 0,
ValueRepr::U128(x) => x != 0,
ValueRepr::I64(x) => x != 0,
ValueRepr::I128(ref x) => **x != 0,
ValueRepr::I128(x) => x != 0,
ValueRepr::F64(x) => x != 0.0,
ValueRepr::Char(x) => x != '\x00',
ValueRepr::String(ref x, _) => !x.is_empty(),
Expand Down Expand Up @@ -779,11 +779,11 @@ impl Value {
ValueRepr::U64(v) => TryFrom::try_from(v)
.map(Key::I64)
.map_err(|_| ErrorKind::NonKey.into()),
ValueRepr::U128(ref v) => TryFrom::try_from(**v)
ValueRepr::U128(v) => TryFrom::try_from(v)
.map(Key::I64)
.map_err(|_| ErrorKind::NonKey.into()),
ValueRepr::I64(v) => Ok(Key::I64(v)),
ValueRepr::I128(ref v) => TryFrom::try_from(**v)
ValueRepr::I128(v) => TryFrom::try_from(v)
.map(Key::I64)
.map_err(|_| ErrorKind::NonKey.into()),
ValueRepr::Char(c) => Ok(Key::Char(c)),
Expand Down Expand Up @@ -857,8 +857,8 @@ impl Serialize for Value {
ValueRepr::Char(c) => serializer.serialize_char(c),
ValueRepr::None => serializer.serialize_unit(),
ValueRepr::Undefined => serializer.serialize_unit(),
ValueRepr::U128(ref u) => serializer.serialize_u128(**u),
ValueRepr::I128(ref i) => serializer.serialize_i128(**i),
ValueRepr::U128(u) => serializer.serialize_u128(u),
ValueRepr::I128(i) => serializer.serialize_i128(i),
ValueRepr::String(ref s, _) => serializer.serialize_str(s),
ValueRepr::Bytes(ref b) => serializer.serialize_bytes(b),
ValueRepr::Seq(ref elements) => elements.serialize(serializer),
Expand Down
8 changes: 4 additions & 4 deletions minijinja/src/value/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fn as_f64(value: &Value) -> Option<f64> {
Some(match value.0 {
ValueRepr::Bool(x) => x as i64 as f64,
ValueRepr::U64(x) => x as f64,
ValueRepr::U128(ref x) => **x as f64,
ValueRepr::U128(x) => x as f64,
ValueRepr::I64(x) => x as f64,
ValueRepr::I128(ref x) => **x as f64,
ValueRepr::I128(x) => x as f64,
ValueRepr::F64(x) => x,
_ => return None,
})
Expand All @@ -27,13 +27,13 @@ pub fn coerce(a: &Value, b: &Value) -> Option<CoerceResult> {
// equal mappings are trivial
(ValueRepr::U64(a), ValueRepr::U64(b)) => Some(CoerceResult::I128(*a as i128, *b as i128)),
(ValueRepr::U128(a), ValueRepr::U128(b)) => {
Some(CoerceResult::I128(**a as i128, **b as i128))
Some(CoerceResult::I128(*a as i128, *b as i128))
}
(ValueRepr::String(a, _), ValueRepr::String(b, _)) => {
Some(CoerceResult::String(a.to_string(), b.to_string()))
}
(ValueRepr::I64(a), ValueRepr::I64(b)) => Some(CoerceResult::I128(*a as i128, *b as i128)),
(ValueRepr::I128(ref a), ValueRepr::I128(ref b)) => Some(CoerceResult::I128(**a, **b)),
(ValueRepr::I128(a), ValueRepr::I128(b)) => Some(CoerceResult::I128(*a, *b)),
(ValueRepr::F64(a), ValueRepr::F64(b)) => Some(CoerceResult::F64(*a, *b)),

// are floats involved?
Expand Down
4 changes: 2 additions & 2 deletions minijinja/src/value/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Serializer for ValueSerializer {
}

fn serialize_i128(self, v: i128) -> Result<Value, Error> {
Ok(ValueRepr::I128(Arc::new(v)).into())
Ok(ValueRepr::I128(v).into())
}

fn serialize_u8(self, v: u8) -> Result<Value, Error> {
Expand All @@ -63,7 +63,7 @@ impl Serializer for ValueSerializer {
}

fn serialize_u128(self, v: u128) -> Result<Value, Error> {
Ok(ValueRepr::U128(Arc::new(v)).into())
Ok(ValueRepr::U128(v).into())
}

fn serialize_f32(self, v: f32) -> Result<Value, Error> {
Expand Down

0 comments on commit c11507c

Please sign in to comment.