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

perf(scope): Cache the Scope.watch AST. #1173

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/core/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,14 @@ class Scope {
}
}

AST ast = rootScope._astParser(expression,
formatters: formatters, collection: collection);
String astKey =
"${collection ? "C" : "."}${formatters == null ? "." : formatters.hashCode}$expression";
AST ast = rootScope.astCache[astKey];
if (ast == null) {
ast = rootScope.astCache[astKey] =
rootScope._astParser(expression,
formatters: formatters, collection: collection);
}

return watch = watchAST(ast, fn, canChangeModel: canChangeModel);
}
Expand Down Expand Up @@ -568,6 +574,9 @@ class RootScope extends Scope {
final ScopeDigestTTL _ttl;
final VmTurnZone _zone;

// For Scope.watch().
final Map<String, AST> astCache = new HashMap<String, AST>();

_FunctionChain _runAsyncHead, _runAsyncTail;
_FunctionChain _domWriteHead, _domWriteTail;
_FunctionChain _domReadHead, _domReadTail;
Expand Down Expand Up @@ -629,7 +638,7 @@ class RootScope extends Scope {

RootScope(Object context, Parser parser, ASTParser astParser,
FieldGetterFactory fieldGetterFactory, FormatterMap formatters, this._exceptionHandler,
ScopeDigestTTL ttl, this._zone, ScopeStats _scopeStats)
ScopeDigestTTL ttl, this._zone, ScopeStats _scopeStats, CacheRegister cacheRegister)
: _scopeStats = _scopeStats,
_ttl = ttl,
_parser = parser,
Expand All @@ -650,6 +659,7 @@ class RootScope extends Scope {
for(num i = 0; i <= _ttl.ttl; i++) {
_digestTags.add(new UserTag('NgDigest${i}'));
}
cacheRegister.registerCache("ScopeWatchASTs", astCache);
}

RootScope get rootScope => this;
Expand Down