OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart2js.tokens.token_map; | 5 library dart2js.tokens.token_map; |
6 | 6 |
7 import 'token.dart' show | 7 import 'token.dart' show Token; |
8 Token; | |
9 | 8 |
10 /** | 9 /** |
11 * Key class used in [TokenMap] in which the hash code for a token is based | 10 * Key class used in [TokenMap] in which the hash code for a token is based |
12 * on the [charOffset]. | 11 * on the [charOffset]. |
13 */ | 12 */ |
14 class TokenKey { | 13 class TokenKey { |
15 final Token token; | 14 final Token token; |
16 TokenKey(this.token); | 15 TokenKey(this.token); |
17 int get hashCode => token.charOffset; | 16 int get hashCode => token.charOffset; |
18 operator==(other) => other is TokenKey && token == other.token; | 17 operator ==(other) => other is TokenKey && token == other.token; |
19 } | 18 } |
20 | 19 |
21 /// Map of tokens and the first associated comment. | 20 /// Map of tokens and the first associated comment. |
22 /* | 21 /* |
23 * This implementation was chosen among several candidates for its space/time | 22 * This implementation was chosen among several candidates for its space/time |
24 * efficiency by empirical tests of running dartdoc on dartdoc itself. Time | 23 * efficiency by empirical tests of running dartdoc on dartdoc itself. Time |
25 * measurements for the use of [Compiler.commentMap]: | 24 * measurements for the use of [Compiler.commentMap]: |
26 * | 25 * |
27 * 1) Using [TokenKey] as key (this class): ~80 msec | 26 * 1) Using [TokenKey] as key (this class): ~80 msec |
28 * 2) Using [TokenKey] as key + storing a separate map in each script: ~120 msec | 27 * 2) Using [TokenKey] as key + storing a separate map in each script: ~120 msec |
29 * 3) Using [Token] as key in a [Map]: ~38000 msec | 28 * 3) Using [Token] as key in a [Map]: ~38000 msec |
30 * 4) Storing comments is new field in [Token]: ~20 msec | 29 * 4) Storing comments is new field in [Token]: ~20 msec |
31 * (Abandoned due to the increased memory usage) | 30 * (Abandoned due to the increased memory usage) |
32 * 5) Storing comments in an [Expando]: ~14000 msec | 31 * 5) Storing comments in an [Expando]: ~14000 msec |
33 * 6) Storing token/comments pairs in a linked list: ~5400 msec | 32 * 6) Storing token/comments pairs in a linked list: ~5400 msec |
34 */ | 33 */ |
35 class TokenMap { | 34 class TokenMap { |
36 Map<TokenKey,Token> comments = new Map<TokenKey,Token>(); | 35 Map<TokenKey, Token> comments = new Map<TokenKey, Token>(); |
37 | 36 |
38 Token operator[] (Token key) { | 37 Token operator [](Token key) { |
39 if (key == null) return null; | 38 if (key == null) return null; |
40 return comments[new TokenKey(key)]; | 39 return comments[new TokenKey(key)]; |
41 } | 40 } |
42 | 41 |
43 void operator[]= (Token key, Token value) { | 42 void operator []=(Token key, Token value) { |
44 if (key == null) return; | 43 if (key == null) return; |
45 comments[new TokenKey(key)] = value; | 44 comments[new TokenKey(key)] = value; |
46 } | 45 } |
47 } | 46 } |
OLD | NEW |