dart2js: Failure to optimize _JsonParser constructor. #7068
Labels
P2
A bug or feature request we're likely to work on
type-enhancement
A request for a change that isn't a bug
web-dart2js
lib/json/json.dart has the following code
class _JsonParser {
static List<int> tokens;
_JsonParser(String json)
: json = json,
length = json.length {
if (tokens != null) return;
// Use a list as jump-table. It is faster than switch and if.
tokens = new List<int>(LAST_ASCII + 1);
tokens[TAB] = WHITESPACE;
tokens[NEW_LINE] = WHITESPACE;
... // 25 assignments total
tokens[CHAR_F] = FALSE_LITERAL;
}
It compiles to:
_JsonParser$1: function(json) {
if (!($._JsonParser_tokens == null))
return;
$._JsonParser_tokens = $.List_List(126);
$.indexSet($._JsonParser_tokens, 9, 32);
$.indexSet($._JsonParser_tokens, 10, 32);
...
$.indexSet($._JsonParser_tokens, 102, 102);
}
In the body of the function, global _JsonParser.tokens is an alias for the list.
If I assign the list to a local (also called tokens), the compiler does a much better job, eliminating all range checks:
_JsonParser$1: function(json) {
var tokens;
if (!($._JsonParser_tokens == null))
return;
tokens = $.List_List(126);
tokens[9] = 32;
tokens[10] = 32;
...
tokens[102] = 102;
this._JsonParser_tokens = tokens;
}
What would be required to enable the compiler to optimize the original code to the same as the hand-optimized code?
The text was updated successfully, but these errors were encountered: