blob: cccb7a4f2163d9157e8908035fa2799fec1c7817 [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);
Iain Merrick75681382010-08-19 15:07:18 +010084 void IterateFunctions(ObjectVisitor* v);
Steve Blocka7e24c12009-10-30 11:49:00 +000085
86 // Clear this sub-cache evicting all its content.
87 void Clear();
88
Ben Murdochb0fe1622011-05-05 13:52:32 +010089 // Remove given shared function info from sub-cache.
90 void Remove(Handle<SharedFunctionInfo> function_info);
91
Steve Blocka7e24c12009-10-30 11:49:00 +000092 // Number of generations in this sub-cache.
93 inline int generations() { return generations_; }
94
95 private:
96 int generations_; // Number of generations.
97 Object** tables_; // Compilation cache tables - one for each generation.
98
99 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache);
100};
101
102
103// Sub-cache for scripts.
104class CompilationCacheScript : public CompilationSubCache {
105 public:
106 explicit CompilationCacheScript(int generations)
107 : CompilationSubCache(generations) { }
108
Steve Block6ded16b2010-05-10 14:33:55 +0100109 Handle<SharedFunctionInfo> Lookup(Handle<String> source,
110 Handle<Object> name,
111 int line_offset,
112 int column_offset);
113 void Put(Handle<String> source, Handle<SharedFunctionInfo> function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000114
115 private:
John Reck59135872010-11-02 12:39:01 -0700116 MUST_USE_RESULT MaybeObject* TryTablePut(
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100117 Handle<String> source, Handle<SharedFunctionInfo> function_info);
118
Steve Block6ded16b2010-05-10 14:33:55 +0100119 // Note: Returns a new hash table if operation results in expansion.
120 Handle<CompilationCacheTable> TablePut(
121 Handle<String> source, Handle<SharedFunctionInfo> function_info);
122
123 bool HasOrigin(Handle<SharedFunctionInfo> function_info,
Steve Blocka7e24c12009-10-30 11:49:00 +0000124 Handle<Object> name,
125 int line_offset,
126 int column_offset);
127
128 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript);
129};
130
131
132// Sub-cache for eval scripts.
133class CompilationCacheEval: public CompilationSubCache {
134 public:
135 explicit CompilationCacheEval(int generations)
136 : CompilationSubCache(generations) { }
137
Steve Block6ded16b2010-05-10 14:33:55 +0100138 Handle<SharedFunctionInfo> Lookup(Handle<String> source,
Steve Block1e0659c2011-05-24 12:43:12 +0100139 Handle<Context> context,
140 StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000141
142 void Put(Handle<String> source,
143 Handle<Context> context,
Steve Block6ded16b2010-05-10 14:33:55 +0100144 Handle<SharedFunctionInfo> function_info);
145
146 private:
John Reck59135872010-11-02 12:39:01 -0700147 MUST_USE_RESULT MaybeObject* TryTablePut(
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100148 Handle<String> source,
149 Handle<Context> context,
150 Handle<SharedFunctionInfo> function_info);
151
152
Steve Block6ded16b2010-05-10 14:33:55 +0100153 // Note: Returns a new hash table if operation results in expansion.
154 Handle<CompilationCacheTable> TablePut(
155 Handle<String> source,
156 Handle<Context> context,
157 Handle<SharedFunctionInfo> function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000158
159 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
160};
161
162
163// Sub-cache for regular expressions.
164class CompilationCacheRegExp: public CompilationSubCache {
165 public:
166 explicit CompilationCacheRegExp(int generations)
167 : CompilationSubCache(generations) { }
168
169 Handle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags);
170
171 void Put(Handle<String> source,
172 JSRegExp::Flags flags,
173 Handle<FixedArray> data);
Steve Block6ded16b2010-05-10 14:33:55 +0100174 private:
John Reck59135872010-11-02 12:39:01 -0700175 MUST_USE_RESULT MaybeObject* TryTablePut(Handle<String> source,
176 JSRegExp::Flags flags,
177 Handle<FixedArray> data);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100178
Steve Block6ded16b2010-05-10 14:33:55 +0100179 // Note: Returns a new hash table if operation results in expansion.
180 Handle<CompilationCacheTable> TablePut(Handle<String> source,
181 JSRegExp::Flags flags,
182 Handle<FixedArray> data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000183
184 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp);
185};
186
187
188// Statically allocate all the sub-caches.
189static CompilationCacheScript script(kScriptGenerations);
190static CompilationCacheEval eval_global(kEvalGlobalGenerations);
191static CompilationCacheEval eval_contextual(kEvalContextualGenerations);
192static CompilationCacheRegExp reg_exp(kRegExpGenerations);
193static CompilationSubCache* subcaches[kSubCacheCount] =
194 {&script, &eval_global, &eval_contextual, &reg_exp};
195
196
197// Current enable state of the compilation cache.
198static bool enabled = true;
199static inline bool IsEnabled() {
200 return FLAG_compilation_cache && enabled;
201}
202
203
204static Handle<CompilationCacheTable> AllocateTable(int size) {
205 CALL_HEAP_FUNCTION(CompilationCacheTable::Allocate(size),
206 CompilationCacheTable);
207}
208
209
210Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) {
211 ASSERT(generation < generations_);
212 Handle<CompilationCacheTable> result;
213 if (tables_[generation]->IsUndefined()) {
214 result = AllocateTable(kInitialCacheSize);
215 tables_[generation] = *result;
216 } else {
217 CompilationCacheTable* table =
218 CompilationCacheTable::cast(tables_[generation]);
219 result = Handle<CompilationCacheTable>(table);
220 }
221 return result;
222}
223
224
225void CompilationSubCache::Age() {
226 // Age the generations implicitly killing off the oldest.
227 for (int i = generations_ - 1; i > 0; i--) {
228 tables_[i] = tables_[i - 1];
229 }
230
231 // Set the first generation as unborn.
232 tables_[0] = Heap::undefined_value();
233}
234
235
Iain Merrick75681382010-08-19 15:07:18 +0100236void CompilationSubCache::IterateFunctions(ObjectVisitor* v) {
237 Object* undefined = Heap::raw_unchecked_undefined_value();
238 for (int i = 0; i < generations_; i++) {
239 if (tables_[i] != undefined) {
240 reinterpret_cast<CompilationCacheTable*>(tables_[i])->IterateElements(v);
241 }
242 }
243}
244
245
Steve Blocka7e24c12009-10-30 11:49:00 +0000246void CompilationSubCache::Iterate(ObjectVisitor* v) {
247 v->VisitPointers(&tables_[0], &tables_[generations_]);
248}
249
250
251void CompilationSubCache::Clear() {
Steve Block6ded16b2010-05-10 14:33:55 +0100252 MemsetPointer(tables_, Heap::undefined_value(), generations_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000253}
254
255
Ben Murdochb0fe1622011-05-05 13:52:32 +0100256void CompilationSubCache::Remove(Handle<SharedFunctionInfo> function_info) {
257 // Probe the script generation tables. Make sure not to leak handles
258 // into the caller's handle scope.
259 { HandleScope scope;
260 for (int generation = 0; generation < generations(); generation++) {
261 Handle<CompilationCacheTable> table = GetTable(generation);
262 table->Remove(*function_info);
263 }
264 }
265}
266
267
Steve Blocka7e24c12009-10-30 11:49:00 +0000268// We only re-use a cached function for some script source code if the
269// script originates from the same place. This is to avoid issues
270// when reporting errors, etc.
Steve Block6ded16b2010-05-10 14:33:55 +0100271bool CompilationCacheScript::HasOrigin(
272 Handle<SharedFunctionInfo> function_info,
273 Handle<Object> name,
274 int line_offset,
275 int column_offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 Handle<Script> script =
Steve Block6ded16b2010-05-10 14:33:55 +0100277 Handle<Script>(Script::cast(function_info->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 // If the script name isn't set, the boilerplate script should have
279 // an undefined name to have the same origin.
280 if (name.is_null()) {
281 return script->name()->IsUndefined();
282 }
283 // Do the fast bailout checks first.
284 if (line_offset != script->line_offset()->value()) return false;
285 if (column_offset != script->column_offset()->value()) return false;
286 // Check that both names are strings. If not, no match.
287 if (!name->IsString() || !script->name()->IsString()) return false;
288 // Compare the two name strings for equality.
289 return String::cast(*name)->Equals(String::cast(script->name()));
290}
291
292
293// TODO(245): Need to allow identical code from different contexts to
294// be cached in the same script generation. Currently the first use
295// will be cached, but subsequent code from different source / line
296// won't.
Steve Block6ded16b2010-05-10 14:33:55 +0100297Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(Handle<String> source,
298 Handle<Object> name,
299 int line_offset,
300 int column_offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000301 Object* result = NULL;
302 int generation;
303
304 // Probe the script generation tables. Make sure not to leak handles
305 // into the caller's handle scope.
306 { HandleScope scope;
307 for (generation = 0; generation < generations(); generation++) {
308 Handle<CompilationCacheTable> table = GetTable(generation);
309 Handle<Object> probe(table->Lookup(*source));
Steve Block6ded16b2010-05-10 14:33:55 +0100310 if (probe->IsSharedFunctionInfo()) {
311 Handle<SharedFunctionInfo> function_info =
312 Handle<SharedFunctionInfo>::cast(probe);
313 // Break when we've found a suitable shared function info that
Steve Blocka7e24c12009-10-30 11:49:00 +0000314 // matches the origin.
Steve Block6ded16b2010-05-10 14:33:55 +0100315 if (HasOrigin(function_info, name, line_offset, column_offset)) {
316 result = *function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 break;
318 }
319 }
320 }
321 }
322
323 static void* script_histogram = StatsTable::CreateHistogram(
324 "V8.ScriptCache",
325 0,
326 kScriptGenerations,
327 kScriptGenerations + 1);
328
329 if (script_histogram != NULL) {
330 // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss.
331 StatsTable::AddHistogramSample(script_histogram, generation);
332 }
333
334 // Once outside the manacles of the handle scope, we need to recheck
335 // to see if we actually found a cached script. If so, we return a
336 // handle created in the caller's handle scope.
337 if (result != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100338 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
339 ASSERT(HasOrigin(shared, name, line_offset, column_offset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000340 // If the script was found in a later generation, we promote it to
341 // the first generation to let it survive longer in the cache.
Steve Block6ded16b2010-05-10 14:33:55 +0100342 if (generation != 0) Put(source, shared);
Steve Blocka7e24c12009-10-30 11:49:00 +0000343 Counters::compilation_cache_hits.Increment();
Steve Block6ded16b2010-05-10 14:33:55 +0100344 return shared;
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 } else {
346 Counters::compilation_cache_misses.Increment();
Steve Block6ded16b2010-05-10 14:33:55 +0100347 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 }
349}
350
351
John Reck59135872010-11-02 12:39:01 -0700352MaybeObject* CompilationCacheScript::TryTablePut(
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100353 Handle<String> source,
354 Handle<SharedFunctionInfo> function_info) {
355 Handle<CompilationCacheTable> table = GetFirstTable();
356 return table->Put(*source, *function_info);
357}
358
359
Steve Block6ded16b2010-05-10 14:33:55 +0100360Handle<CompilationCacheTable> CompilationCacheScript::TablePut(
361 Handle<String> source,
362 Handle<SharedFunctionInfo> function_info) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100363 CALL_HEAP_FUNCTION(TryTablePut(source, function_info), CompilationCacheTable);
Steve Blocka7e24c12009-10-30 11:49:00 +0000364}
365
366
Steve Block6ded16b2010-05-10 14:33:55 +0100367void CompilationCacheScript::Put(Handle<String> source,
368 Handle<SharedFunctionInfo> function_info) {
369 HandleScope scope;
370 SetFirstTable(TablePut(source, function_info));
371}
372
373
374Handle<SharedFunctionInfo> CompilationCacheEval::Lookup(
Steve Block1e0659c2011-05-24 12:43:12 +0100375 Handle<String> source,
376 Handle<Context> context,
377 StrictModeFlag strict_mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 // Make sure not to leak the table into the surrounding handle
379 // scope. Otherwise, we risk keeping old tables around even after
380 // having cleared the cache.
381 Object* result = NULL;
382 int generation;
383 { HandleScope scope;
384 for (generation = 0; generation < generations(); generation++) {
385 Handle<CompilationCacheTable> table = GetTable(generation);
Steve Block1e0659c2011-05-24 12:43:12 +0100386 result = table->LookupEval(*source, *context, strict_mode);
Steve Block6ded16b2010-05-10 14:33:55 +0100387 if (result->IsSharedFunctionInfo()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 break;
389 }
390 }
391 }
Steve Block6ded16b2010-05-10 14:33:55 +0100392 if (result->IsSharedFunctionInfo()) {
393 Handle<SharedFunctionInfo>
394 function_info(SharedFunctionInfo::cast(result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000395 if (generation != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +0100396 Put(source, context, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000397 }
398 Counters::compilation_cache_hits.Increment();
Steve Block6ded16b2010-05-10 14:33:55 +0100399 return function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 } else {
401 Counters::compilation_cache_misses.Increment();
Steve Block6ded16b2010-05-10 14:33:55 +0100402 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000403 }
404}
405
406
John Reck59135872010-11-02 12:39:01 -0700407MaybeObject* CompilationCacheEval::TryTablePut(
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100408 Handle<String> source,
409 Handle<Context> context,
410 Handle<SharedFunctionInfo> function_info) {
411 Handle<CompilationCacheTable> table = GetFirstTable();
412 return table->PutEval(*source, *context, *function_info);
413}
414
415
Steve Block6ded16b2010-05-10 14:33:55 +0100416Handle<CompilationCacheTable> CompilationCacheEval::TablePut(
417 Handle<String> source,
418 Handle<Context> context,
419 Handle<SharedFunctionInfo> function_info) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100420 CALL_HEAP_FUNCTION(TryTablePut(source, context, function_info),
Steve Block6ded16b2010-05-10 14:33:55 +0100421 CompilationCacheTable);
422}
423
424
Steve Blocka7e24c12009-10-30 11:49:00 +0000425void CompilationCacheEval::Put(Handle<String> source,
426 Handle<Context> context,
Steve Block6ded16b2010-05-10 14:33:55 +0100427 Handle<SharedFunctionInfo> function_info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000428 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +0100429 SetFirstTable(TablePut(source, context, function_info));
Steve Blocka7e24c12009-10-30 11:49:00 +0000430}
431
432
433Handle<FixedArray> CompilationCacheRegExp::Lookup(Handle<String> source,
434 JSRegExp::Flags flags) {
435 // Make sure not to leak the table into the surrounding handle
436 // scope. Otherwise, we risk keeping old tables around even after
437 // having cleared the cache.
438 Object* result = NULL;
439 int generation;
440 { HandleScope scope;
441 for (generation = 0; generation < generations(); generation++) {
442 Handle<CompilationCacheTable> table = GetTable(generation);
443 result = table->LookupRegExp(*source, flags);
444 if (result->IsFixedArray()) {
445 break;
446 }
447 }
448 }
449 if (result->IsFixedArray()) {
450 Handle<FixedArray> data(FixedArray::cast(result));
451 if (generation != 0) {
452 Put(source, flags, data);
453 }
454 Counters::compilation_cache_hits.Increment();
455 return data;
456 } else {
457 Counters::compilation_cache_misses.Increment();
458 return Handle<FixedArray>::null();
459 }
460}
461
462
John Reck59135872010-11-02 12:39:01 -0700463MaybeObject* CompilationCacheRegExp::TryTablePut(
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100464 Handle<String> source,
465 JSRegExp::Flags flags,
466 Handle<FixedArray> data) {
467 Handle<CompilationCacheTable> table = GetFirstTable();
468 return table->PutRegExp(*source, flags, *data);
469}
470
471
Steve Block6ded16b2010-05-10 14:33:55 +0100472Handle<CompilationCacheTable> CompilationCacheRegExp::TablePut(
473 Handle<String> source,
474 JSRegExp::Flags flags,
475 Handle<FixedArray> data) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100476 CALL_HEAP_FUNCTION(TryTablePut(source, flags, data), CompilationCacheTable);
Steve Block6ded16b2010-05-10 14:33:55 +0100477}
478
479
Steve Blocka7e24c12009-10-30 11:49:00 +0000480void CompilationCacheRegExp::Put(Handle<String> source,
481 JSRegExp::Flags flags,
482 Handle<FixedArray> data) {
483 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +0100484 SetFirstTable(TablePut(source, flags, data));
Steve Blocka7e24c12009-10-30 11:49:00 +0000485}
486
487
Ben Murdochb0fe1622011-05-05 13:52:32 +0100488void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) {
489 if (!IsEnabled()) return;
490
491 eval_global.Remove(function_info);
492 eval_contextual.Remove(function_info);
493 script.Remove(function_info);
494}
495
496
Steve Block6ded16b2010-05-10 14:33:55 +0100497Handle<SharedFunctionInfo> CompilationCache::LookupScript(Handle<String> source,
498 Handle<Object> name,
499 int line_offset,
500 int column_offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000501 if (!IsEnabled()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100502 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000503 }
504
505 return script.Lookup(source, name, line_offset, column_offset);
506}
507
508
Steve Block1e0659c2011-05-24 12:43:12 +0100509Handle<SharedFunctionInfo> CompilationCache::LookupEval(
510 Handle<String> source,
511 Handle<Context> context,
512 bool is_global,
513 StrictModeFlag strict_mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000514 if (!IsEnabled()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100515 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000516 }
517
Steve Block6ded16b2010-05-10 14:33:55 +0100518 Handle<SharedFunctionInfo> result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000519 if (is_global) {
Steve Block1e0659c2011-05-24 12:43:12 +0100520 result = eval_global.Lookup(source, context, strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000521 } else {
Steve Block1e0659c2011-05-24 12:43:12 +0100522 result = eval_contextual.Lookup(source, context, strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000523 }
524 return result;
525}
526
527
528Handle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source,
529 JSRegExp::Flags flags) {
530 if (!IsEnabled()) {
531 return Handle<FixedArray>::null();
532 }
533
534 return reg_exp.Lookup(source, flags);
535}
536
537
538void CompilationCache::PutScript(Handle<String> source,
Steve Block6ded16b2010-05-10 14:33:55 +0100539 Handle<SharedFunctionInfo> function_info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 if (!IsEnabled()) {
541 return;
542 }
543
Steve Block6ded16b2010-05-10 14:33:55 +0100544 script.Put(source, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000545}
546
547
548void CompilationCache::PutEval(Handle<String> source,
549 Handle<Context> context,
550 bool is_global,
Steve Block6ded16b2010-05-10 14:33:55 +0100551 Handle<SharedFunctionInfo> function_info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000552 if (!IsEnabled()) {
553 return;
554 }
555
556 HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000557 if (is_global) {
Steve Block6ded16b2010-05-10 14:33:55 +0100558 eval_global.Put(source, context, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000559 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100560 eval_contextual.Put(source, context, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000561 }
562}
563
564
565
566void CompilationCache::PutRegExp(Handle<String> source,
567 JSRegExp::Flags flags,
568 Handle<FixedArray> data) {
569 if (!IsEnabled()) {
570 return;
571 }
572
573 reg_exp.Put(source, flags, data);
574}
575
576
Ben Murdochb0fe1622011-05-05 13:52:32 +0100577static bool SourceHashCompare(void* key1, void* key2) {
578 return key1 == key2;
579}
580
581
582static HashMap* EagerOptimizingSet() {
583 static HashMap map(&SourceHashCompare);
584 return &map;
585}
586
587
588bool CompilationCache::ShouldOptimizeEagerly(Handle<JSFunction> function) {
589 if (FLAG_opt_eagerly) return true;
590 uint32_t hash = function->SourceHash();
591 void* key = reinterpret_cast<void*>(hash);
592 return EagerOptimizingSet()->Lookup(key, hash, false) != NULL;
593}
594
595
596void CompilationCache::MarkForEagerOptimizing(Handle<JSFunction> function) {
597 uint32_t hash = function->SourceHash();
598 void* key = reinterpret_cast<void*>(hash);
599 EagerOptimizingSet()->Lookup(key, hash, true);
600}
601
602
603void CompilationCache::MarkForLazyOptimizing(Handle<JSFunction> function) {
604 uint32_t hash = function->SourceHash();
605 void* key = reinterpret_cast<void*>(hash);
606 EagerOptimizingSet()->Remove(key, hash);
607}
608
609
610void CompilationCache::ResetEagerOptimizingData() {
611 HashMap* set = EagerOptimizingSet();
612 if (set->occupancy() > 0) set->Clear();
613}
614
615
Steve Blocka7e24c12009-10-30 11:49:00 +0000616void CompilationCache::Clear() {
617 for (int i = 0; i < kSubCacheCount; i++) {
618 subcaches[i]->Clear();
619 }
620}
621
Steve Blocka7e24c12009-10-30 11:49:00 +0000622void CompilationCache::Iterate(ObjectVisitor* v) {
623 for (int i = 0; i < kSubCacheCount; i++) {
624 subcaches[i]->Iterate(v);
625 }
626}
627
628
Iain Merrick75681382010-08-19 15:07:18 +0100629void CompilationCache::IterateFunctions(ObjectVisitor* v) {
630 for (int i = 0; i < kSubCacheCount; i++) {
631 subcaches[i]->IterateFunctions(v);
632 }
633}
634
635
Steve Blocka7e24c12009-10-30 11:49:00 +0000636void CompilationCache::MarkCompactPrologue() {
637 for (int i = 0; i < kSubCacheCount; i++) {
638 subcaches[i]->Age();
639 }
640}
641
642
643void CompilationCache::Enable() {
644 enabled = true;
645}
646
647
648void CompilationCache::Disable() {
649 enabled = false;
650 Clear();
651}
652
653
654} } // namespace v8::internal