Index: src/harmony-templates.js |
diff --git a/src/harmony-templates.js b/src/harmony-templates.js |
index 712d8d3a12bbb996d1bd838c09d81fd9d180e3e6..dbd0f504ea83dea749df802137e3a7ebcb3f6ace 100644 |
--- a/src/harmony-templates.js |
+++ b/src/harmony-templates.js |
@@ -4,13 +4,57 @@ |
'use strict'; |
-function GetTemplateCallSite(siteObj, rawStrings) { |
- // TODO(caitp): ensure same template callsite is used for subsequent tag calls |
+var callSiteCache = new $Map; |
+ |
+function SameCallSiteElements(rawStrings, other) { |
+ var length = rawStrings.length; |
+ var other = other.raw; |
+ |
+ if (length !== other.length) return false; |
+ |
+ for (var i = 0; i < length; ++i) { |
+ if (rawStrings[i] !== other[i]) return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+ |
+function GetCachedCallSite(siteObj, hash) { |
+ var obj = %MapGet(callSiteCache, hash); |
+ |
+ if (IS_UNDEFINED(obj)) return; |
+ |
+ var length = obj.length; |
+ for (var i = 0; i < length; ++i) { |
+ if (SameCallSiteElements(siteObj, obj[i])) return obj[i]; |
+ } |
+} |
+ |
+ |
+function SetCachedCallSite(siteObj, hash) { |
+ var obj = %MapGet(callSiteCache, hash); |
+ |
+ if (IS_UNDEFINED(obj)) { |
+ %MapSet(callSiteCache, hash, new InternalArray(siteObj)); |
arv (Not doing code reviews)
2014/11/19 21:38:57
Lets not use the strange overloading of params for
|
+ } else { |
+ // Unlikely hash collision case. |
+ obj.push(siteObj); |
+ } |
+ |
+ return siteObj; |
+} |
+ |
+ |
+function GetTemplateCallSite(siteObj, rawStrings, hash) { |
+ var cached = GetCachedCallSite(rawStrings, hash); |
+ |
+ if (!IS_UNDEFINED(cached)) return cached; |
%AddNamedProperty(siteObj, "raw", %ObjectFreeze(rawStrings), |
READ_ONLY | DONT_ENUM | DONT_DELETE); |
- return %ObjectFreeze(siteObj); |
+ return SetCachedCallSite(%ObjectFreeze(siteObj), hash); |
} |