Skip to content

Commit

Permalink
perf: Reduce binary size
Browse files Browse the repository at this point in the history
It looks like this has little impact on performance.

Inspired by toml-rs#580
  • Loading branch information
epage committed Jul 14, 2023
1 parent 6e63746 commit 4562b9f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
14 changes: 13 additions & 1 deletion crates/toml_edit/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::table::TableLike;
use crate::{Array, InlineTable, Table, Value};

/// Type representing either a value, a table, an array of tables, or none.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub enum Item {
/// Type representing none.
None,
Expand Down Expand Up @@ -316,6 +316,18 @@ impl Item {
}
}

impl Clone for Item {
#[inline(never)]
fn clone(&self) -> Self {
match self {
Item::None => Item::None,
Item::Value(v) => Item::Value(v.clone()),
Item::Table(v) => Item::Table(v.clone()),
Item::ArrayOfTables(v) => Item::ArrayOfTables(v.clone()),
}
}
}

impl Default for Item {
fn default() -> Self {
Item::None
Expand Down
13 changes: 12 additions & 1 deletion crates/toml_edit/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::InternalString;
/// For details see [toml spec](https://github.com/toml-lang/toml/#keyvalue-pair).
///
/// To parse a key use `FromStr` trait implementation: `"string".parse::<Key>()`.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct Key {
key: InternalString,
pub(crate) repr: Option<Repr>,
Expand Down Expand Up @@ -142,6 +142,17 @@ impl Key {
}
}

impl Clone for Key {
#[inline(never)]
fn clone(&self) -> Self {
Self {
key: self.key.clone(),
repr: self.repr.clone(),
decor: self.decor.clone(),
}
}
}

impl std::ops::Deref for Key {
type Target = str;

Expand Down
5 changes: 4 additions & 1 deletion crates/toml_edit/src/parser/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ pub(crate) fn float(input: &mut Input<'_>) -> PResult<f64> {
}

pub(crate) fn float_<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
(dec_int, alt((exp, (frac, opt(exp)).map(|_| ""))))
(
dec_int,
alt((exp.void(), frac.void(), opt(exp.void()).void())),
)
.recognize()
.map(|b: &[u8]| unsafe {
from_utf8_unchecked(
Expand Down

0 comments on commit 4562b9f

Please sign in to comment.