Avoid allocations in Decoder::read_str.

`opaque::Decoder::read_str` is very hot within `rustc` due to its use in
the reading of crate metadata, and it currently returns a `String`. This
commit changes it to instead return a `Cow<str>`, which avoids a heap
allocation.

This change reduces the number of calls to `malloc` by almost 10% in
some benchmarks.

This is a [breaking-change] to libserialize.
This commit is contained in:
Nicholas Nethercote
2016-10-10 09:07:18 +11:00
parent 9d4d0da7af
commit b043e11de2
6 changed files with 13 additions and 11 deletions
+1 -1
View File
@@ -566,7 +566,7 @@ fn ne(&self, other: &InternedString) -> bool {
impl Decodable for InternedString {
fn decode<D: Decoder>(d: &mut D) -> Result<InternedString, D::Error> {
Ok(intern(d.read_str()?.as_ref()).as_str())
Ok(intern(&d.read_str()?).as_str())
}
}