Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
fix(parser): Add caching to the dynamic parser.
Browse files Browse the repository at this point in the history
This change cut memory allocations for some actions in the Todo
demo app by 50%.
  • Loading branch information
jbdeboer authored and mhevery committed Nov 24, 2013
1 parent 8619c8e commit 9cdd77a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
38 changes: 25 additions & 13 deletions lib/core/parser/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,36 @@ class GetterSetter {
}
return null;
}

Map<String, Function> _getter_cache = {};

Function getter(String key) {
var value = _getter_cache[key];
if (value != null) return value;
return _getter_cache[key] = _getter(key);
}

Function _getter(String key) {
var symbol = new Symbol(key);
Map<ClassMirror, Function> fieldCache = {};
return (o) {
InstanceMirror instanceMirror = reflect(o);
try {
return instanceMirror.getField(symbol).reflectee;
} on NoSuchMethodError catch (e) {
var invokeClosure = _maybeInvoke(instanceMirror, symbol);
if (invokeClosure == null) {
rethrow;
}
return invokeClosure;
} on UnsupportedError catch (e) {
var invokeClosure = _maybeInvoke(instanceMirror, symbol);
if (invokeClosure == null) {
rethrow;
ClassMirror classMirror = instanceMirror.type;
Function fn = fieldCache[classMirror];
if (fn == null) {
try {
return (fieldCache[classMirror] = (instanceMirror) => instanceMirror.getField(symbol).reflectee)(instanceMirror);
} on NoSuchMethodError catch (e) {
var value = (fieldCache[classMirror] = (instanceMirror) => _maybeInvoke(instanceMirror, symbol))(instanceMirror);
if (value == null) { rethrow; }
return value;
} on UnsupportedError catch (e) {
var value = (fieldCache[classMirror] = (instanceMirror) => _maybeInvoke(instanceMirror, symbol))(instanceMirror);
if (value == null) { rethrow; }
return value;
}
return invokeClosure;
} else {
return fn(instanceMirror);
}
};
}
Expand Down
10 changes: 10 additions & 0 deletions lib/core/parser/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,17 @@ class DynamicParser implements Parser {
String _text;
var _evalError;

Map<String, ParserAST> _cache = {};

ParserAST call(String text) {
var value = _cache[text];
if (value != null) {
return value;
}
return _cache[text] = _call(text);
}

ParserAST _call(String text) {
try {
if (text == null) text = '';
_tokenSavers = [];
Expand Down

0 comments on commit 9cdd77a

Please sign in to comment.