Skip to content

Commit

Permalink
Pre-allocate Vec and HashMap with non-zero capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
danhje committed Apr 4, 2024
1 parent c258572 commit 978089d
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn update_mapping(mapping: &mut JsonMapping, tag_name: String, value: Value) ->
std::collections::hash_map::Entry::Occupied(mut e) => match e.get_mut() {
Value::List(l) => l.push(value),
_ => {
let old_value = std::mem::replace(e.get_mut(), Value::List(vec![]));
let old_value = std::mem::replace(e.get_mut(), Value::List(Vec::with_capacity(2)));
if let Value::List(l) = e.into_mut() {
l.push(old_value);
l.push(value);
Expand All @@ -71,7 +71,7 @@ pub fn _parse(xml: &str) -> Result<JsonMapping> {
let mut reader = Reader::from_str(xml);
reader.trim_text(true);

let mut mapping: JsonMapping = HashMap::new();
let mut mapping: JsonMapping = HashMap::with_capacity(1);
loop {
match reader.read_event() {
Err(e) => return Err(e.into()),
Expand All @@ -81,10 +81,9 @@ pub fn _parse(xml: &str) -> Result<JsonMapping> {
if e.attributes().count() == 0 {
value = Value::None;
} else {
let mut attrs: JsonMapping = HashMap::new();
let mut attrs: JsonMapping = HashMap::with_capacity(e.attributes().count());
for attr in e.attributes() {
let attr = attr?;
// attrs.set_item(format!("@{}", attr.key.qn()?), attr.unescape_value()?)?;
attrs.insert(
"@".to_string() + &attr.key.qn()?,
Value::Text(attr.unescape_value()?.parse()?),
Expand All @@ -99,7 +98,7 @@ pub fn _parse(xml: &str) -> Result<JsonMapping> {
mapping.insert("#text".to_string(), Value::Text(text));
}
Ok(Event::Start(e)) => {
let mut sub_xml_mapping = Value::Mapping(HashMap::new());
let mut sub_xml_mapping = Value::Mapping(HashMap::with_capacity(1));
if e.attributes().count() > 0 {
for attr in e.attributes() {
let attr = attr?;
Expand Down

0 comments on commit 978089d

Please sign in to comment.