Skip to content

Commit ad47145

Browse files
committed
Make Directory::path a Cow.
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%.
1 parent fcf2b24 commit ad47145

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

src/libsyntax/ext/tt/macro_rules.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use parse::token::Token::*;
2626
use symbol::Symbol;
2727
use tokenstream::{TokenStream, TokenTree};
2828

29+
use std::borrow::Cow;
2930
use std::collections::HashMap;
3031
use std::collections::hash_map::Entry;
3132

@@ -141,7 +142,7 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
141142
}
142143

143144
let directory = Directory {
144-
path: cx.current_expansion.module.directory.clone(),
145+
path: Cow::from(cx.current_expansion.module.directory.as_path()),
145146
ownership: cx.current_expansion.directory_ownership,
146147
};
147148
let mut p = Parser::new(cx.parse_sess(), tts, Some(directory), true, false);

src/libsyntax/parse/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use symbol::Symbol;
2323
use tokenstream::{TokenStream, TokenTree};
2424
use diagnostics::plugin::ErrorMap;
2525

26+
use std::borrow::Cow;
2627
use std::collections::HashSet;
2728
use std::iter;
2829
use std::path::{Path, PathBuf};
@@ -89,8 +90,8 @@ impl ParseSess {
8990
}
9091

9192
#[derive(Clone)]
92-
pub struct Directory {
93-
pub path: PathBuf,
93+
pub struct Directory<'a> {
94+
pub path: Cow<'a, Path>,
9495
pub ownership: DirectoryOwnership,
9596
}
9697

src/libsyntax/parse/parser.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use tokenstream::{self, Delimited, ThinTokenStream, TokenTree, TokenStream};
5757
use symbol::{Symbol, keywords};
5858
use util::ThinVec;
5959

60+
use std::borrow::Cow;
6061
use std::cmp;
6162
use std::mem;
6263
use std::path::{self, Path, PathBuf};
@@ -228,7 +229,7 @@ pub struct Parser<'a> {
228229
prev_token_kind: PrevTokenKind,
229230
pub restrictions: Restrictions,
230231
/// Used to determine the path to externally loaded source files
231-
pub directory: Directory,
232+
pub directory: Directory<'a>,
232233
/// Whether to parse sub-modules in other files.
233234
pub recurse_into_file_modules: bool,
234235
/// Name of the root module this parser originated from. If `None`, then the
@@ -535,7 +536,7 @@ enum TokenExpectType {
535536
impl<'a> Parser<'a> {
536537
pub fn new(sess: &'a ParseSess,
537538
tokens: TokenStream,
538-
directory: Option<Directory>,
539+
directory: Option<Directory<'a>>,
539540
recurse_into_file_modules: bool,
540541
desugar_doc_comments: bool)
541542
-> Self {
@@ -549,7 +550,7 @@ impl<'a> Parser<'a> {
549550
restrictions: Restrictions::empty(),
550551
recurse_into_file_modules,
551552
directory: Directory {
552-
path: PathBuf::new(),
553+
path: Cow::from(PathBuf::new()),
553554
ownership: DirectoryOwnership::Owned { relative: None }
554555
},
555556
root_module_name: None,
@@ -572,9 +573,9 @@ impl<'a> Parser<'a> {
572573
if let Some(directory) = directory {
573574
parser.directory = directory;
574575
} else if !parser.span.source_equal(&DUMMY_SP) {
575-
if let FileName::Real(path) = sess.codemap().span_to_unmapped_path(parser.span) {
576-
parser.directory.path = path;
577-
parser.directory.path.pop();
576+
if let FileName::Real(mut path) = sess.codemap().span_to_unmapped_path(parser.span) {
577+
path.pop();
578+
parser.directory.path = Cow::from(path);
578579
}
579580
}
580581

@@ -6000,10 +6001,10 @@ impl<'a> Parser<'a> {
60006001

60016002
fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) {
60026003
if let Some(path) = attr::first_attr_value_str_by_name(attrs, "path") {
6003-
self.directory.path.push(&path.as_str());
6004+
self.directory.path.to_mut().push(&path.as_str());
60046005
self.directory.ownership = DirectoryOwnership::Owned { relative: None };
60056006
} else {
6006-
self.directory.path.push(&id.name.as_str());
6007+
self.directory.path.to_mut().push(&id.name.as_str());
60076008
}
60086009
}
60096010

src/libsyntax/tokenstream.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use print::pprust;
3131
use serialize::{Decoder, Decodable, Encoder, Encodable};
3232
use util::RcSlice;
3333

34+
use std::borrow::Cow;
3435
use std::{fmt, iter, mem};
3536
use std::hash::{self, Hash};
3637

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

0 commit comments

Comments
 (0)