Skip to content

Commit

Permalink
Favor String::from_str over to_string
Browse files Browse the repository at this point in the history
`to_string` uses the formatting functionality used by Show, which is
overly expensive.

On a 100MB XML file, this reduced time to open, parse, and output it
to /dev/null by 1.2 seconds (taking 87% of the previous time).
  • Loading branch information
shepmaster committed Sep 14, 2014
1 parent e6ea47f commit 4f29dee
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,30 +585,30 @@ struct Hydrator;

impl Hydrator {
fn hydrate_text(&self, doc: &super::Document, text_data: Text) -> super::Text {
doc.new_text(text_data.text.to_string())
doc.new_text(String::from_str(text_data.text))
}

fn hydrate_reference_raw(&self, ref_data: Reference) -> String {
match ref_data {
DecimalCharReference(d) => {
let code: u32 = from_str_radix(d, 10).expect("Not valid decimal");
let c: char = from_u32(code).expect("Not a valid codepoint");
c.to_string()
String::from_char(1, c)
},
HexCharReference(h) => {
let code: u32 = from_str_radix(h, 16).expect("Not valid hex");
let c: char = from_u32(code).expect("Not a valid codepoint");
c.to_string()
String::from_char(1, c)
},
EntityReference(e) => {
match e {
String::from_str(match e {
"amp" => "&",
"lt" => "<",
"gt" => ">",
"apos" => "'",
"quot" => "\"",
_ => fail!("unknown entity"),
}.to_string()
})
}
}
}
Expand All @@ -618,24 +618,24 @@ impl Hydrator {
}

fn hydrate_comment(&self, doc: &super::Document, comment_data: Comment) -> super::Comment {
doc.new_comment(comment_data.text.to_string())
doc.new_comment(String::from_str(comment_data.text))
}

fn hydrate_pi(&self, doc: &super::Document, pi_data: ProcessingInstruction) -> super::ProcessingInstruction {
doc.new_processing_instruction(pi_data.target.to_string(), pi_data.value.map(|v| v.to_string()))
doc.new_processing_instruction(String::from_str(pi_data.target), pi_data.value.map(|v| String::from_str(v)))
}

fn hydrate_element(&self, doc: &super::Document, element_data: Element) -> super::Element {
let element = doc.new_element(element_data.name.to_string());
let element = doc.new_element(String::from_str(element_data.name));

for attr in element_data.attributes.move_iter() {
let to_v_str = |v: AttributeValue| match v {
LiteralAttributeValue(v) => v.to_string(),
LiteralAttributeValue(v) => String::from_str(v),
ReferenceAttributeValue(r) => self.hydrate_reference_raw(r),
};

let v = attr.values.move_iter().fold(String::new(), |s, v| s.append(to_v_str(v).as_slice()));
element.set_attribute(attr.name.to_string(), v);
element.set_attribute(String::from_str(attr.name), v);
}

for child in element_data.children.move_iter() {
Expand Down

0 comments on commit 4f29dee

Please sign in to comment.