Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid allocations in Decoder::read_str. #37064

Merged
merged 1 commit into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use rustc_const_math::ConstInt;

use rustc::mir::repr::Mir;

use std::borrow::Cow;
use std::cell::Ref;
use std::io;
use std::mem;
Expand Down Expand Up @@ -202,7 +203,7 @@ impl<'doc, 'tcx> Decoder for DecodeContext<'doc, 'tcx> {
read_f64 -> f64;
read_f32 -> f32;
read_char -> char;
read_str -> String;
read_str -> Cow<str>;
}

fn error(&mut self, err: &str) -> Self::Error {
Expand Down
7 changes: 3 additions & 4 deletions src/libserialize/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ use self::DecoderError::*;
use self::ParserState::*;
use self::InternalStackElement::*;

use std::borrow::Cow;
use std::collections::{HashMap, BTreeMap};
use std::io::prelude::*;
use std::io;
Expand Down Expand Up @@ -2081,9 +2082,7 @@ impl Decoder {
pub fn new(json: Json) -> Decoder {
Decoder { stack: vec![json] }
}
}

impl Decoder {
fn pop(&mut self) -> Json {
self.stack.pop().unwrap()
}
Expand Down Expand Up @@ -2182,8 +2181,8 @@ impl ::Decoder for Decoder {
Err(ExpectedError("single character string".to_owned(), format!("{}", s)))
}

fn read_str(&mut self) -> DecodeResult<string::String> {
expect!(self.pop(), String)
fn read_str(&mut self) -> DecodeResult<Cow<str>> {
expect!(self.pop(), String).map(Cow::Owned)
}

fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T> where
Expand Down
5 changes: 3 additions & 2 deletions src/libserialize/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

use leb128::{read_signed_leb128, read_unsigned_leb128, write_signed_leb128, write_unsigned_leb128};
use std::borrow::Cow;
use std::io::{self, Write};
use serialize;

Expand Down Expand Up @@ -246,11 +247,11 @@ impl<'a> serialize::Decoder for Decoder<'a> {
Ok(::std::char::from_u32(bits).unwrap())
}

fn read_str(&mut self) -> Result<String, Self::Error> {
fn read_str(&mut self) -> Result<Cow<str>, Self::Error> {
let len = self.read_usize()?;
let s = ::std::str::from_utf8(&self.data[self.position..self.position + len]).unwrap();
self.position += len;
Ok(s.to_string())
Ok(Cow::Borrowed(s))
}

fn error(&mut self, err: &str) -> Self::Error {
Expand Down
5 changes: 3 additions & 2 deletions src/libserialize/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Core encoding and decoding interfaces.
*/

use std::borrow::Cow;
use std::intrinsics;
use std::path;
use std::rc::Rc;
Expand Down Expand Up @@ -156,7 +157,7 @@ pub trait Decoder {
fn read_f64(&mut self) -> Result<f64, Self::Error>;
fn read_f32(&mut self) -> Result<f32, Self::Error>;
fn read_char(&mut self) -> Result<char, Self::Error>;
fn read_str(&mut self) -> Result<String, Self::Error>;
fn read_str(&mut self) -> Result<Cow<str>, Self::Error>;

// Compound types:
fn read_enum<T, F>(&mut self, _name: &str, f: F) -> Result<T, Self::Error>
Expand Down Expand Up @@ -401,7 +402,7 @@ impl Encodable for String {

impl Decodable for String {
fn decode<D: Decoder>(d: &mut D) -> Result<String, D::Error> {
d.read_str()
Ok(d.read_str()?.into_owned())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Encodable for Name {

impl Decodable for Name {
fn decode<D: Decoder>(d: &mut D) -> Result<Name, D::Error> {
Ok(token::intern(&d.read_str()?[..]))
Ok(token::intern(&d.read_str()?))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ impl PartialEq<InternedString> for str {

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())
}
}

Expand Down