blob: 38438cb9132ca6c21eed433b78c04af7c1e967c0 [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,
139 Handle<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000140
141 void Put(Handle<String> source,
142 Handle<Context> context,
Steve Block6ded16b2010-05-10 14:33:55 +0100143 Handle<SharedFunctionInfo> function_info);
144
145 private:
John Reck59135872010-11-02 12:39:01 -0700146 MUST_USE_RESULT MaybeObject* TryTablePut(
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100147 Handle<String> source,
148 Handle<Context> context,
149 Handle<SharedFunctionInfo> function_info);
150
151
Steve Block6ded16b2010-05-10 14:33:55 +0100152 // Note: Returns a new hash table if operation results in expansion.
153 Handle<CompilationCacheTable> TablePut(
154 Handle<String> source,
155 Handle<Context> context,
156 Handle<SharedFunctionInfo> function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000157
158 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
159};
160
161
162// Sub-cache for regular expressions.
163class CompilationCacheRegExp: public CompilationSubCache {
164 public:
165 explicit CompilationCacheRegExp(int generations)
166 : CompilationSubCache(generations) { }
167
168 Handle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags);
169
170 void Put(Handle<String> source,
171 JSRegExp::Flags flags,
172 Handle<FixedArray> data);
Steve Block6ded16b2010-05-10 14:33:55 +0100173 private:
John Reck59135872010-11-02 12:39:01 -0700174 MUST_USE_RESULT MaybeObject* TryTablePut(Handle<String> source,
175 JSRegExp::Flags flags,
176 Handle<FixedArray> data);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100177
Steve Block6ded16b2010-05-10 14:33:55 +0100178 // Note: Returns a new hash table if operation results in expansion.
179 Handle<CompilationCacheTable> TablePut(Handle<String> source,
180 JSRegExp::Flags flags,
181 Handle<FixedArray> data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000182
183 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp);
184};
185
186
187// Statically allocate all the sub-caches.
188static CompilationCacheScript script(kScriptGenerations);
189static CompilationCacheEval eval_global(kEvalGlobalGenerations);
190static CompilationCacheEval eval_contextual(kEvalContextualGenerations);
191static CompilationCacheRegExp reg_exp(kRegExpGenerations);
192static CompilationSubCache* subcaches[kSubCacheCount] =
193 {&script, &eval_global, &eval_contextual, &reg_exp};
194
195
196// Current enable state of the compilation cache.
197static bool enabled = true;
198static inline bool IsEnabled() {
199 return FLAG_compilation_cache && enabled;
200}
201
202
203static Handle<CompilationCacheTable> AllocateTable(int size) {
204 CALL_HEAP_FUNCTION(CompilationCacheTable::Allocate(size),
205 CompilationCacheTable);
206}
207
208
209Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) {
210 ASSERT(generation < generations_);
211 Handle<CompilationCacheTable> result;
212 if (tables_[generation]->IsUndefined()) {
213 result = AllocateTable(kInitialCacheSize);
214 tables_[generation] = *result;
215 } else {
216 CompilationCacheTable* table =
217 CompilationCacheTable::cast(tables_[generation]);
218 result = Handle<CompilationCacheTable>(table);
219 }
220 return result;
221}
222
223
224void CompilationSubCache::Age() {
225 // Age the generations implicitly killing off the oldest.
226 for (int i = generations_ - 1; i > 0; i--) {
227 tables_[i] = tables_[i - 1];
228 }
229
230 // Set the first generation as unborn.
231 tables_[0] = Heap::undefined_value();
232}
233
234
Iain Merrick75681382010-08-19 15:07:18 +0100235void CompilationSubCache::IterateFunctions(ObjectVisitor* v) {
236 Object* undefined = Heap::raw_unchecked_undefined_value();
237 for (int i = 0; i < generations_; i++) {
238 if (tables_[i] != undefined) {
239 reinterpret_cast<CompilationCacheTable*>(tables_[i])->IterateElements(v);
240 }
241 }
242}
243
244
Steve Blocka7e24c12009-10-30 11:49:00 +0000245void CompilationSubCache::Iterate(ObjectVisitor* v) {
246 v->VisitPointers(&tables_[0], &tables_[generations_]);
247}
248
249
250void CompilationSubCache::Clear() {
Steve Block6ded16b2010-05-10 14:33:55 +0100251 MemsetPointer(tables_, Heap::undefined_value(), generations_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000252}
253
254
Ben Murdochb0fe1622011-05-05 13:52:32 +0100255void CompilationSubCache::Remove(Handle<SharedFunctionInfo> function_info) {
256 // Probe the script generation tables. Make sure not to leak handles
257 // into the caller's handle scope.
258 { HandleScope scope;
259 for (int generation = 0; generation < generations(); generation++) {
260 Handle<CompilationCacheTable> table = GetTable(generation);
261 table->Remove(*function_info);
262 }
263 }
264}
265
266
Steve Blocka7e24c12009-10-30 11:49:00 +0000267// We only re-use a cached function for some script source code if the
268// script originates from the same place. This is to avoid issues
269// when reporting errors, etc.
Steve Block6ded16b2010-05-10 14:33:55 +0100270bool CompilationCacheScript::HasOrigin(
271 Handle<SharedFunctionInfo> function_info,
272 Handle<Object> name,
273 int line_offset,
274 int column_offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 Handle<Script> script =
Steve Block6ded16b2010-05-10 14:33:55 +0100276 Handle<Script>(Script::cast(function_info->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 // If the script name isn't set, the boilerplate script should have
278 // an undefined name to have the same origin.
279 if (name.is_null()) {
280 return script->name()->IsUndefined();
281 }
282 // Do the fast bailout checks first.
283 if (line_offset != script->line_offset()->value()) return false;
284 if (column_offset != script->column_offset()->value()) return false;
285 // Check that both names are strings. If not, no match.
286 if (!name->IsString() || !script->name()->IsString()) return false;
287 // Compare the two name strings for equality.
288 return String::cast(*name)->Equals(String::cast(script->name()));
289}
290
291
292// TODO(245): Need to allow identical code from different contexts to
293// be cached in the same script generation. Currently the first use
294// will be cached, but subsequent code from different source / line
295// won't.
Steve Block6ded16b2010-05-10 14:33:55 +0100296Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(Handle<String> source,
297 Handle<Object> name,
298 int line_offset,
299 int column_offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 Object* result = NULL;
301 int generation;
302
303 // Probe the script generation tables. Make sure not to leak handles
304 // into the caller's handle scope.
305 { HandleScope scope;
306 for (generation = 0; generation < generations(); generation++) {
307 Handle<CompilationCacheTable> table = GetTable(generation);
308 Handle<Object> probe(table->Lookup(*source));
Steve Block6ded16b2010-05-10 14:33:55 +0100309 if (probe->IsSharedFunctionInfo()) {
310 Handle<SharedFunctionInfo> function_info =
311 Handle<SharedFunctionInfo>::cast(probe);
312 // Break when we've found a suitable shared function info that
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 // matches the origin.
Steve Block6ded16b2010-05-10 14:33:55 +0100314 if (HasOrigin(function_info, name, line_offset, column_offset)) {
315 result = *function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +0000316 break;
317 }
318 }
319 }
320 }
321
322 static void* script_histogram = StatsTable::CreateHistogram(
323 "V8.ScriptCache",
324 0,
325 kScriptGenerations,
326 kScriptGenerations + 1);
327
328 if (script_histogram != NULL) {
329 // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss.
330 StatsTable::AddHistogramSample(script_histogram, generation);
331 }
332
333 // Once outside the manacles of the handle scope, we need to recheck
334 // to see if we actually found a cached script. If so, we return a
335 // handle created in the caller's handle scope.
336 if (result != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100337 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
338 ASSERT(HasOrigin(shared, name, line_offset, column_offset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 // If the script was found in a later generation, we promote it to
340 // the first generation to let it survive longer in the cache.
Steve Block6ded16b2010-05-10 14:33:55 +0100341 if (generation != 0) Put(source, shared);
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 Counters::compilation_cache_hits.Increment();
Steve Block6ded16b2010-05-10 14:33:55 +0100343 return shared;
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 } else {
345 Counters::compilation_cache_misses.Increment();
Steve Block6ded16b2010-05-10 14:33:55 +0100346 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 }
348}
349
350
John Reck59135872010-11-02 12:39:01 -0700351MaybeObject* CompilationCacheScript::TryTablePut(
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100352 Handle<String> source,
353 Handle<SharedFunctionInfo> function_info) {
354 Handle<CompilationCacheTable> table = GetFirstTable();
355 return table->Put(*source, *function_info);
356}
357
358
Steve Block6ded16b2010-05-10 14:33:55 +0100359Handle<CompilationCacheTable> CompilationCacheScript::TablePut(
360 Handle<String> source,
361 Handle<SharedFunctionInfo> function_info) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100362 CALL_HEAP_FUNCTION(TryTablePut(source, function_info), CompilationCacheTable);
Steve Blocka7e24c12009-10-30 11:49:00 +0000363}
364
365
Steve Block6ded16b2010-05-10 14:33:55 +0100366void CompilationCacheScript::Put(Handle<String> source,
367 Handle<SharedFunctionInfo> function_info) {
368 HandleScope scope;
369 SetFirstTable(TablePut(source, function_info));
370}
371
372
373Handle<SharedFunctionInfo> CompilationCacheEval::Lookup(
374 Handle<String> source, Handle<Context> context) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000375 // Make sure not to leak the table into the surrounding handle
376 // scope. Otherwise, we risk keeping old tables around even after
377 // having cleared the cache.
378 Object* result = NULL;
379 int generation;
380 { HandleScope scope;
381 for (generation = 0; generation < generations(); generation++) {
382 Handle<CompilationCacheTable> table = GetTable(generation);
383 result = table->LookupEval(*source, *context);
Steve Block6ded16b2010-05-10 14:33:55 +0100384 if (result->IsSharedFunctionInfo()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000385 break;
386 }
387 }
388 }
Steve Block6ded16b2010-05-10 14:33:55 +0100389 if (result->IsSharedFunctionInfo()) {
390 Handle<SharedFunctionInfo>
391 function_info(SharedFunctionInfo::cast(result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000392 if (generation != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +0100393 Put(source, context, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000394 }
395 Counters::compilation_cache_hits.Increment();
Steve Block6ded16b2010-05-10 14:33:55 +0100396 return function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +0000397 } else {
398 Counters::compilation_cache_misses.Increment();
Steve Block6ded16b2010-05-10 14:33:55 +0100399 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 }
401}
402
403
John Reck59135872010-11-02 12:39:01 -0700404MaybeObject* CompilationCacheEval::TryTablePut(
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100405 Handle<String> source,
406 Handle<Context> context,
407 Handle<SharedFunctionInfo> function_info) {
408 Handle<CompilationCacheTable> table = GetFirstTable();
409 return table->PutEval(*source, *context, *function_info);
410}
411
412
Steve Block6ded16b2010-05-10 14:33:55 +0100413Handle<CompilationCacheTable> CompilationCacheEval::TablePut(
414 Handle<String> source,
415 Handle<Context> context,
416 Handle<SharedFunctionInfo> function_info) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100417 CALL_HEAP_FUNCTION(TryTablePut(source, context, function_info),
Steve Block6ded16b2010-05-10 14:33:55 +0100418 CompilationCacheTable);
419}
420
421
Steve Blocka7e24c12009-10-30 11:49:00 +0000422void CompilationCacheEval::Put(Handle<String> source,
423 Handle<Context> context,
Steve Block6ded16b2010-05-10 14:33:55 +0100424 Handle<SharedFunctionInfo> function_info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000425 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +0100426 SetFirstTable(TablePut(source, context, function_info));
Steve Blocka7e24c12009-10-30 11:49:00 +0000427}
428
429
430Handle<FixedArray> CompilationCacheRegExp::Lookup(Handle<String> source,
431 JSRegExp::Flags flags) {
432 // Make sure not to leak the table into the surrounding handle
433 // scope. Otherwise, we risk keeping old tables around even after
434 // having cleared the cache.
435 Object* result = NULL;
436 int generation;
437 { HandleScope scope;
438 for (generation = 0; generation < generations(); generation++) {
439 Handle<CompilationCacheTable> table = GetTable(generation);
440 result = table->LookupRegExp(*source, flags);
441 if (result->IsFixedArray()) {
442 break;
443 }
444 }
445 }
446 if (result->IsFixedArray()) {
447 Handle<FixedArray> data(FixedArray::cast(result));
448 if (generation != 0) {
449 Put(source, flags, data);
450 }
451 Counters::compilation_cache_hits.Increment();
452 return data;
453 } else {
454 Counters::compilation_cache_misses.Increment();
455 return Handle<FixedArray>::null();
456 }
457}
458
459
John Reck59135872010-11-02 12:39:01 -0700460MaybeObject* CompilationCacheRegExp::TryTablePut(
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100461 Handle<String> source,
462 JSRegExp::Flags flags,
463 Handle<FixedArray> data) {
464 Handle<CompilationCacheTable> table = GetFirstTable();
465 return table->PutRegExp(*source, flags, *data);
466}
467
468
Steve Block6ded16b2010-05-10 14:33:55 +0100469Handle<CompilationCacheTable> CompilationCacheRegExp::TablePut(
470 Handle<String> source,
471 JSRegExp::Flags flags,
472 Handle<FixedArray> data) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100473 CALL_HEAP_FUNCTION(TryTablePut(source, flags, data), CompilationCacheTable);
Steve Block6ded16b2010-05-10 14:33:55 +0100474}
475
476
Steve Blocka7e24c12009-10-30 11:49:00 +0000477void CompilationCacheRegExp::Put(Handle<String> source,
478 JSRegExp::Flags flags,
479 Handle<FixedArray> data) {
480 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +0100481 SetFirstTable(TablePut(source, flags, data));
Steve Blocka7e24c12009-10-30 11:49:00 +0000482}
483
484
Ben Murdochb0fe1622011-05-05 13:52:32 +0100485void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) {
486 if (!IsEnabled()) return;
487
488 eval_global.Remove(function_info);
489 eval_contextual.Remove(function_info);
490 script.Remove(function_info);
491}
492
493
Steve Block6ded16b2010-05-10 14:33:55 +0100494Handle<SharedFunctionInfo> CompilationCache::LookupScript(Handle<String> source,
495 Handle<Object> name,
496 int line_offset,
497 int column_offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000498 if (!IsEnabled()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100499 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000500 }
501
502 return script.Lookup(source, name, line_offset, column_offset);
503}
504
505
Steve Block6ded16b2010-05-10 14:33:55 +0100506Handle<SharedFunctionInfo> CompilationCache::LookupEval(Handle<String> source,
507 Handle<Context> context,
508 bool is_global) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000509 if (!IsEnabled()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100510 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000511 }
512
Steve Block6ded16b2010-05-10 14:33:55 +0100513 Handle<SharedFunctionInfo> result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000514 if (is_global) {
515 result = eval_global.Lookup(source, context);
516 } else {
517 result = eval_contextual.Lookup(source, context);
518 }
519 return result;
520}
521
522
523Handle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source,
524 JSRegExp::Flags flags) {
525 if (!IsEnabled()) {
526 return Handle<FixedArray>::null();
527 }
528
529 return reg_exp.Lookup(source, flags);
530}
531
532
533void CompilationCache::PutScript(Handle<String> source,
Steve Block6ded16b2010-05-10 14:33:55 +0100534 Handle<SharedFunctionInfo> function_info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000535 if (!IsEnabled()) {
536 return;
537 }
538
Steve Block6ded16b2010-05-10 14:33:55 +0100539 script.Put(source, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000540}
541
542
543void CompilationCache::PutEval(Handle<String> source,
544 Handle<Context> context,
545 bool is_global,
Steve Block6ded16b2010-05-10 14:33:55 +0100546 Handle<SharedFunctionInfo> function_info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000547 if (!IsEnabled()) {
548 return;
549 }
550
551 HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000552 if (is_global) {
Steve Block6ded16b2010-05-10 14:33:55 +0100553 eval_global.Put(source, context, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000554 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100555 eval_contextual.Put(source, context, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 }
557}
558
559
560
561void CompilationCache::PutRegExp(Handle<String> source,
562 JSRegExp::Flags flags,
563 Handle<FixedArray> data) {
564 if (!IsEnabled()) {
565 return;
566 }
567
568 reg_exp.Put(source, flags, data);
569}
570
571
Ben Murdochb0fe1622011-05-05 13:52:32 +0100572static bool SourceHashCompare(void* key1, void* key2) {
573 return key1 == key2;
574}
575
576
577static HashMap* EagerOptimizingSet() {
578 static HashMap map(&SourceHashCompare);
579 return &map;
580}
581
582
583bool CompilationCache::ShouldOptimizeEagerly(Handle<JSFunction> function) {
584 if (FLAG_opt_eagerly) return true;
585 uint32_t hash = function->SourceHash();
586 void* key = reinterpret_cast<void*>(hash);
587 return EagerOptimizingSet()->Lookup(key, hash, false) != NULL;
588}
589
590
591void CompilationCache::MarkForEagerOptimizing(Handle<JSFunction> function) {
592 uint32_t hash = function->SourceHash();
593 void* key = reinterpret_cast<void*>(hash);
594 EagerOptimizingSet()->Lookup(key, hash, true);
595}
596
597
598void CompilationCache::MarkForLazyOptimizing(Handle<JSFunction> function) {
599 uint32_t hash = function->SourceHash();
600 void* key = reinterpret_cast<void*>(hash);
601 EagerOptimizingSet()->Remove(key, hash);
602}
603
604
605void CompilationCache::ResetEagerOptimizingData() {
606 HashMap* set = EagerOptimizingSet();
607 if (set->occupancy() > 0) set->Clear();
608}
609
610
Steve Blocka7e24c12009-10-30 11:49:00 +0000611void CompilationCache::Clear() {
612 for (int i = 0; i < kSubCacheCount; i++) {
613 subcaches[i]->Clear();
614 }
615}
616
Steve Blocka7e24c12009-10-30 11:49:00 +0000617void CompilationCache::Iterate(ObjectVisitor* v) {
618 for (int i = 0; i < kSubCacheCount; i++) {
619 subcaches[i]->Iterate(v);
620 }
621}
622
623
Iain Merrick75681382010-08-19 15:07:18 +0100624void CompilationCache::IterateFunctions(ObjectVisitor* v) {
625 for (int i = 0; i < kSubCacheCount; i++) {
626 subcaches[i]->IterateFunctions(v);
627 }
628}
629
630
Steve Blocka7e24c12009-10-30 11:49:00 +0000631void CompilationCache::MarkCompactPrologue() {
632 for (int i = 0; i < kSubCacheCount; i++) {
633 subcaches[i]->Age();
634 }
635}
636
637
638void CompilationCache::Enable() {
639 enabled = true;
640}
641
642
643void CompilationCache::Disable() {
644 enabled = false;
645 Clear();
646}
647
648
649} } // namespace v8::internal