From 5b0b4b73a7385a1a489a8e0418fb90df7b410721 Mon Sep 17 00:00:00 2001 From: James deBoer Date: Mon, 23 Jun 2014 15:00:29 -0700 Subject: [PATCH] perf(scope): Cache the Scope.watch AST. Makes NgClass 20% faster. This will effect any Scope.watch() calls. --- lib/core/scope.dart | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/core/scope.dart b/lib/core/scope.dart index 96816331f..7f9bf5354 100644 --- a/lib/core/scope.dart +++ b/lib/core/scope.dart @@ -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); } @@ -568,6 +574,9 @@ class RootScope extends Scope { final ScopeDigestTTL _ttl; final VmTurnZone _zone; + // For Scope.watch(). + final Map astCache = new HashMap(); + _FunctionChain _runAsyncHead, _runAsyncTail; _FunctionChain _domWriteHead, _domWriteTail; _FunctionChain _domReadHead, _domReadTail; @@ -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, @@ -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;