blob: cec10fd250aef6ce4b0410fa94ee7dd918613bea [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// 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
30#include "compilation-cache.h"
Andrei Popescu31002712010-02-23 13:46:05 +000031#include "serialize.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000032
33namespace v8 {
34namespace internal {
35
Steve Blocka7e24c12009-10-30 11:49:00 +000036// The number of sub caches covering the different types to cache.
37static const int kSubCacheCount = 4;
38
39// The number of generations for each sub cache.
Steve Block3ce2e202009-11-05 08:53:23 +000040// The number of ScriptGenerations is carefully chosen based on histograms.
41// See issue 458: http://code.google.com/p/v8/issues/detail?id=458
Steve Blocka7e24c12009-10-30 11:49:00 +000042static const int kScriptGenerations = 5;
43static const int kEvalGlobalGenerations = 2;
44static const int kEvalContextualGenerations = 2;
45static const int kRegExpGenerations = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +000046
Steve Block3ce2e202009-11-05 08:53:23 +000047// Initial size of each compilation cache table allocated.
Steve Blocka7e24c12009-10-30 11:49:00 +000048static const int kInitialCacheSize = 64;
49
Steve Block6ded16b2010-05-10 14:33:55 +010050// Index for the first generation in the cache.
51static const int kFirstGeneration = 0;
52
Steve Blocka7e24c12009-10-30 11:49:00 +000053// The compilation cache consists of several generational sub-caches which uses
54// this class as a base class. A sub-cache contains a compilation cache tables
Steve Block3ce2e202009-11-05 08:53:23 +000055// for each generation of the sub-cache. Since the same source code string has
56// different compiled code for scripts and evals, we use separate sub-caches
57// for different compilation modes, to avoid retrieving the wrong result.
Steve Blocka7e24c12009-10-30 11:49:00 +000058class CompilationSubCache {
59 public:
60 explicit CompilationSubCache(int generations): generations_(generations) {
61 tables_ = NewArray<Object*>(generations);
62 }
63
64 ~CompilationSubCache() { DeleteArray(tables_); }
65
66 // Get the compilation cache tables for a specific generation.
67 Handle<CompilationCacheTable> GetTable(int generation);
68
Steve Block6ded16b2010-05-10 14:33:55 +010069 // Accessors for first generation.
70 Handle<CompilationCacheTable> GetFirstTable() {
71 return GetTable(kFirstGeneration);
72 }
73 void SetFirstTable(Handle<CompilationCacheTable> value) {
74 ASSERT(kFirstGeneration < generations_);
75 tables_[kFirstGeneration] = *value;
76 }
77
Steve Blocka7e24c12009-10-30 11:49:00 +000078 // Age the sub-cache by evicting the oldest generation and creating a new
79 // young generation.
80 void Age();
81
82 // GC support.
83 void Iterate(ObjectVisitor* v);
84
85 // Clear this sub-cache evicting all its content.
86 void Clear();
87
88 // Number of generations in this sub-cache.
89 inline int generations() { return generations_; }
90
91 private:
92 int generations_; // Number of generations.
93 Object** tables_; // Compilation cache tables - one for each generation.
94
95 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache);
96};
97
98
99// Sub-cache for scripts.
100class CompilationCacheScript : public CompilationSubCache {
101 public:
102 explicit CompilationCacheScript(int generations)
103 : CompilationSubCache(generations) { }
104
Steve Block6ded16b2010-05-10 14:33:55 +0100105 Handle<SharedFunctionInfo> Lookup(Handle<String> source,
106 Handle<Object> name,
107 int line_offset,
108 int column_offset);
109 void Put(Handle<String> source, Handle<SharedFunctionInfo> function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000110
111 private:
Steve Block6ded16b2010-05-10 14:33:55 +0100112 // Note: Returns a new hash table if operation results in expansion.
113 Handle<CompilationCacheTable> TablePut(
114 Handle<String> source, Handle<SharedFunctionInfo> function_info);
115
116 bool HasOrigin(Handle<SharedFunctionInfo> function_info,
Steve Blocka7e24c12009-10-30 11:49:00 +0000117 Handle<Object> name,
118 int line_offset,
119 int column_offset);
120
121 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript);
122};
123
124
125// Sub-cache for eval scripts.
126class CompilationCacheEval: public CompilationSubCache {
127 public:
128 explicit CompilationCacheEval(int generations)
129 : CompilationSubCache(generations) { }
130
Steve Block6ded16b2010-05-10 14:33:55 +0100131 Handle<SharedFunctionInfo> Lookup(Handle<String> source,
132 Handle<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000133
134 void Put(Handle<String> source,
135 Handle<Context> context,
Steve Block6ded16b2010-05-10 14:33:55 +0100136 Handle<SharedFunctionInfo> function_info);
137
138 private:
139 // Note: Returns a new hash table if operation results in expansion.
140 Handle<CompilationCacheTable> TablePut(
141 Handle<String> source,
142 Handle<Context> context,
143 Handle<SharedFunctionInfo> function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000144
145 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
146};
147
148
149// Sub-cache for regular expressions.
150class CompilationCacheRegExp: public CompilationSubCache {
151 public:
152 explicit CompilationCacheRegExp(int generations)
153 : CompilationSubCache(generations) { }
154
155 Handle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags);
156
157 void Put(Handle<String> source,
158 JSRegExp::Flags flags,
159 Handle<FixedArray> data);
Steve Block6ded16b2010-05-10 14:33:55 +0100160 private:
161 // Note: Returns a new hash table if operation results in expansion.
162 Handle<CompilationCacheTable> TablePut(Handle<String> source,
163 JSRegExp::Flags flags,
164 Handle<FixedArray> data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
166 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp);
167};
168
169
170// Statically allocate all the sub-caches.
171static CompilationCacheScript script(kScriptGenerations);
172static CompilationCacheEval eval_global(kEvalGlobalGenerations);
173static CompilationCacheEval eval_contextual(kEvalContextualGenerations);
174static CompilationCacheRegExp reg_exp(kRegExpGenerations);
175static CompilationSubCache* subcaches[kSubCacheCount] =
176 {&script, &eval_global, &eval_contextual, &reg_exp};
177
178
179// Current enable state of the compilation cache.
180static bool enabled = true;
181static inline bool IsEnabled() {
182 return FLAG_compilation_cache && enabled;
183}
184
185
186static Handle<CompilationCacheTable> AllocateTable(int size) {
187 CALL_HEAP_FUNCTION(CompilationCacheTable::Allocate(size),
188 CompilationCacheTable);
189}
190
191
192Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) {
193 ASSERT(generation < generations_);
194 Handle<CompilationCacheTable> result;
195 if (tables_[generation]->IsUndefined()) {
196 result = AllocateTable(kInitialCacheSize);
197 tables_[generation] = *result;
198 } else {
199 CompilationCacheTable* table =
200 CompilationCacheTable::cast(tables_[generation]);
201 result = Handle<CompilationCacheTable>(table);
202 }
203 return result;
204}
205
206
207void CompilationSubCache::Age() {
208 // Age the generations implicitly killing off the oldest.
209 for (int i = generations_ - 1; i > 0; i--) {
210 tables_[i] = tables_[i - 1];
211 }
212
213 // Set the first generation as unborn.
214 tables_[0] = Heap::undefined_value();
215}
216
217
218void CompilationSubCache::Iterate(ObjectVisitor* v) {
219 v->VisitPointers(&tables_[0], &tables_[generations_]);
220}
221
222
223void CompilationSubCache::Clear() {
Steve Block6ded16b2010-05-10 14:33:55 +0100224 MemsetPointer(tables_, Heap::undefined_value(), generations_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000225}
226
227
228// We only re-use a cached function for some script source code if the
229// script originates from the same place. This is to avoid issues
230// when reporting errors, etc.
Steve Block6ded16b2010-05-10 14:33:55 +0100231bool CompilationCacheScript::HasOrigin(
232 Handle<SharedFunctionInfo> function_info,
233 Handle<Object> name,
234 int line_offset,
235 int column_offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 Handle<Script> script =
Steve Block6ded16b2010-05-10 14:33:55 +0100237 Handle<Script>(Script::cast(function_info->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 // If the script name isn't set, the boilerplate script should have
239 // an undefined name to have the same origin.
240 if (name.is_null()) {
241 return script->name()->IsUndefined();
242 }
243 // Do the fast bailout checks first.
244 if (line_offset != script->line_offset()->value()) return false;
245 if (column_offset != script->column_offset()->value()) return false;
246 // Check that both names are strings. If not, no match.
247 if (!name->IsString() || !script->name()->IsString()) return false;
248 // Compare the two name strings for equality.
249 return String::cast(*name)->Equals(String::cast(script->name()));
250}
251
252
253// TODO(245): Need to allow identical code from different contexts to
254// be cached in the same script generation. Currently the first use
255// will be cached, but subsequent code from different source / line
256// won't.
Steve Block6ded16b2010-05-10 14:33:55 +0100257Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(Handle<String> source,
258 Handle<Object> name,
259 int line_offset,
260 int column_offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000261 Object* result = NULL;
262 int generation;
263
264 // Probe the script generation tables. Make sure not to leak handles
265 // into the caller's handle scope.
266 { HandleScope scope;
267 for (generation = 0; generation < generations(); generation++) {
268 Handle<CompilationCacheTable> table = GetTable(generation);
269 Handle<Object> probe(table->Lookup(*source));
Steve Block6ded16b2010-05-10 14:33:55 +0100270 if (probe->IsSharedFunctionInfo()) {
271 Handle<SharedFunctionInfo> function_info =
272 Handle<SharedFunctionInfo>::cast(probe);
273 // Break when we've found a suitable shared function info that
Steve Blocka7e24c12009-10-30 11:49:00 +0000274 // matches the origin.
Steve Block6ded16b2010-05-10 14:33:55 +0100275 if (HasOrigin(function_info, name, line_offset, column_offset)) {
276 result = *function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 break;
278 }
279 }
280 }
281 }
282
283 static void* script_histogram = StatsTable::CreateHistogram(
284 "V8.ScriptCache",
285 0,
286 kScriptGenerations,
287 kScriptGenerations + 1);
288
289 if (script_histogram != NULL) {
290 // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss.
291 StatsTable::AddHistogramSample(script_histogram, generation);
292 }
293
294 // Once outside the manacles of the handle scope, we need to recheck
295 // to see if we actually found a cached script. If so, we return a
296 // handle created in the caller's handle scope.
297 if (result != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100298 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
299 ASSERT(HasOrigin(shared, name, line_offset, column_offset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 // If the script was found in a later generation, we promote it to
301 // the first generation to let it survive longer in the cache.
Steve Block6ded16b2010-05-10 14:33:55 +0100302 if (generation != 0) Put(source, shared);
Steve Blocka7e24c12009-10-30 11:49:00 +0000303 Counters::compilation_cache_hits.Increment();
Steve Block6ded16b2010-05-10 14:33:55 +0100304 return shared;
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 } else {
306 Counters::compilation_cache_misses.Increment();
Steve Block6ded16b2010-05-10 14:33:55 +0100307 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 }
309}
310
311
Steve Block6ded16b2010-05-10 14:33:55 +0100312Handle<CompilationCacheTable> CompilationCacheScript::TablePut(
313 Handle<String> source,
314 Handle<SharedFunctionInfo> function_info) {
315 CALL_HEAP_FUNCTION(GetFirstTable()->Put(*source, *function_info),
316 CompilationCacheTable);
Steve Blocka7e24c12009-10-30 11:49:00 +0000317}
318
319
Steve Block6ded16b2010-05-10 14:33:55 +0100320void CompilationCacheScript::Put(Handle<String> source,
321 Handle<SharedFunctionInfo> function_info) {
322 HandleScope scope;
323 SetFirstTable(TablePut(source, function_info));
324}
325
326
327Handle<SharedFunctionInfo> CompilationCacheEval::Lookup(
328 Handle<String> source, Handle<Context> context) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 // Make sure not to leak the table into the surrounding handle
330 // scope. Otherwise, we risk keeping old tables around even after
331 // having cleared the cache.
332 Object* result = NULL;
333 int generation;
334 { HandleScope scope;
335 for (generation = 0; generation < generations(); generation++) {
336 Handle<CompilationCacheTable> table = GetTable(generation);
337 result = table->LookupEval(*source, *context);
Steve Block6ded16b2010-05-10 14:33:55 +0100338 if (result->IsSharedFunctionInfo()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 break;
340 }
341 }
342 }
Steve Block6ded16b2010-05-10 14:33:55 +0100343 if (result->IsSharedFunctionInfo()) {
344 Handle<SharedFunctionInfo>
345 function_info(SharedFunctionInfo::cast(result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 if (generation != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +0100347 Put(source, context, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 }
349 Counters::compilation_cache_hits.Increment();
Steve Block6ded16b2010-05-10 14:33:55 +0100350 return function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +0000351 } else {
352 Counters::compilation_cache_misses.Increment();
Steve Block6ded16b2010-05-10 14:33:55 +0100353 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 }
355}
356
357
Steve Block6ded16b2010-05-10 14:33:55 +0100358Handle<CompilationCacheTable> CompilationCacheEval::TablePut(
359 Handle<String> source,
360 Handle<Context> context,
361 Handle<SharedFunctionInfo> function_info) {
362 CALL_HEAP_FUNCTION(GetFirstTable()->PutEval(*source,
363 *context,
364 *function_info),
365 CompilationCacheTable);
366}
367
368
Steve Blocka7e24c12009-10-30 11:49:00 +0000369void CompilationCacheEval::Put(Handle<String> source,
370 Handle<Context> context,
Steve Block6ded16b2010-05-10 14:33:55 +0100371 Handle<SharedFunctionInfo> function_info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +0100373 SetFirstTable(TablePut(source, context, function_info));
Steve Blocka7e24c12009-10-30 11:49:00 +0000374}
375
376
377Handle<FixedArray> CompilationCacheRegExp::Lookup(Handle<String> source,
378 JSRegExp::Flags flags) {
379 // Make sure not to leak the table into the surrounding handle
380 // scope. Otherwise, we risk keeping old tables around even after
381 // having cleared the cache.
382 Object* result = NULL;
383 int generation;
384 { HandleScope scope;
385 for (generation = 0; generation < generations(); generation++) {
386 Handle<CompilationCacheTable> table = GetTable(generation);
387 result = table->LookupRegExp(*source, flags);
388 if (result->IsFixedArray()) {
389 break;
390 }
391 }
392 }
393 if (result->IsFixedArray()) {
394 Handle<FixedArray> data(FixedArray::cast(result));
395 if (generation != 0) {
396 Put(source, flags, data);
397 }
398 Counters::compilation_cache_hits.Increment();
399 return data;
400 } else {
401 Counters::compilation_cache_misses.Increment();
402 return Handle<FixedArray>::null();
403 }
404}
405
406
Steve Block6ded16b2010-05-10 14:33:55 +0100407Handle<CompilationCacheTable> CompilationCacheRegExp::TablePut(
408 Handle<String> source,
409 JSRegExp::Flags flags,
410 Handle<FixedArray> data) {
411 CALL_HEAP_FUNCTION(GetFirstTable()->PutRegExp(*source, flags, *data),
412 CompilationCacheTable);
413}
414
415
Steve Blocka7e24c12009-10-30 11:49:00 +0000416void CompilationCacheRegExp::Put(Handle<String> source,
417 JSRegExp::Flags flags,
418 Handle<FixedArray> data) {
419 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +0100420 SetFirstTable(TablePut(source, flags, data));
Steve Blocka7e24c12009-10-30 11:49:00 +0000421}
422
423
Steve Block6ded16b2010-05-10 14:33:55 +0100424Handle<SharedFunctionInfo> CompilationCache::LookupScript(Handle<String> source,
425 Handle<Object> name,
426 int line_offset,
427 int column_offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000428 if (!IsEnabled()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100429 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000430 }
431
432 return script.Lookup(source, name, line_offset, column_offset);
433}
434
435
Steve Block6ded16b2010-05-10 14:33:55 +0100436Handle<SharedFunctionInfo> CompilationCache::LookupEval(Handle<String> source,
437 Handle<Context> context,
438 bool is_global) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000439 if (!IsEnabled()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100440 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000441 }
442
Steve Block6ded16b2010-05-10 14:33:55 +0100443 Handle<SharedFunctionInfo> result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000444 if (is_global) {
445 result = eval_global.Lookup(source, context);
446 } else {
447 result = eval_contextual.Lookup(source, context);
448 }
449 return result;
450}
451
452
453Handle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source,
454 JSRegExp::Flags flags) {
455 if (!IsEnabled()) {
456 return Handle<FixedArray>::null();
457 }
458
459 return reg_exp.Lookup(source, flags);
460}
461
462
463void CompilationCache::PutScript(Handle<String> source,
Steve Block6ded16b2010-05-10 14:33:55 +0100464 Handle<SharedFunctionInfo> function_info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000465 if (!IsEnabled()) {
466 return;
467 }
468
Steve Block6ded16b2010-05-10 14:33:55 +0100469 script.Put(source, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000470}
471
472
473void CompilationCache::PutEval(Handle<String> source,
474 Handle<Context> context,
475 bool is_global,
Steve Block6ded16b2010-05-10 14:33:55 +0100476 Handle<SharedFunctionInfo> function_info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000477 if (!IsEnabled()) {
478 return;
479 }
480
481 HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000482 if (is_global) {
Steve Block6ded16b2010-05-10 14:33:55 +0100483 eval_global.Put(source, context, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000484 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100485 eval_contextual.Put(source, context, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000486 }
487}
488
489
490
491void CompilationCache::PutRegExp(Handle<String> source,
492 JSRegExp::Flags flags,
493 Handle<FixedArray> data) {
494 if (!IsEnabled()) {
495 return;
496 }
497
498 reg_exp.Put(source, flags, data);
499}
500
501
502void CompilationCache::Clear() {
503 for (int i = 0; i < kSubCacheCount; i++) {
504 subcaches[i]->Clear();
505 }
506}
507
508
509void CompilationCache::Iterate(ObjectVisitor* v) {
510 for (int i = 0; i < kSubCacheCount; i++) {
511 subcaches[i]->Iterate(v);
512 }
513}
514
515
516void CompilationCache::MarkCompactPrologue() {
517 for (int i = 0; i < kSubCacheCount; i++) {
518 subcaches[i]->Age();
519 }
520}
521
522
523void CompilationCache::Enable() {
524 enabled = true;
525}
526
527
528void CompilationCache::Disable() {
529 enabled = false;
530 Clear();
531}
532
533
534} } // namespace v8::internal