1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "src/assembler.h"
8 #include "src/compilation-cache.h"
9 #include "src/serialize.h"
15 // The number of generations for each sub cache.
16 // The number of ScriptGenerations is carefully chosen based on histograms.
17 // See issue 458: http://code.google.com/p/v8/issues/detail?id=458
18 static const int kScriptGenerations = 5;
19 static const int kEvalGlobalGenerations = 2;
20 static const int kEvalContextualGenerations = 2;
21 static const int kRegExpGenerations = 2;
23 // Initial size of each compilation cache table allocated.
24 static const int kInitialCacheSize = 64;
27 CompilationCache::CompilationCache(Isolate* isolate)
29 script_(isolate, kScriptGenerations),
30 eval_global_(isolate, kEvalGlobalGenerations),
31 eval_contextual_(isolate, kEvalContextualGenerations),
32 reg_exp_(isolate, kRegExpGenerations),
34 CompilationSubCache* subcaches[kSubCacheCount] =
35 {&script_, &eval_global_, &eval_contextual_, ®_exp_};
36 for (int i = 0; i < kSubCacheCount; ++i) {
37 subcaches_[i] = subcaches[i];
42 CompilationCache::~CompilationCache() {}
45 Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) {
46 DCHECK(generation < generations_);
47 Handle<CompilationCacheTable> result;
48 if (tables_[generation]->IsUndefined()) {
49 result = CompilationCacheTable::New(isolate(), kInitialCacheSize);
50 tables_[generation] = *result;
52 CompilationCacheTable* table =
53 CompilationCacheTable::cast(tables_[generation]);
54 result = Handle<CompilationCacheTable>(table, isolate());
60 void CompilationSubCache::Age() {
61 // Age the generations implicitly killing off the oldest.
62 for (int i = generations_ - 1; i > 0; i--) {
63 tables_[i] = tables_[i - 1];
66 // Set the first generation as unborn.
67 tables_[0] = isolate()->heap()->undefined_value();
71 void CompilationSubCache::IterateFunctions(ObjectVisitor* v) {
72 Object* undefined = isolate()->heap()->undefined_value();
73 for (int i = 0; i < generations_; i++) {
74 if (tables_[i] != undefined) {
75 reinterpret_cast<CompilationCacheTable*>(tables_[i])->IterateElements(v);
81 void CompilationSubCache::Iterate(ObjectVisitor* v) {
82 v->VisitPointers(&tables_[0], &tables_[generations_]);
86 void CompilationSubCache::Clear() {
87 MemsetPointer(tables_, isolate()->heap()->undefined_value(), generations_);
91 void CompilationSubCache::Remove(Handle<SharedFunctionInfo> function_info) {
92 // Probe the script generation tables. Make sure not to leak handles
93 // into the caller's handle scope.
94 { HandleScope scope(isolate());
95 for (int generation = 0; generation < generations(); generation++) {
96 Handle<CompilationCacheTable> table = GetTable(generation);
97 table->Remove(*function_info);
103 CompilationCacheScript::CompilationCacheScript(Isolate* isolate,
105 : CompilationSubCache(isolate, generations),
106 script_histogram_(NULL),
107 script_histogram_initialized_(false) { }
110 // We only re-use a cached function for some script source code if the
111 // script originates from the same place. This is to avoid issues
112 // when reporting errors, etc.
113 bool CompilationCacheScript::HasOrigin(
114 Handle<SharedFunctionInfo> function_info,
118 bool is_shared_cross_origin) {
119 Handle<Script> script =
120 Handle<Script>(Script::cast(function_info->script()), isolate());
121 // If the script name isn't set, the boilerplate script should have
122 // an undefined name to have the same origin.
123 if (name.is_null()) {
124 return script->name()->IsUndefined();
126 // Do the fast bailout checks first.
127 if (line_offset != script->line_offset()->value()) return false;
128 if (column_offset != script->column_offset()->value()) return false;
129 // Check that both names are strings. If not, no match.
130 if (!name->IsString() || !script->name()->IsString()) return false;
131 // Were both scripts tagged by the embedder as being shared cross-origin?
132 if (is_shared_cross_origin != script->is_shared_cross_origin()) return false;
133 // Compare the two name strings for equality.
134 return String::Equals(Handle<String>::cast(name),
135 Handle<String>(String::cast(script->name())));
139 // TODO(245): Need to allow identical code from different contexts to
140 // be cached in the same script generation. Currently the first use
141 // will be cached, but subsequent code from different source / line
143 Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(
144 Handle<String> source,
148 bool is_shared_cross_origin,
149 Handle<Context> context) {
150 Object* result = NULL;
153 // Probe the script generation tables. Make sure not to leak handles
154 // into the caller's handle scope.
155 { HandleScope scope(isolate());
156 for (generation = 0; generation < generations(); generation++) {
157 Handle<CompilationCacheTable> table = GetTable(generation);
158 Handle<Object> probe = table->Lookup(source, context);
159 if (probe->IsSharedFunctionInfo()) {
160 Handle<SharedFunctionInfo> function_info =
161 Handle<SharedFunctionInfo>::cast(probe);
162 // Break when we've found a suitable shared function info that
163 // matches the origin.
164 if (HasOrigin(function_info,
168 is_shared_cross_origin)) {
169 result = *function_info;
176 if (!script_histogram_initialized_) {
177 script_histogram_ = isolate()->stats_table()->CreateHistogram(
181 kScriptGenerations + 1);
182 script_histogram_initialized_ = true;
185 if (script_histogram_ != NULL) {
186 // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss.
187 isolate()->stats_table()->AddHistogramSample(script_histogram_, generation);
190 // Once outside the manacles of the handle scope, we need to recheck
191 // to see if we actually found a cached script. If so, we return a
192 // handle created in the caller's handle scope.
193 if (result != NULL) {
194 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result),
196 DCHECK(HasOrigin(shared,
200 is_shared_cross_origin));
201 // If the script was found in a later generation, we promote it to
202 // the first generation to let it survive longer in the cache.
203 if (generation != 0) Put(source, context, shared);
204 isolate()->counters()->compilation_cache_hits()->Increment();
207 isolate()->counters()->compilation_cache_misses()->Increment();
208 return Handle<SharedFunctionInfo>::null();
213 void CompilationCacheScript::Put(Handle<String> source,
214 Handle<Context> context,
215 Handle<SharedFunctionInfo> function_info) {
216 HandleScope scope(isolate());
217 Handle<CompilationCacheTable> table = GetFirstTable();
219 CompilationCacheTable::Put(table, source, context, function_info));
223 MaybeHandle<SharedFunctionInfo> CompilationCacheEval::Lookup(
224 Handle<String> source, Handle<SharedFunctionInfo> outer_info,
225 StrictMode strict_mode, int scope_position) {
226 HandleScope scope(isolate());
227 // Make sure not to leak the table into the surrounding handle
228 // scope. Otherwise, we risk keeping old tables around even after
229 // having cleared the cache.
230 Handle<Object> result = isolate()->factory()->undefined_value();
232 for (generation = 0; generation < generations(); generation++) {
233 Handle<CompilationCacheTable> table = GetTable(generation);
234 result = table->LookupEval(source, outer_info, strict_mode, scope_position);
235 if (result->IsSharedFunctionInfo()) break;
237 if (result->IsSharedFunctionInfo()) {
238 Handle<SharedFunctionInfo> function_info =
239 Handle<SharedFunctionInfo>::cast(result);
240 if (generation != 0) {
241 Put(source, outer_info, function_info, scope_position);
243 isolate()->counters()->compilation_cache_hits()->Increment();
244 return scope.CloseAndEscape(function_info);
246 isolate()->counters()->compilation_cache_misses()->Increment();
247 return MaybeHandle<SharedFunctionInfo>();
252 void CompilationCacheEval::Put(Handle<String> source,
253 Handle<SharedFunctionInfo> outer_info,
254 Handle<SharedFunctionInfo> function_info,
255 int scope_position) {
256 HandleScope scope(isolate());
257 Handle<CompilationCacheTable> table = GetFirstTable();
258 table = CompilationCacheTable::PutEval(table, source, outer_info,
259 function_info, scope_position);
260 SetFirstTable(table);
264 MaybeHandle<FixedArray> CompilationCacheRegExp::Lookup(
265 Handle<String> source,
266 JSRegExp::Flags flags) {
267 HandleScope scope(isolate());
268 // Make sure not to leak the table into the surrounding handle
269 // scope. Otherwise, we risk keeping old tables around even after
270 // having cleared the cache.
271 Handle<Object> result = isolate()->factory()->undefined_value();
273 for (generation = 0; generation < generations(); generation++) {
274 Handle<CompilationCacheTable> table = GetTable(generation);
275 result = table->LookupRegExp(source, flags);
276 if (result->IsFixedArray()) break;
278 if (result->IsFixedArray()) {
279 Handle<FixedArray> data = Handle<FixedArray>::cast(result);
280 if (generation != 0) {
281 Put(source, flags, data);
283 isolate()->counters()->compilation_cache_hits()->Increment();
284 return scope.CloseAndEscape(data);
286 isolate()->counters()->compilation_cache_misses()->Increment();
287 return MaybeHandle<FixedArray>();
292 void CompilationCacheRegExp::Put(Handle<String> source,
293 JSRegExp::Flags flags,
294 Handle<FixedArray> data) {
295 HandleScope scope(isolate());
296 Handle<CompilationCacheTable> table = GetFirstTable();
297 SetFirstTable(CompilationCacheTable::PutRegExp(table, source, flags, data));
301 void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) {
302 if (!IsEnabled()) return;
304 eval_global_.Remove(function_info);
305 eval_contextual_.Remove(function_info);
306 script_.Remove(function_info);
310 MaybeHandle<SharedFunctionInfo> CompilationCache::LookupScript(
311 Handle<String> source,
315 bool is_shared_cross_origin,
316 Handle<Context> context) {
317 if (!IsEnabled()) return MaybeHandle<SharedFunctionInfo>();
319 return script_.Lookup(source, name, line_offset, column_offset,
320 is_shared_cross_origin, context);
324 MaybeHandle<SharedFunctionInfo> CompilationCache::LookupEval(
325 Handle<String> source, Handle<SharedFunctionInfo> outer_info,
326 Handle<Context> context, StrictMode strict_mode, int scope_position) {
327 if (!IsEnabled()) return MaybeHandle<SharedFunctionInfo>();
329 MaybeHandle<SharedFunctionInfo> result;
330 if (context->IsNativeContext()) {
332 eval_global_.Lookup(source, outer_info, strict_mode, scope_position);
334 DCHECK(scope_position != RelocInfo::kNoPosition);
335 result = eval_contextual_.Lookup(source, outer_info, strict_mode,
342 MaybeHandle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source,
343 JSRegExp::Flags flags) {
344 if (!IsEnabled()) return MaybeHandle<FixedArray>();
346 return reg_exp_.Lookup(source, flags);
350 void CompilationCache::PutScript(Handle<String> source,
351 Handle<Context> context,
352 Handle<SharedFunctionInfo> function_info) {
353 if (!IsEnabled()) return;
355 script_.Put(source, context, function_info);
359 void CompilationCache::PutEval(Handle<String> source,
360 Handle<SharedFunctionInfo> outer_info,
361 Handle<Context> context,
362 Handle<SharedFunctionInfo> function_info,
363 int scope_position) {
364 if (!IsEnabled()) return;
366 HandleScope scope(isolate());
367 if (context->IsNativeContext()) {
368 eval_global_.Put(source, outer_info, function_info, scope_position);
370 DCHECK(scope_position != RelocInfo::kNoPosition);
371 eval_contextual_.Put(source, outer_info, function_info, scope_position);
377 void CompilationCache::PutRegExp(Handle<String> source,
378 JSRegExp::Flags flags,
379 Handle<FixedArray> data) {
384 reg_exp_.Put(source, flags, data);
388 void CompilationCache::Clear() {
389 for (int i = 0; i < kSubCacheCount; i++) {
390 subcaches_[i]->Clear();
395 void CompilationCache::Iterate(ObjectVisitor* v) {
396 for (int i = 0; i < kSubCacheCount; i++) {
397 subcaches_[i]->Iterate(v);
402 void CompilationCache::IterateFunctions(ObjectVisitor* v) {
403 for (int i = 0; i < kSubCacheCount; i++) {
404 subcaches_[i]->IterateFunctions(v);
409 void CompilationCache::MarkCompactPrologue() {
410 for (int i = 0; i < kSubCacheCount; i++) {
411 subcaches_[i]->Age();
416 void CompilationCache::Enable() {
421 void CompilationCache::Disable() {
427 } } // namespace v8::internal