From c7c37debe4b7827234642df3c1ed378b7c3e1849 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 8 Jun 2012 17:17:31 -0700 Subject: [PATCH 1/5] syntax: Use a ring buffer instead of a dvec and save a bunch of copies. Shaves a second or two off rustc. --- src/libsyntax/parse/parser.rs | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index df27b2388e073..a6c32f99a7243 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -70,7 +70,9 @@ class parser { let mut token: token::token; let mut span: span; let mut last_span: span; - let buffer: dvec<{tok: token::token, span: span}>; + let mut buffer: [mut {tok: token::token, span: span}]/4; + let mut buffer_start: int; + let mut buffer_end: int; let mut restriction: restriction; let reader: reader; let keywords: hashmap; @@ -86,7 +88,14 @@ class parser { self.token = tok0.tok; self.span = span0; self.last_span = span0; - self.buffer = dvec::dvec(); + self.buffer = [mut + {tok: tok0.tok, span: span0}, + {tok: tok0.tok, span: span0}, + {tok: tok0.tok, span: span0}, + {tok: tok0.tok, span: span0} + ]/4; + self.buffer_start = 0; + self.buffer_end = 0; self.restriction = UNRESTRICTED; self.reader = rdr; self.keywords = token::keyword_table(); @@ -98,12 +107,13 @@ class parser { fn bump() { self.last_span = self.span; - if self.buffer.len() == 0u { + if self.buffer_start == self.buffer_end { let next = lexer::next_token(self.reader); self.token = next.tok; self.span = mk_sp(next.chpos, self.reader.chpos); } else { - let next = self.buffer.shift(); + let next = self.buffer[self.buffer_start]; + self.buffer_start = (self.buffer_start + 1) & 3; self.token = next.tok; self.span = next.span; } @@ -112,13 +122,21 @@ class parser { self.token = next; self.span = mk_sp(lo, hi); } + fn buffer_length() -> int { + if self.buffer_start <= self.buffer_end { + ret self.buffer_end - self.buffer_start; + } + ret (4 - self.buffer_start) + self.buffer_end; + } fn look_ahead(distance: uint) -> token::token { - while self.buffer.len() < distance { + let dist = distance as int; + while self.buffer_length() < dist { let next = lexer::next_token(self.reader); let sp = mk_sp(next.chpos, self.reader.chpos); - self.buffer.push({tok: next.tok, span: sp}); + self.buffer[self.buffer_end] = {tok: next.tok, span: sp}; + self.buffer_end = (self.buffer_end + 1) & 3; } - ret self.buffer[distance - 1u].tok; + ret copy self.buffer[(self.buffer_start + dist - 1) & 3].tok; } fn fatal(m: str) -> ! { self.sess.span_diagnostic.span_fatal(copy self.span, m) From fbd583bde2d51dadd87f1b3c74b4723399f7e3b9 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 8 Jun 2012 17:38:12 -0700 Subject: [PATCH 2/5] core: Implement string equal natively to save a call into the shape code. Shaves a couple of seconds off rustc. --- src/libcore/str.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 6c24b65698845..1056e61fe27d7 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -595,7 +595,22 @@ Section: Comparing strings */ #[doc = "Bytewise string equality"] -pure fn eq(&&a: str, &&b: str) -> bool { a == b } +pure fn eq(&&a: str, &&b: str) -> bool { + // FIXME: This should just be "a == b" but that calls into the shape code + // :( + let a_len = a.len(); + let b_len = b.len(); + if a_len != b_len { ret false; } + let mut end = uint::min(a_len, b_len); + + let mut i = 0u; + while i < end { + if a[i] != b[i] { ret false; } + i += 1u; + } + + ret true; +} #[doc = "Bytewise less than or equal"] pure fn le(&&a: str, &&b: str) -> bool { a <= b } @@ -1874,7 +1889,7 @@ impl extensions for str { fn is_alphanumeric() -> bool { is_alphanumeric(self) } #[inline] #[doc ="Returns the size in bytes not counting the null terminator"] - fn len() -> uint { len(self) } + pure fn len() -> uint { len(self) } #[doc = " Returns a slice of the given string from the byte range [`begin`..`end`) From 46b12d3e05a5dc1feaf45520310a6dc8f1b5d13a Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 8 Jun 2012 17:56:17 -0700 Subject: [PATCH 3/5] syntax: Remove a couple of implicit copies --- src/libsyntax/parse/parser.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a6c32f99a7243..8130a1034a380 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -679,7 +679,7 @@ class parser { ret @{id: self.get_id(), node: node, span: mk_sp(lo, hi)}; } - fn mk_mac_expr(lo: uint, hi: uint, m: mac_) -> @expr { + fn mk_mac_expr(lo: uint, hi: uint, +m: mac_) -> @expr { ret @{id: self.get_id(), node: expr_mac({node: m, span: mk_sp(lo, hi)}), span: mk_sp(lo, hi)}; @@ -693,7 +693,7 @@ class parser { ret @{id: self.get_id(), node: expr_lit(lv_lit), span: span}; } - fn mk_pexpr(lo: uint, hi: uint, node: expr_) -> pexpr { + fn mk_pexpr(lo: uint, hi: uint, +node: expr_) -> pexpr { ret pexpr(self.mk_expr(lo, hi, node)); } From 8ce0215f1b7b5c70df234b87c45f457a6e0fbcc0 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 8 Jun 2012 17:57:39 -0700 Subject: [PATCH 4/5] core: "inlune" is not an attribute --- src/libcore/str.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 1056e61fe27d7..1e1e5916a92da 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -1885,7 +1885,7 @@ impl extensions for str { Alphanumeric characters are determined by `char::is_alphanumeric` "] - #[inlune] + #[inline] fn is_alphanumeric() -> bool { is_alphanumeric(self) } #[inline] #[doc ="Returns the size in bytes not counting the null terminator"] From 436b77c9f82eef997a95a3e7c73606c3bb469dc3 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 8 Jun 2012 19:15:04 -0700 Subject: [PATCH 5/5] Revert "syntax: Remove a couple of implicit copies" due to test failures This reverts commit 46b12d3e05a5dc1feaf45520310a6dc8f1b5d13a. --- src/libsyntax/parse/parser.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 8130a1034a380..a6c32f99a7243 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -679,7 +679,7 @@ class parser { ret @{id: self.get_id(), node: node, span: mk_sp(lo, hi)}; } - fn mk_mac_expr(lo: uint, hi: uint, +m: mac_) -> @expr { + fn mk_mac_expr(lo: uint, hi: uint, m: mac_) -> @expr { ret @{id: self.get_id(), node: expr_mac({node: m, span: mk_sp(lo, hi)}), span: mk_sp(lo, hi)}; @@ -693,7 +693,7 @@ class parser { ret @{id: self.get_id(), node: expr_lit(lv_lit), span: span}; } - fn mk_pexpr(lo: uint, hi: uint, +node: expr_) -> pexpr { + fn mk_pexpr(lo: uint, hi: uint, node: expr_) -> pexpr { ret pexpr(self.mk_expr(lo, hi, node)); }