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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | src/parser.h » ('j') | src/parser.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 function GetTemplateCallSite(siteObj, rawStrings) { 7 var callSiteCache = new $Map;
8 // TODO(caitp): ensure same template callsite is used for subsequent tag calls 8
9 function SameCallSiteElements(rawStrings, other) {
10 var length = rawStrings.length;
11 var other = other.raw;
12
13 if (length !== other.length) return false;
14
15 for (var i = 0; i < length; ++i) {
16 if (rawStrings[i] !== other[i]) return false;
17 }
18
19 return true;
20 }
21
22
23 function GetCachedCallSite(siteObj, hash) {
24 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
25
26 if (IS_UNDEFINED(obj)) return;
27
28 var length = obj.length;
29 for (var i = 0; i < length; ++i) {
30 if (SameCallSiteElements(siteObj, obj[i])) return obj[i];
31 }
32 }
33
34
35 function SetCachedCallSite(siteObj, hash) {
36 var obj = %MapGet(callSiteCache, hash);
37
38 if (IS_UNDEFINED(obj)) {
39 %MapSet(callSiteCache, hash, [siteObj]);
40 } else {
41 // Unlikely hash collision case.
42 var arrays = new InternalArray(2);
43 args[0] = obj;
44 args[1] = siteObj;
45 %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
46 }
47
48 return siteObj;
49 }
50
51
52 function GetTemplateCallSite(siteObj, rawStrings, hash) {
53 var cached = GetCachedCallSite(rawStrings, hash);
54
55 if (!IS_UNDEFINED(cached)) return cached;
9 56
10 %AddNamedProperty(siteObj, "raw", %ObjectFreeze(rawStrings), 57 %AddNamedProperty(siteObj, "raw", %ObjectFreeze(rawStrings),
11 READ_ONLY | DONT_ENUM | DONT_DELETE); 58 READ_ONLY | DONT_ENUM | DONT_DELETE);
12 59
13 return %ObjectFreeze(siteObj); 60 return SetCachedCallSite(%ObjectFreeze(siteObj), hash);
14 } 61 }
15 62
16 63
17 // ES6 Draft 10-14-2014, section 21.1.2.4 64 // ES6 Draft 10-14-2014, section 21.1.2.4
18 function StringRaw(callSite) { 65 function StringRaw(callSite) {
19 // TODO(caitp): Use rest parameters when implemented 66 // TODO(caitp): Use rest parameters when implemented
20 var numberOfSubstitutions = %_ArgumentsLength(); 67 var numberOfSubstitutions = %_ArgumentsLength();
21 var cooked = ToObject(callSite); 68 var cooked = ToObject(callSite);
22 var raw = ToObject(cooked.raw); 69 var raw = ToObject(cooked.raw);
23 var literalSegments = ToLength(raw.length); 70 var literalSegments = ToLength(raw.length);
(...skipping 15 matching lines...) Expand all
39 function ExtendStringForTemplates() { 86 function ExtendStringForTemplates() {
40 %CheckIsBootstrapping(); 87 %CheckIsBootstrapping();
41 88
42 // Set up the non-enumerable functions on the String object. 89 // Set up the non-enumerable functions on the String object.
43 InstallFunctions($String, DONT_ENUM, $Array( 90 InstallFunctions($String, DONT_ENUM, $Array(
44 "raw", StringRaw 91 "raw", StringRaw
45 )); 92 ));
46 } 93 }
47 94
48 ExtendStringForTemplates(); 95 ExtendStringForTemplates();
OLDNEW
« 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