Skip to content

Commit

Permalink
perf: Remove spurious allocations (toml-rs#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
epage authored Sep 24, 2021
1 parent 1f81d2e commit 41b4527
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
11 changes: 5 additions & 6 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,23 @@ impl Index for str {
}
}
fn index_or_insert<'v>(&self, v: &'v mut Item) -> &'v mut Item {
let key = Key::new(self);
if let Item::None = *v {
let mut t = InlineTable::default();
t.items.insert(
InternalString::from(key.get()),
TableKeyValue::new(key.clone(), Item::None),
InternalString::from(self),
TableKeyValue::new(Key::new(self), Item::None),
);
*v = value(Value::InlineTable(t));
}
match *v {
Item::Table(ref mut t) => t.entry(key.get()).or_insert(Item::None),
Item::Table(ref mut t) => t.entry(self).or_insert(Item::None),
Item::Value(ref mut v) if v.is_inline_table() => {
&mut v
.as_inline_table_mut()
.unwrap()
.items
.entry(InternalString::from(key.get()))
.or_insert(TableKeyValue::new(key, Item::None))
.entry(InternalString::from(self))
.or_insert_with(|| TableKeyValue::new(Key::new(self), Item::None))
.value
}
_ => panic!("cannot access key {}", self),
Expand Down
8 changes: 5 additions & 3 deletions src/parser/inline_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ fn table_from_pairs(
) -> Result<InlineTable, CustomError> {
let mut root = InlineTable::new();
root.preamble = InternalString::from(preamble);
// Assuming almost all pairs will be directly in `root`
root.items.reserve(v.len());

for (path, kv) in v {
let table = descend_path(&mut root, &path, 0)?;
Expand All @@ -33,7 +35,7 @@ fn table_from_pairs(
table: "inline".into(),
});
}
table.items.insert(kv.key.clone().into(), kv);
table.items.insert(kv.key.get().into(), kv);
}
Ok(root)
}
Expand Down Expand Up @@ -88,8 +90,8 @@ parse!(keyval() -> (Vec<Key>, TableKeyValue), {
char(KEYVAL_SEP),
(ws(), value(), ws()),
).map(|(key, _, v)| {
let mut path = key.into_vec();
let key = path.pop().expect("Was vec1, so at least one exists");
let mut path = key.into_vec();
let key = path.pop().expect("Was vec1, so at least one exists");

let (pre, v, suf) = v;
let v = v.decorated(pre, suf);
Expand Down

0 comments on commit 41b4527

Please sign in to comment.