Skip to content

Commit fd2b247

Browse files
jensjohaCommit Queue
authored andcommitted
[scanner/parser] Naturally avoid bounds check in SimpleToken.type
By making the constant array have size 256 we "naturally" avoid the bounds check in SimpleToken.type. Stats for 10 runs each before/after: ``` page-faults:u: -0.1222% +/- 0.0686% (-127.40 +/- 71.50) instructions:u: -0.2589% +/- 0.0007% (-55273529.20 +/- 147748.67) ``` Stats for 100 runs each before/after: ``` msec task-clock:u: -1.6562% +/- 0.7810% (-71.89 +/- 33.90) page-faults:u: -0.1251% +/- 0.0186% (-130.44 +/- 19.36) cycles:u: -1.6881% +/- 0.8129% (-302264895.65 +/- 145560853.90) instructions:u: -0.2593% +/- 0.0002% (-55345822.71 +/- 36070.85) branch-misses:u: -5.9592% +/- 2.5980% (-4206550.69 +/- 1833947.73) seconds time elapsed: -1.6470% +/- 0.7804% (-0.07 +/- 0.03) seconds user: -1.6494% +/- 0.8506% (-0.07 +/- 0.04) ``` To my surprise this also reduced the size of the aot compiled CFE by ~20 kb. Change-Id: I149eda23a0b88f06953156021389bd85728e97ac Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/387920 Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent 595662b commit fd2b247

File tree

1 file changed

+112
-5
lines changed

1 file changed

+112
-5
lines changed

pkg/_fe_analyzer_shared/lib/src/scanner/token.dart

Lines changed: 112 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ class SimpleToken implements Token {
546546
*/
547547
@override
548548
void set offset(int value) {
549-
assert(_tokenTypesByIndex.length < 256);
549+
assert(_tokenTypesByIndex.length == 256);
550550
// See https://github.com/dart-lang/sdk/issues/50048 for details.
551551
assert(value >= -1);
552552
_typeAndOffset = ((value + 1) << 8) | (_typeAndOffset & 0xff);
@@ -580,7 +580,7 @@ class SimpleToken implements Token {
580580
assert(offset >= -1);
581581

582582
// Assert the encoding of the [type] is fully reversible.
583-
assert(type.index < 256 && _tokenTypesByIndex.length < 256);
583+
assert(type.index < 256 && _tokenTypesByIndex.length == 256);
584584
assert(identical(offset, this.offset));
585585
assert(identical(type, this.type), '$type != ${this.type}');
586586

@@ -1226,6 +1226,9 @@ class TokenClass {
12261226
* Clients may not extend, implement or mix-in this class.
12271227
*/
12281228
class TokenType {
1229+
static const TokenType UNUSED = const TokenType(
1230+
/* index = */ 255, '', 'UNUSED', NO_PRECEDENCE, EOF_TOKEN);
1231+
12291232
/**
12301233
* The type of the token that marks the start or end of the input.
12311234
*/
@@ -1875,9 +1878,11 @@ class TokenType {
18751878
String toString() => name;
18761879
}
18771880

1878-
/**
1879-
* Constant list of [TokenType] and [Keyword] ordered by index.
1880-
*/
1881+
/// Constant list of [TokenType] and [Keyword] ordered by index.
1882+
///
1883+
/// This list should always have length 256 to avoid bounds checks in
1884+
/// SimpleToken.type:
1885+
/// DartDocTest(_tokenTypesByIndex.length, 256)
18811886
const List<TokenType> _tokenTypesByIndex = [
18821887
TokenType.EOF,
18831888
TokenType.DOUBLE,
@@ -2034,4 +2039,106 @@ const List<TokenType> _tokenTypesByIndex = [
20342039
Keyword.WHILE,
20352040
Keyword.WITH,
20362041
Keyword.YIELD,
2042+
// Fill to length 256 to avoid bounds check in SimpleToken.type.
2043+
TokenType.UNUSED,
2044+
TokenType.UNUSED,
2045+
TokenType.UNUSED,
2046+
TokenType.UNUSED,
2047+
TokenType.UNUSED,
2048+
TokenType.UNUSED,
2049+
TokenType.UNUSED,
2050+
TokenType.UNUSED,
2051+
TokenType.UNUSED,
2052+
TokenType.UNUSED,
2053+
TokenType.UNUSED,
2054+
TokenType.UNUSED,
2055+
TokenType.UNUSED,
2056+
TokenType.UNUSED,
2057+
TokenType.UNUSED,
2058+
TokenType.UNUSED,
2059+
TokenType.UNUSED,
2060+
TokenType.UNUSED,
2061+
TokenType.UNUSED,
2062+
TokenType.UNUSED,
2063+
TokenType.UNUSED,
2064+
TokenType.UNUSED,
2065+
TokenType.UNUSED,
2066+
TokenType.UNUSED,
2067+
TokenType.UNUSED,
2068+
TokenType.UNUSED,
2069+
TokenType.UNUSED,
2070+
TokenType.UNUSED,
2071+
TokenType.UNUSED,
2072+
TokenType.UNUSED,
2073+
TokenType.UNUSED,
2074+
TokenType.UNUSED,
2075+
TokenType.UNUSED,
2076+
TokenType.UNUSED,
2077+
TokenType.UNUSED,
2078+
TokenType.UNUSED,
2079+
TokenType.UNUSED,
2080+
TokenType.UNUSED,
2081+
TokenType.UNUSED,
2082+
TokenType.UNUSED,
2083+
TokenType.UNUSED,
2084+
TokenType.UNUSED,
2085+
TokenType.UNUSED,
2086+
TokenType.UNUSED,
2087+
TokenType.UNUSED,
2088+
TokenType.UNUSED,
2089+
TokenType.UNUSED,
2090+
TokenType.UNUSED,
2091+
TokenType.UNUSED,
2092+
TokenType.UNUSED,
2093+
TokenType.UNUSED,
2094+
TokenType.UNUSED,
2095+
TokenType.UNUSED,
2096+
TokenType.UNUSED,
2097+
TokenType.UNUSED,
2098+
TokenType.UNUSED,
2099+
TokenType.UNUSED,
2100+
TokenType.UNUSED,
2101+
TokenType.UNUSED,
2102+
TokenType.UNUSED,
2103+
TokenType.UNUSED,
2104+
TokenType.UNUSED,
2105+
TokenType.UNUSED,
2106+
TokenType.UNUSED,
2107+
TokenType.UNUSED,
2108+
TokenType.UNUSED,
2109+
TokenType.UNUSED,
2110+
TokenType.UNUSED,
2111+
TokenType.UNUSED,
2112+
TokenType.UNUSED,
2113+
TokenType.UNUSED,
2114+
TokenType.UNUSED,
2115+
TokenType.UNUSED,
2116+
TokenType.UNUSED,
2117+
TokenType.UNUSED,
2118+
TokenType.UNUSED,
2119+
TokenType.UNUSED,
2120+
TokenType.UNUSED,
2121+
TokenType.UNUSED,
2122+
TokenType.UNUSED,
2123+
TokenType.UNUSED,
2124+
TokenType.UNUSED,
2125+
TokenType.UNUSED,
2126+
TokenType.UNUSED,
2127+
TokenType.UNUSED,
2128+
TokenType.UNUSED,
2129+
TokenType.UNUSED,
2130+
TokenType.UNUSED,
2131+
TokenType.UNUSED,
2132+
TokenType.UNUSED,
2133+
TokenType.UNUSED,
2134+
TokenType.UNUSED,
2135+
TokenType.UNUSED,
2136+
TokenType.UNUSED,
2137+
TokenType.UNUSED,
2138+
TokenType.UNUSED,
2139+
TokenType.UNUSED,
2140+
TokenType.UNUSED,
2141+
TokenType.UNUSED,
2142+
TokenType.UNUSED,
2143+
TokenType.UNUSED,
20372144
];

0 commit comments

Comments
 (0)