Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Unified Diff: src/harmony-templates.js

Issue 742643003: Cache template literal callSiteObj (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Use existing hash algorithm, simplify cache get/set, more tests Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/parser.h » ('j') | src/parser.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
}
« no previous file with comments | « no previous file | src/parser.h » ('j') | src/parser.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698