Skip to content

Commit b9504d2

Browse files
author
blake2-ppc
committed
syntax: Shrink enum Token and enum nonterminal
`enum Token` was 192 bytes (64-bit), as pointed out by pnkfelix; the only bloating variant being `INTERPOLATED(nonterminal)`. Updating `enum nonterminal` to use ~ where variants included big types, shrunk size_of(Token) to 32 bytes (64-bit). I am unsure if the `nt_ident` variant should have an indirection, with ast::ident being only 16 bytes (64-bit), but without this, enum Token would be 40 bytes. A dumb benchmark says that compilation time is unchanged, while peak memory usage for compiling std.rs is down 3% Before:: $ time ./x86_64-unknown-linux-gnu/stage1/bin/rustc --cfg stage1 src/libstd/std.rs 19.00user 0.39system 0:19.41elapsed 99%CPU (0avgtext+0avgdata 627820maxresident)k 0inputs+28896outputs (0major+228665minor)pagefaults 0swaps $ time ./x86_64-unknown-linux-gnu/stage1/bin/rustc -O --cfg stage1 src/libstd/std.rs 31.64user 0.34system 0:32.02elapsed 99%CPU (0avgtext+0avgdata 629876maxresident)k 0inputs+22432outputs (0major+229411minor)pagefaults 0swaps After:: $ time ./x86_64-unknown-linux-gnu/stage1/bin/rustc --cfg stage1 src/libstd/std.rs 19.07user 0.45system 0:19.55elapsed 99%CPU (0avgtext+0avgdata 609384maxresident)k 0inputs+28896outputs (0major+221997minor)pagefaults 0swaps $ time ./x86_64-unknown-linux-gnu/stage1/bin/rustc -O --cfg stage1 src/libstd/std.rs 31.90user 0.34system 0:32.28elapsed 99%CPU (0avgtext+0avgdata 612080maxresident)k 0inputs+22432outputs (0major+223726minor)pagefaults 0swaps
1 parent 74efdf6 commit b9504d2

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,18 +418,18 @@ pub fn parse_nt(p: &Parser, name: &str) -> nonterminal {
418418
Some(i) => token::nt_item(i),
419419
None => p.fatal("expected an item keyword")
420420
},
421-
"block" => token::nt_block(p.parse_block()),
421+
"block" => token::nt_block(~p.parse_block()),
422422
"stmt" => token::nt_stmt(p.parse_stmt(~[])),
423423
"pat" => token::nt_pat(p.parse_pat()),
424424
"expr" => token::nt_expr(p.parse_expr()),
425-
"ty" => token::nt_ty(p.parse_ty(false /* no need to disambiguate*/)),
425+
"ty" => token::nt_ty(~p.parse_ty(false /* no need to disambiguate*/)),
426426
// this could be handled like a token, since it is one
427427
"ident" => match *p.token {
428-
token::IDENT(sn,b) => { p.bump(); token::nt_ident(sn,b) }
428+
token::IDENT(sn,b) => { p.bump(); token::nt_ident(~sn,b) }
429429
_ => p.fatal(~"expected ident, found "
430430
+ token::to_str(get_ident_interner(), p.token))
431431
},
432-
"path" => token::nt_path(p.parse_path_with_tps(false)),
432+
"path" => token::nt_path(~p.parse_path_with_tps(false)),
433433
"tt" => {
434434
*p.quote_depth += 1u; //but in theory, non-quoted tts might be useful
435435
let res = token::nt_tt(@p.parse_token_tree());

src/libsyntax/ext/tt/transcribe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan {
279279
/* sidestep the interpolation tricks for ident because
280280
(a) idents can be in lots of places, so it'd be a pain
281281
(b) we actually can, since it's a token. */
282-
matched_nonterminal(nt_ident(sn,b)) => {
282+
matched_nonterminal(nt_ident(~sn,b)) => {
283283
r.cur_span = sp; r.cur_tok = IDENT(sn,b);
284284
r.stack.idx += 1u;
285285
return ret_val;

src/libsyntax/parse/parser.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ macro_rules! maybe_whole_expr (
144144
Some($p.mk_expr(
145145
($p).span.lo,
146146
($p).span.hi,
147-
expr_path(/* bad */ (*pt).clone())))
147+
expr_path(/* bad */ (**pt).clone())))
148148
}
149149
_ => None
150150
};
@@ -233,8 +233,8 @@ macro_rules! maybe_whole (
233233
_ => None
234234
};
235235
match __found__ {
236-
Some(INTERPOLATED(token::$constructor(x))) => {
237-
return (~[], x.clone())
236+
Some(INTERPOLATED(token::$constructor(ref x))) => {
237+
return (~[], (**x).clone())
238238
}
239239
_ => {}
240240
}
@@ -937,7 +937,7 @@ impl Parser {
937937
// Useless second parameter for compatibility with quasiquote macros.
938938
// Bleh!
939939
pub fn parse_ty(&self, _: bool) -> Ty {
940-
maybe_whole!(self, nt_ty);
940+
maybe_whole!(deref self, nt_ty);
941941

942942
let lo = self.span.lo;
943943

@@ -1291,7 +1291,7 @@ impl Parser {
12911291

12921292
// parse a path that doesn't have type parameters attached
12931293
pub fn parse_path_without_tps(&self) -> ast::Path {
1294-
maybe_whole!(self, nt_path);
1294+
maybe_whole!(deref self, nt_path);
12951295
let (ids,is_global,sp) = self.parse_path();
12961296
ast::Path { span: sp,
12971297
global: is_global,
@@ -1304,7 +1304,7 @@ impl Parser {
13041304
before_tps: Option<&fn()>) -> ast::Path {
13051305
debug!("parse_path_with_tps(colons=%b)", colons);
13061306

1307-
maybe_whole!(self, nt_path);
1307+
maybe_whole!(deref self, nt_path);
13081308
let lo = self.span.lo;
13091309
let path = self.parse_path_without_tps();
13101310
if colons && !self.eat(&token::MOD_SEP) {
@@ -3098,7 +3098,7 @@ impl Parser {
30983098

30993099
// parse a block. No inner attrs are allowed.
31003100
pub fn parse_block(&self) -> Block {
3101-
maybe_whole!(self, nt_block);
3101+
maybe_whole!(deref self, nt_block);
31023102

31033103
let lo = self.span.lo;
31043104
if self.eat_keyword(keywords::Unsafe) {

src/libsyntax/parse/token.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ pub enum Token {
9999
/// For interpolation during macro expansion.
100100
pub enum nonterminal {
101101
nt_item(@ast::item),
102-
nt_block(ast::Block),
102+
nt_block(~ast::Block),
103103
nt_stmt(@ast::stmt),
104104
nt_pat( @ast::pat),
105105
nt_expr(@ast::expr),
106-
nt_ty( ast::Ty),
107-
nt_ident(ast::ident, bool),
108-
nt_path( ast::Path),
106+
nt_ty( ~ast::Ty),
107+
nt_ident(~ast::ident, bool),
108+
nt_path(~ast::Path),
109109
nt_tt( @ast::token_tree), //needs @ed to break a circularity
110110
nt_matchers(~[ast::matcher])
111111
}

0 commit comments

Comments
 (0)