Skip to content

Commit 695e559

Browse files
jbdeboerDiana Salsbury
authored and
Diana Salsbury
committed
perf(scope): Cache the Scope.watch AST.
Makes NgClass 20% faster. This will effect any Scope.watch() calls. Closes dart-archive#1173
1 parent 84da1d5 commit 695e559

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

lib/core/scope.dart

+13-3
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,14 @@ class Scope {
254254
}
255255
}
256256

257-
AST ast = rootScope._astParser(expression,
258-
formatters: formatters, collection: collection);
257+
String astKey =
258+
"${collection ? "C" : "."}${formatters == null ? "." : formatters.hashCode}$expression";
259+
AST ast = rootScope.astCache[astKey];
260+
if (ast == null) {
261+
ast = rootScope.astCache[astKey] =
262+
rootScope._astParser(expression,
263+
formatters: formatters, collection: collection);
264+
}
259265

260266
return watch = watchAST(ast, fn, canChangeModel: canChangeModel);
261267
}
@@ -568,6 +574,9 @@ class RootScope extends Scope {
568574
final ScopeDigestTTL _ttl;
569575
final VmTurnZone _zone;
570576

577+
// For Scope.watch().
578+
final Map<String, AST> astCache = new HashMap<String, AST>();
579+
571580
_FunctionChain _runAsyncHead, _runAsyncTail;
572581
_FunctionChain _domWriteHead, _domWriteTail;
573582
_FunctionChain _domReadHead, _domReadTail;
@@ -629,7 +638,7 @@ class RootScope extends Scope {
629638

630639
RootScope(Object context, Parser parser, ASTParser astParser,
631640
FieldGetterFactory fieldGetterFactory, FormatterMap formatters, this._exceptionHandler,
632-
ScopeDigestTTL ttl, this._zone, ScopeStats _scopeStats)
641+
ScopeDigestTTL ttl, this._zone, ScopeStats _scopeStats, CacheRegister cacheRegister)
633642
: _scopeStats = _scopeStats,
634643
_ttl = ttl,
635644
_parser = parser,
@@ -650,6 +659,7 @@ class RootScope extends Scope {
650659
for(num i = 0; i <= _ttl.ttl; i++) {
651660
_digestTags.add(new UserTag('NgDigest${i}'));
652661
}
662+
cacheRegister.registerCache("ScopeWatchASTs", astCache);
653663
}
654664

655665
RootScope get rootScope => this;

0 commit comments

Comments
 (0)