Skip to content

Commit

Permalink
Make Directory::path a Cow.
Browse files Browse the repository at this point in the history
Because we create a lot of these in the macro parser, but only very
rarely modify them.

This speeds up some html5ever runs by 2--3%.
  • Loading branch information
nnethercote committed May 18, 2018
1 parent fcf2b24 commit ad47145
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use parse::token::Token::*;
use symbol::Symbol;
use tokenstream::{TokenStream, TokenTree};

use std::borrow::Cow;
use std::collections::HashMap;
use std::collections::hash_map::Entry;

Expand Down Expand Up @@ -141,7 +142,7 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
}

let directory = Directory {
path: cx.current_expansion.module.directory.clone(),
path: Cow::from(cx.current_expansion.module.directory.as_path()),
ownership: cx.current_expansion.directory_ownership,
};
let mut p = Parser::new(cx.parse_sess(), tts, Some(directory), true, false);
Expand Down
5 changes: 3 additions & 2 deletions src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use symbol::Symbol;
use tokenstream::{TokenStream, TokenTree};
use diagnostics::plugin::ErrorMap;

use std::borrow::Cow;
use std::collections::HashSet;
use std::iter;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -89,8 +90,8 @@ impl ParseSess {
}

#[derive(Clone)]
pub struct Directory {
pub path: PathBuf,
pub struct Directory<'a> {
pub path: Cow<'a, Path>,
pub ownership: DirectoryOwnership,
}

Expand Down
17 changes: 9 additions & 8 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ use tokenstream::{self, Delimited, ThinTokenStream, TokenTree, TokenStream};
use symbol::{Symbol, keywords};
use util::ThinVec;

use std::borrow::Cow;
use std::cmp;
use std::mem;
use std::path::{self, Path, PathBuf};
Expand Down Expand Up @@ -228,7 +229,7 @@ pub struct Parser<'a> {
prev_token_kind: PrevTokenKind,
pub restrictions: Restrictions,
/// Used to determine the path to externally loaded source files
pub directory: Directory,
pub directory: Directory<'a>,
/// Whether to parse sub-modules in other files.
pub recurse_into_file_modules: bool,
/// Name of the root module this parser originated from. If `None`, then the
Expand Down Expand Up @@ -535,7 +536,7 @@ enum TokenExpectType {
impl<'a> Parser<'a> {
pub fn new(sess: &'a ParseSess,
tokens: TokenStream,
directory: Option<Directory>,
directory: Option<Directory<'a>>,
recurse_into_file_modules: bool,
desugar_doc_comments: bool)
-> Self {
Expand All @@ -549,7 +550,7 @@ impl<'a> Parser<'a> {
restrictions: Restrictions::empty(),
recurse_into_file_modules,
directory: Directory {
path: PathBuf::new(),
path: Cow::from(PathBuf::new()),
ownership: DirectoryOwnership::Owned { relative: None }
},
root_module_name: None,
Expand All @@ -572,9 +573,9 @@ impl<'a> Parser<'a> {
if let Some(directory) = directory {
parser.directory = directory;
} else if !parser.span.source_equal(&DUMMY_SP) {
if let FileName::Real(path) = sess.codemap().span_to_unmapped_path(parser.span) {
parser.directory.path = path;
parser.directory.path.pop();
if let FileName::Real(mut path) = sess.codemap().span_to_unmapped_path(parser.span) {
path.pop();
parser.directory.path = Cow::from(path);
}
}

Expand Down Expand Up @@ -6000,10 +6001,10 @@ impl<'a> Parser<'a> {

fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) {
if let Some(path) = attr::first_attr_value_str_by_name(attrs, "path") {
self.directory.path.push(&path.as_str());
self.directory.path.to_mut().push(&path.as_str());
self.directory.ownership = DirectoryOwnership::Owned { relative: None };
} else {
self.directory.path.push(&id.name.as_str());
self.directory.path.to_mut().push(&id.name.as_str());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use print::pprust;
use serialize::{Decoder, Decodable, Encoder, Encodable};
use util::RcSlice;

use std::borrow::Cow;
use std::{fmt, iter, mem};
use std::hash::{self, Hash};

Expand Down Expand Up @@ -106,7 +107,7 @@ impl TokenTree {
-> macro_parser::NamedParseResult {
// `None` is because we're not interpolating
let directory = Directory {
path: cx.current_expansion.module.directory.clone(),
path: Cow::from(cx.current_expansion.module.directory.as_path()),
ownership: cx.current_expansion.directory_ownership,
};
macro_parser::parse(cx.parse_sess(), tts, mtch, Some(directory), true)
Expand Down

0 comments on commit ad47145

Please sign in to comment.