Chromium Code Reviews| Index: src/harmony-templates.js |
| diff --git a/src/harmony-templates.js b/src/harmony-templates.js |
| index 712d8d3a12bbb996d1bd838c09d81fd9d180e3e6..b7e94a2eec14b38e5c0338c6041afb477c199d59 100644 |
| --- a/src/harmony-templates.js |
| +++ b/src/harmony-templates.js |
| @@ -4,13 +4,60 @@ |
| '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); |
|
caitp (gmail)
2014/11/19 12:14:11
It might be a good idea to use LRU caching or some
|
| + |
| + 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, [siteObj]); |
| + } else { |
| + // Unlikely hash collision case. |
| + var arrays = new InternalArray(2); |
| + args[0] = obj; |
| + args[1] = siteObj; |
| + %MapSet(callSiteCache, hash, %ArrayConcat(args)); |
|
arv (Not doing code reviews)
2014/11/19 17:18:45
The following looks cleaner to me.
var array = %M
|
| + } |
| + |
| + 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); |
| } |