Home | History | Annotate | Download | only in src
      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.
      4 
      5 #include "src/v8.h"
      6 
      7 #include "src/assembler.h"
      8 #include "src/compilation-cache.h"
      9 #include "src/serialize.h"
     10 
     11 namespace v8 {
     12 namespace internal {
     13 
     14 
     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;
     22 
     23 // Initial size of each compilation cache table allocated.
     24 static const int kInitialCacheSize = 64;
     25 
     26 
     27 CompilationCache::CompilationCache(Isolate* isolate)
     28     : isolate_(isolate),
     29       script_(isolate, kScriptGenerations),
     30       eval_global_(isolate, kEvalGlobalGenerations),
     31       eval_contextual_(isolate, kEvalContextualGenerations),
     32       reg_exp_(isolate, kRegExpGenerations),
     33       enabled_(true) {
     34   CompilationSubCache* subcaches[kSubCacheCount] =
     35     {&script_, &eval_global_, &eval_contextual_, &reg_exp_};
     36   for (int i = 0; i < kSubCacheCount; ++i) {
     37     subcaches_[i] = subcaches[i];
     38   }
     39 }
     40 
     41 
     42 CompilationCache::~CompilationCache() {}
     43 
     44 
     45 Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) {
     46   ASSERT(generation < generations_);
     47   Handle<CompilationCacheTable> result;
     48   if (tables_[generation]->IsUndefined()) {
     49     result = CompilationCacheTable::New(isolate(), kInitialCacheSize);
     50     tables_[generation] = *result;
     51   } else {
     52     CompilationCacheTable* table =
     53         CompilationCacheTable::cast(tables_[generation]);
     54     result = Handle<CompilationCacheTable>(table, isolate());
     55   }
     56   return result;
     57 }
     58 
     59 
     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];
     64   }
     65 
     66   // Set the first generation as unborn.
     67   tables_[0] = isolate()->heap()->undefined_value();
     68 }
     69 
     70 
     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);
     76     }
     77   }
     78 }
     79 
     80 
     81 void CompilationSubCache::Iterate(ObjectVisitor* v) {
     82   v->VisitPointers(&tables_[0], &tables_[generations_]);
     83 }
     84 
     85 
     86 void CompilationSubCache::Clear() {
     87   MemsetPointer(tables_, isolate()->heap()->undefined_value(), generations_);
     88 }
     89 
     90 
     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);
     98     }
     99   }
    100 }
    101 
    102 
    103 CompilationCacheScript::CompilationCacheScript(Isolate* isolate,
    104                                                int generations)
    105     : CompilationSubCache(isolate, generations),
    106       script_histogram_(NULL),
    107       script_histogram_initialized_(false) { }
    108 
    109 
    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,
    115     Handle<Object> name,
    116     int line_offset,
    117     int column_offset,
    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();
    125   }
    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())));
    136 }
    137 
    138 
    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
    142 // won't.
    143 Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(
    144     Handle<String> source,
    145     Handle<Object> name,
    146     int line_offset,
    147     int column_offset,
    148     bool is_shared_cross_origin,
    149     Handle<Context> context) {
    150   Object* result = NULL;
    151   int generation;
    152 
    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,
    165                       name,
    166                       line_offset,
    167                       column_offset,
    168                       is_shared_cross_origin)) {
    169           result = *function_info;
    170           break;
    171         }
    172       }
    173     }
    174   }
    175 
    176   if (!script_histogram_initialized_) {
    177     script_histogram_ = isolate()->stats_table()->CreateHistogram(
    178         "V8.ScriptCache",
    179         0,
    180         kScriptGenerations,
    181         kScriptGenerations + 1);
    182     script_histogram_initialized_ = true;
    183   }
    184 
    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);
    188   }
    189 
    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),
    195                                       isolate());
    196     ASSERT(HasOrigin(shared,
    197                      name,
    198                      line_offset,
    199                      column_offset,
    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();
    205     return shared;
    206   } else {
    207     isolate()->counters()->compilation_cache_misses()->Increment();
    208     return Handle<SharedFunctionInfo>::null();
    209   }
    210 }
    211 
    212 
    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();
    218   SetFirstTable(
    219       CompilationCacheTable::Put(table, source, context, function_info));
    220 }
    221 
    222 
    223 MaybeHandle<SharedFunctionInfo> CompilationCacheEval::Lookup(
    224     Handle<String> source,
    225     Handle<Context> context,
    226     StrictMode strict_mode,
    227     int scope_position) {
    228   HandleScope scope(isolate());
    229   // Make sure not to leak the table into the surrounding handle
    230   // scope. Otherwise, we risk keeping old tables around even after
    231   // having cleared the cache.
    232   Handle<Object> result = isolate()->factory()->undefined_value();
    233   int generation;
    234   for (generation = 0; generation < generations(); generation++) {
    235     Handle<CompilationCacheTable> table = GetTable(generation);
    236     result = table->LookupEval(source, context, strict_mode, scope_position);
    237     if (result->IsSharedFunctionInfo()) break;
    238   }
    239   if (result->IsSharedFunctionInfo()) {
    240     Handle<SharedFunctionInfo> function_info =
    241         Handle<SharedFunctionInfo>::cast(result);
    242     if (generation != 0) {
    243       Put(source, context, function_info, scope_position);
    244     }
    245     isolate()->counters()->compilation_cache_hits()->Increment();
    246     return scope.CloseAndEscape(function_info);
    247   } else {
    248     isolate()->counters()->compilation_cache_misses()->Increment();
    249     return MaybeHandle<SharedFunctionInfo>();
    250   }
    251 }
    252 
    253 
    254 void CompilationCacheEval::Put(Handle<String> source,
    255                                Handle<Context> context,
    256                                Handle<SharedFunctionInfo> function_info,
    257                                int scope_position) {
    258   HandleScope scope(isolate());
    259   Handle<CompilationCacheTable> table = GetFirstTable();
    260   table = CompilationCacheTable::PutEval(table, source, context,
    261                                          function_info, scope_position);
    262   SetFirstTable(table);
    263 }
    264 
    265 
    266 MaybeHandle<FixedArray> CompilationCacheRegExp::Lookup(
    267     Handle<String> source,
    268     JSRegExp::Flags flags) {
    269   HandleScope scope(isolate());
    270   // Make sure not to leak the table into the surrounding handle
    271   // scope. Otherwise, we risk keeping old tables around even after
    272   // having cleared the cache.
    273   Handle<Object> result = isolate()->factory()->undefined_value();
    274   int generation;
    275   for (generation = 0; generation < generations(); generation++) {
    276     Handle<CompilationCacheTable> table = GetTable(generation);
    277     result = table->LookupRegExp(source, flags);
    278     if (result->IsFixedArray()) break;
    279   }
    280   if (result->IsFixedArray()) {
    281     Handle<FixedArray> data = Handle<FixedArray>::cast(result);
    282     if (generation != 0) {
    283       Put(source, flags, data);
    284     }
    285     isolate()->counters()->compilation_cache_hits()->Increment();
    286     return scope.CloseAndEscape(data);
    287   } else {
    288     isolate()->counters()->compilation_cache_misses()->Increment();
    289     return MaybeHandle<FixedArray>();
    290   }
    291 }
    292 
    293 
    294 void CompilationCacheRegExp::Put(Handle<String> source,
    295                                  JSRegExp::Flags flags,
    296                                  Handle<FixedArray> data) {
    297   HandleScope scope(isolate());
    298   Handle<CompilationCacheTable> table = GetFirstTable();
    299   SetFirstTable(CompilationCacheTable::PutRegExp(table, source, flags, data));
    300 }
    301 
    302 
    303 void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) {
    304   if (!IsEnabled()) return;
    305 
    306   eval_global_.Remove(function_info);
    307   eval_contextual_.Remove(function_info);
    308   script_.Remove(function_info);
    309 }
    310 
    311 
    312 MaybeHandle<SharedFunctionInfo> CompilationCache::LookupScript(
    313     Handle<String> source,
    314     Handle<Object> name,
    315     int line_offset,
    316     int column_offset,
    317     bool is_shared_cross_origin,
    318     Handle<Context> context) {
    319   if (!IsEnabled()) return MaybeHandle<SharedFunctionInfo>();
    320 
    321   return script_.Lookup(source, name, line_offset, column_offset,
    322                         is_shared_cross_origin, context);
    323 }
    324 
    325 
    326 MaybeHandle<SharedFunctionInfo> CompilationCache::LookupEval(
    327     Handle<String> source,
    328     Handle<Context> context,
    329     StrictMode strict_mode,
    330     int scope_position) {
    331   if (!IsEnabled()) return MaybeHandle<SharedFunctionInfo>();
    332 
    333   MaybeHandle<SharedFunctionInfo> result;
    334   if (context->IsNativeContext()) {
    335     result = eval_global_.Lookup(
    336         source, context, strict_mode, scope_position);
    337   } else {
    338     ASSERT(scope_position != RelocInfo::kNoPosition);
    339     result = eval_contextual_.Lookup(
    340         source, context, strict_mode, scope_position);
    341   }
    342   return result;
    343 }
    344 
    345 
    346 MaybeHandle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source,
    347                                                   JSRegExp::Flags flags) {
    348   if (!IsEnabled()) return MaybeHandle<FixedArray>();
    349 
    350   return reg_exp_.Lookup(source, flags);
    351 }
    352 
    353 
    354 void CompilationCache::PutScript(Handle<String> source,
    355                                  Handle<Context> context,
    356                                  Handle<SharedFunctionInfo> function_info) {
    357   if (!IsEnabled()) return;
    358 
    359   script_.Put(source, context, function_info);
    360 }
    361 
    362 
    363 void CompilationCache::PutEval(Handle<String> source,
    364                                Handle<Context> context,
    365                                Handle<SharedFunctionInfo> function_info,
    366                                int scope_position) {
    367   if (!IsEnabled()) return;
    368 
    369   HandleScope scope(isolate());
    370   if (context->IsNativeContext()) {
    371     eval_global_.Put(source, context, function_info, scope_position);
    372   } else {
    373     ASSERT(scope_position != RelocInfo::kNoPosition);
    374     eval_contextual_.Put(source, context, function_info, scope_position);
    375   }
    376 }
    377 
    378 
    379 
    380 void CompilationCache::PutRegExp(Handle<String> source,
    381                                  JSRegExp::Flags flags,
    382                                  Handle<FixedArray> data) {
    383   if (!IsEnabled()) {
    384     return;
    385   }
    386 
    387   reg_exp_.Put(source, flags, data);
    388 }
    389 
    390 
    391 void CompilationCache::Clear() {
    392   for (int i = 0; i < kSubCacheCount; i++) {
    393     subcaches_[i]->Clear();
    394   }
    395 }
    396 
    397 
    398 void CompilationCache::Iterate(ObjectVisitor* v) {
    399   for (int i = 0; i < kSubCacheCount; i++) {
    400     subcaches_[i]->Iterate(v);
    401   }
    402 }
    403 
    404 
    405 void CompilationCache::IterateFunctions(ObjectVisitor* v) {
    406   for (int i = 0; i < kSubCacheCount; i++) {
    407     subcaches_[i]->IterateFunctions(v);
    408   }
    409 }
    410 
    411 
    412 void CompilationCache::MarkCompactPrologue() {
    413   for (int i = 0; i < kSubCacheCount; i++) {
    414     subcaches_[i]->Age();
    415   }
    416 }
    417 
    418 
    419 void CompilationCache::Enable() {
    420   enabled_ = true;
    421 }
    422 
    423 
    424 void CompilationCache::Disable() {
    425   enabled_ = false;
    426   Clear();
    427 }
    428 
    429 
    430 } }  // namespace v8::internal
    431