blob: fffe5da71da80ed2e1006ab4b37b5a29a8556afc [file] [log] [blame]
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001// Copyright 2011 the V8 project authors. All rights reserved.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +000030#include "assembler.h"
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000031#include "compilation-cache.h"
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000032#include "serialize.h"
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000033
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000036
ager@chromium.org5aa501c2009-06-23 07:57:28 +000037
38// The number of generations for each sub cache.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000039// The number of ScriptGenerations is carefully chosen based on histograms.
40// See issue 458: http://code.google.com/p/v8/issues/detail?id=458
ager@chromium.org5aa501c2009-06-23 07:57:28 +000041static const int kScriptGenerations = 5;
42static const int kEvalGlobalGenerations = 2;
43static const int kEvalContextualGenerations = 2;
44static const int kRegExpGenerations = 2;
45
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000046// Initial size of each compilation cache table allocated.
ager@chromium.org5aa501c2009-06-23 07:57:28 +000047static const int kInitialCacheSize = 64;
48
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000049
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000050CompilationCache::CompilationCache(Isolate* isolate)
51 : isolate_(isolate),
52 script_(isolate, kScriptGenerations),
53 eval_global_(isolate, kEvalGlobalGenerations),
54 eval_contextual_(isolate, kEvalContextualGenerations),
55 reg_exp_(isolate, kRegExpGenerations),
ricow@chromium.org4f693d62011-07-04 14:01:31 +000056 enabled_(true) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000057 CompilationSubCache* subcaches[kSubCacheCount] =
58 {&script_, &eval_global_, &eval_contextual_, &reg_exp_};
59 for (int i = 0; i < kSubCacheCount; ++i) {
60 subcaches_[i] = subcaches[i];
ager@chromium.org5aa501c2009-06-23 07:57:28 +000061 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +000062}
63
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000064
ricow@chromium.org4f693d62011-07-04 14:01:31 +000065CompilationCache::~CompilationCache() {}
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000066
67
68static Handle<CompilationCacheTable> AllocateTable(Isolate* isolate, int size) {
69 CALL_HEAP_FUNCTION(isolate,
ulan@chromium.org6e196bf2013-03-13 09:38:22 +000070 CompilationCacheTable::Allocate(isolate->heap(), size),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000071 CompilationCacheTable);
72}
73
74
ager@chromium.org5aa501c2009-06-23 07:57:28 +000075Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) {
76 ASSERT(generation < generations_);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000077 Handle<CompilationCacheTable> result;
ager@chromium.org5aa501c2009-06-23 07:57:28 +000078 if (tables_[generation]->IsUndefined()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000079 result = AllocateTable(isolate(), kInitialCacheSize);
ager@chromium.org5aa501c2009-06-23 07:57:28 +000080 tables_[generation] = *result;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000081 } else {
ager@chromium.org5aa501c2009-06-23 07:57:28 +000082 CompilationCacheTable* table =
83 CompilationCacheTable::cast(tables_[generation]);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000084 result = Handle<CompilationCacheTable>(table, isolate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000085 }
86 return result;
87}
88
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +000089
ager@chromium.org5aa501c2009-06-23 07:57:28 +000090void CompilationSubCache::Age() {
91 // Age the generations implicitly killing off the oldest.
92 for (int i = generations_ - 1; i > 0; i--) {
93 tables_[i] = tables_[i - 1];
ager@chromium.org381abbb2009-02-25 13:23:22 +000094 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +000095
96 // Set the first generation as unborn.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000097 tables_[0] = isolate()->heap()->undefined_value();
ager@chromium.org381abbb2009-02-25 13:23:22 +000098}
99
100
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000101void CompilationSubCache::IterateFunctions(ObjectVisitor* v) {
danno@chromium.org72204d52012-10-31 10:02:10 +0000102 Object* undefined = isolate()->heap()->undefined_value();
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000103 for (int i = 0; i < generations_; i++) {
104 if (tables_[i] != undefined) {
105 reinterpret_cast<CompilationCacheTable*>(tables_[i])->IterateElements(v);
106 }
107 }
108}
109
110
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000111void CompilationSubCache::Iterate(ObjectVisitor* v) {
112 v->VisitPointers(&tables_[0], &tables_[generations_]);
113}
114
115
116void CompilationSubCache::Clear() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000117 MemsetPointer(tables_, isolate()->heap()->undefined_value(), generations_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000118}
119
120
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000121void CompilationSubCache::Remove(Handle<SharedFunctionInfo> function_info) {
122 // Probe the script generation tables. Make sure not to leak handles
123 // into the caller's handle scope.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000124 { HandleScope scope(isolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000125 for (int generation = 0; generation < generations(); generation++) {
126 Handle<CompilationCacheTable> table = GetTable(generation);
127 table->Remove(*function_info);
128 }
129 }
130}
131
132
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000133CompilationCacheScript::CompilationCacheScript(Isolate* isolate,
134 int generations)
135 : CompilationSubCache(isolate, generations),
136 script_histogram_(NULL),
137 script_histogram_initialized_(false) { }
138
139
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000140// We only re-use a cached function for some script source code if the
141// script originates from the same place. This is to avoid issues
142// when reporting errors, etc.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000143bool CompilationCacheScript::HasOrigin(
144 Handle<SharedFunctionInfo> function_info,
145 Handle<Object> name,
146 int line_offset,
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000147 int column_offset,
148 bool is_shared_cross_origin) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000149 Handle<Script> script =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000150 Handle<Script>(Script::cast(function_info->script()), isolate());
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000151 // If the script name isn't set, the boilerplate script should have
152 // an undefined name to have the same origin.
153 if (name.is_null()) {
154 return script->name()->IsUndefined();
155 }
156 // Do the fast bailout checks first.
157 if (line_offset != script->line_offset()->value()) return false;
158 if (column_offset != script->column_offset()->value()) return false;
159 // Check that both names are strings. If not, no match.
160 if (!name->IsString() || !script->name()->IsString()) return false;
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000161 // Were both scripts tagged by the embedder as being shared cross-origin?
162 if (is_shared_cross_origin != script->is_shared_cross_origin()) return false;
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000163 // Compare the two name strings for equality.
164 return String::cast(*name)->Equals(String::cast(script->name()));
165}
166
167
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000168// TODO(245): Need to allow identical code from different contexts to
169// be cached in the same script generation. Currently the first use
170// will be cached, but subsequent code from different source / line
171// won't.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000172Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(
173 Handle<String> source,
174 Handle<Object> name,
175 int line_offset,
176 int column_offset,
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000177 bool is_shared_cross_origin,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000178 Handle<Context> context) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000179 Object* result = NULL;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000180 int generation;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000181
182 // Probe the script generation tables. Make sure not to leak handles
183 // into the caller's handle scope.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000184 { HandleScope scope(isolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000185 for (generation = 0; generation < generations(); generation++) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000186 Handle<CompilationCacheTable> table = GetTable(generation);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000187 Handle<Object> probe(table->Lookup(*source, *context), isolate());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000188 if (probe->IsSharedFunctionInfo()) {
189 Handle<SharedFunctionInfo> function_info =
190 Handle<SharedFunctionInfo>::cast(probe);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000191 // Break when we've found a suitable shared function info that
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000192 // matches the origin.
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000193 if (HasOrigin(function_info,
194 name,
195 line_offset,
196 column_offset,
197 is_shared_cross_origin)) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000198 result = *function_info;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000199 break;
200 }
201 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000202 }
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000203 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000204
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000205 if (!script_histogram_initialized_) {
206 script_histogram_ = isolate()->stats_table()->CreateHistogram(
207 "V8.ScriptCache",
208 0,
209 kScriptGenerations,
210 kScriptGenerations + 1);
211 script_histogram_initialized_ = true;
212 }
kasperl@chromium.org31ca8882009-05-26 09:36:02 +0000213
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000214 if (script_histogram_ != NULL) {
kasperl@chromium.org31ca8882009-05-26 09:36:02 +0000215 // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000216 isolate()->stats_table()->AddHistogramSample(script_histogram_, generation);
kasperl@chromium.org31ca8882009-05-26 09:36:02 +0000217 }
218
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000219 // Once outside the manacles of the handle scope, we need to recheck
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000220 // to see if we actually found a cached script. If so, we return a
221 // handle created in the caller's handle scope.
222 if (result != NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000223 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result),
224 isolate());
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000225 ASSERT(HasOrigin(shared,
226 name,
227 line_offset,
228 column_offset,
229 is_shared_cross_origin));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000230 // If the script was found in a later generation, we promote it to
231 // the first generation to let it survive longer in the cache.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000232 if (generation != 0) Put(source, context, shared);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000233 isolate()->counters()->compilation_cache_hits()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000234 return shared;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000235 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000236 isolate()->counters()->compilation_cache_misses()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000237 return Handle<SharedFunctionInfo>::null();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000238 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000239}
240
241
lrn@chromium.org303ada72010-10-27 09:33:13 +0000242MaybeObject* CompilationCacheScript::TryTablePut(
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000243 Handle<String> source,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000244 Handle<Context> context,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000245 Handle<SharedFunctionInfo> function_info) {
246 Handle<CompilationCacheTable> table = GetFirstTable();
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000247 return table->Put(*source, *context, *function_info);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000248}
249
250
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000251Handle<CompilationCacheTable> CompilationCacheScript::TablePut(
252 Handle<String> source,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000253 Handle<Context> context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000254 Handle<SharedFunctionInfo> function_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000255 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000256 TryTablePut(source, context, function_info),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000257 CompilationCacheTable);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000258}
259
260
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000261void CompilationCacheScript::Put(Handle<String> source,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000262 Handle<Context> context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000263 Handle<SharedFunctionInfo> function_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000264 HandleScope scope(isolate());
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000265 SetFirstTable(TablePut(source, context, function_info));
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000266}
267
268
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000269Handle<SharedFunctionInfo> CompilationCacheEval::Lookup(
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000270 Handle<String> source,
271 Handle<Context> context,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000272 LanguageMode language_mode,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000273 int scope_position) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000274 // Make sure not to leak the table into the surrounding handle
275 // scope. Otherwise, we risk keeping old tables around even after
276 // having cleared the cache.
277 Object* result = NULL;
278 int generation;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000279 { HandleScope scope(isolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000280 for (generation = 0; generation < generations(); generation++) {
281 Handle<CompilationCacheTable> table = GetTable(generation);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000282 result = table->LookupEval(
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000283 *source, *context, language_mode, scope_position);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000284 if (result->IsSharedFunctionInfo()) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000285 break;
286 }
287 }
288 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000289 if (result->IsSharedFunctionInfo()) {
290 Handle<SharedFunctionInfo>
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000291 function_info(SharedFunctionInfo::cast(result), isolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000292 if (generation != 0) {
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000293 Put(source, context, function_info, scope_position);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000294 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000295 isolate()->counters()->compilation_cache_hits()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000296 return function_info;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000297 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000298 isolate()->counters()->compilation_cache_misses()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000299 return Handle<SharedFunctionInfo>::null();
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000300 }
301}
302
303
lrn@chromium.org303ada72010-10-27 09:33:13 +0000304MaybeObject* CompilationCacheEval::TryTablePut(
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000305 Handle<String> source,
306 Handle<Context> context,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000307 Handle<SharedFunctionInfo> function_info,
308 int scope_position) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000309 Handle<CompilationCacheTable> table = GetFirstTable();
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000310 return table->PutEval(*source, *context, *function_info, scope_position);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000311}
312
313
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000314Handle<CompilationCacheTable> CompilationCacheEval::TablePut(
315 Handle<String> source,
316 Handle<Context> context,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000317 Handle<SharedFunctionInfo> function_info,
318 int scope_position) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000319 CALL_HEAP_FUNCTION(isolate(),
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000320 TryTablePut(
321 source, context, function_info, scope_position),
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000322 CompilationCacheTable);
323}
324
325
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000326void CompilationCacheEval::Put(Handle<String> source,
327 Handle<Context> context,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000328 Handle<SharedFunctionInfo> function_info,
329 int scope_position) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000330 HandleScope scope(isolate());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000331 SetFirstTable(TablePut(source, context, function_info, scope_position));
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000332}
333
334
335Handle<FixedArray> CompilationCacheRegExp::Lookup(Handle<String> source,
336 JSRegExp::Flags flags) {
337 // Make sure not to leak the table into the surrounding handle
338 // scope. Otherwise, we risk keeping old tables around even after
339 // having cleared the cache.
340 Object* result = NULL;
341 int generation;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000342 { HandleScope scope(isolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000343 for (generation = 0; generation < generations(); generation++) {
344 Handle<CompilationCacheTable> table = GetTable(generation);
345 result = table->LookupRegExp(*source, flags);
346 if (result->IsFixedArray()) {
347 break;
348 }
349 }
350 }
351 if (result->IsFixedArray()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000352 Handle<FixedArray> data(FixedArray::cast(result), isolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000353 if (generation != 0) {
354 Put(source, flags, data);
355 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000356 isolate()->counters()->compilation_cache_hits()->Increment();
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000357 return data;
358 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000359 isolate()->counters()->compilation_cache_misses()->Increment();
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000360 return Handle<FixedArray>::null();
361 }
362}
363
364
lrn@chromium.org303ada72010-10-27 09:33:13 +0000365MaybeObject* CompilationCacheRegExp::TryTablePut(
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000366 Handle<String> source,
367 JSRegExp::Flags flags,
368 Handle<FixedArray> data) {
369 Handle<CompilationCacheTable> table = GetFirstTable();
370 return table->PutRegExp(*source, flags, *data);
371}
372
373
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000374Handle<CompilationCacheTable> CompilationCacheRegExp::TablePut(
375 Handle<String> source,
376 JSRegExp::Flags flags,
377 Handle<FixedArray> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000378 CALL_HEAP_FUNCTION(isolate(),
379 TryTablePut(source, flags, data),
380 CompilationCacheTable);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000381}
382
383
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000384void CompilationCacheRegExp::Put(Handle<String> source,
385 JSRegExp::Flags flags,
386 Handle<FixedArray> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000387 HandleScope scope(isolate());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000388 SetFirstTable(TablePut(source, flags, data));
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000389}
390
391
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000392void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) {
393 if (!IsEnabled()) return;
394
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000395 eval_global_.Remove(function_info);
396 eval_contextual_.Remove(function_info);
397 script_.Remove(function_info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000398}
399
400
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000401Handle<SharedFunctionInfo> CompilationCache::LookupScript(
402 Handle<String> source,
403 Handle<Object> name,
404 int line_offset,
405 int column_offset,
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000406 bool is_shared_cross_origin,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000407 Handle<Context> context) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000408 if (!IsEnabled()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000409 return Handle<SharedFunctionInfo>::null();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000410 }
411
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000412 return script_.Lookup(source,
413 name,
414 line_offset,
415 column_offset,
416 is_shared_cross_origin,
417 context);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000418}
419
420
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000421Handle<SharedFunctionInfo> CompilationCache::LookupEval(
422 Handle<String> source,
423 Handle<Context> context,
424 bool is_global,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000425 LanguageMode language_mode,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000426 int scope_position) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000427 if (!IsEnabled()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000428 return Handle<SharedFunctionInfo>::null();
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000429 }
430
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000431 Handle<SharedFunctionInfo> result;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000432 if (is_global) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000433 result = eval_global_.Lookup(
434 source, context, language_mode, scope_position);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000435 } else {
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000436 ASSERT(scope_position != RelocInfo::kNoPosition);
437 result = eval_contextual_.Lookup(
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000438 source, context, language_mode, scope_position);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000439 }
440 return result;
441}
442
443
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000444Handle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source,
445 JSRegExp::Flags flags) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000446 if (!IsEnabled()) {
447 return Handle<FixedArray>::null();
448 }
449
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000450 return reg_exp_.Lookup(source, flags);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000451}
452
453
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000454void CompilationCache::PutScript(Handle<String> source,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000455 Handle<Context> context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000456 Handle<SharedFunctionInfo> function_info) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000457 if (!IsEnabled()) {
458 return;
459 }
460
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000461 script_.Put(source, context, function_info);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000462}
463
464
465void CompilationCache::PutEval(Handle<String> source,
466 Handle<Context> context,
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000467 bool is_global,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000468 Handle<SharedFunctionInfo> function_info,
469 int scope_position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000470 if (!IsEnabled()) {
471 return;
472 }
473
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000474 HandleScope scope(isolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000475 if (is_global) {
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000476 eval_global_.Put(source, context, function_info, scope_position);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000477 } else {
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000478 ASSERT(scope_position != RelocInfo::kNoPosition);
479 eval_contextual_.Put(source, context, function_info, scope_position);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000480 }
ager@chromium.org381abbb2009-02-25 13:23:22 +0000481}
482
483
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000484
485void CompilationCache::PutRegExp(Handle<String> source,
486 JSRegExp::Flags flags,
487 Handle<FixedArray> data) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000488 if (!IsEnabled()) {
489 return;
490 }
491
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000492 reg_exp_.Put(source, flags, data);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000493}
494
495
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000496void CompilationCache::Clear() {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000497 for (int i = 0; i < kSubCacheCount; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000498 subcaches_[i]->Clear();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000499 }
500}
501
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000502
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000503void CompilationCache::Iterate(ObjectVisitor* v) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000504 for (int i = 0; i < kSubCacheCount; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000505 subcaches_[i]->Iterate(v);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000506 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000507}
508
509
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000510void CompilationCache::IterateFunctions(ObjectVisitor* v) {
511 for (int i = 0; i < kSubCacheCount; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000512 subcaches_[i]->IterateFunctions(v);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000513 }
514}
515
516
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000517void CompilationCache::MarkCompactPrologue() {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000518 for (int i = 0; i < kSubCacheCount; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000519 subcaches_[i]->Age();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000520 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000521}
522
523
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000524void CompilationCache::Enable() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000525 enabled_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000526}
527
528
529void CompilationCache::Disable() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000530 enabled_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000531 Clear();
532}
533
534
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000535} } // namespace v8::internal