compilation-cache.cc 16.8 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "v8.h"

30
#include "assembler.h"
31
#include "compilation-cache.h"
32
#include "serialize.h"
33

34 35
namespace v8 {
namespace internal {
36

37 38

// The number of generations for each sub cache.
39 40
// The number of ScriptGenerations is carefully chosen based on histograms.
// See issue 458: http://code.google.com/p/v8/issues/detail?id=458
41 42 43 44 45
static const int kScriptGenerations = 5;
static const int kEvalGlobalGenerations = 2;
static const int kEvalContextualGenerations = 2;
static const int kRegExpGenerations = 2;

46
// Initial size of each compilation cache table allocated.
47 48 49
static const int kInitialCacheSize = 64;


50 51 52 53 54 55
CompilationCache::CompilationCache(Isolate* isolate)
    : isolate_(isolate),
      script_(isolate, kScriptGenerations),
      eval_global_(isolate, kEvalGlobalGenerations),
      eval_contextual_(isolate, kEvalContextualGenerations),
      reg_exp_(isolate, kRegExpGenerations),
56
      enabled_(true) {
57 58 59 60
  CompilationSubCache* subcaches[kSubCacheCount] =
    {&script_, &eval_global_, &eval_contextual_, &reg_exp_};
  for (int i = 0; i < kSubCacheCount; ++i) {
    subcaches_[i] = subcaches[i];
61
  }
62
}
63 64


65
CompilationCache::~CompilationCache() {}
66

67

68
static Handle<CompilationCacheTable> AllocateTable(Isolate* isolate, int size) {
69
  CALL_HEAP_FUNCTION(isolate,
70
                     CompilationCacheTable::Allocate(isolate->heap(), size),
71 72 73 74
                     CompilationCacheTable);
}


75 76
Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) {
  ASSERT(generation < generations_);
77
  Handle<CompilationCacheTable> result;
78
  if (tables_[generation]->IsUndefined()) {
79
    result = AllocateTable(isolate(), kInitialCacheSize);
80
    tables_[generation] = *result;
81
  } else {
82 83
    CompilationCacheTable* table =
        CompilationCacheTable::cast(tables_[generation]);
84
    result = Handle<CompilationCacheTable>(table, isolate());
85 86 87 88
  }
  return result;
}

89

90 91 92 93
void CompilationSubCache::Age() {
  // Age the generations implicitly killing off the oldest.
  for (int i = generations_ - 1; i > 0; i--) {
    tables_[i] = tables_[i - 1];
94
  }
95 96

  // Set the first generation as unborn.
97
  tables_[0] = isolate()->heap()->undefined_value();
98 99 100
}


101
void CompilationSubCache::IterateFunctions(ObjectVisitor* v) {
102
  Object* undefined = isolate()->heap()->undefined_value();
103 104 105 106 107 108 109 110
  for (int i = 0; i < generations_; i++) {
    if (tables_[i] != undefined) {
      reinterpret_cast<CompilationCacheTable*>(tables_[i])->IterateElements(v);
    }
  }
}


111 112 113 114 115 116
void CompilationSubCache::Iterate(ObjectVisitor* v) {
  v->VisitPointers(&tables_[0], &tables_[generations_]);
}


void CompilationSubCache::Clear() {
117
  MemsetPointer(tables_, isolate()->heap()->undefined_value(), generations_);
118 119 120
}


121 122 123
void CompilationSubCache::Remove(Handle<SharedFunctionInfo> function_info) {
  // Probe the script generation tables. Make sure not to leak handles
  // into the caller's handle scope.
124
  { HandleScope scope(isolate());
125 126 127 128 129 130 131 132
    for (int generation = 0; generation < generations(); generation++) {
      Handle<CompilationCacheTable> table = GetTable(generation);
      table->Remove(*function_info);
    }
  }
}


133 134 135 136 137
CompilationCacheScript::CompilationCacheScript(Isolate* isolate,
                                               int generations)
    : CompilationSubCache(isolate, generations),
      script_histogram_(NULL),
      script_histogram_initialized_(false) { }
138 139


140 141 142
// We only re-use a cached function for some script source code if the
// script originates from the same place. This is to avoid issues
// when reporting errors, etc.
143 144 145 146
bool CompilationCacheScript::HasOrigin(
    Handle<SharedFunctionInfo> function_info,
    Handle<Object> name,
    int line_offset,
147 148
    int column_offset,
    bool is_shared_cross_origin) {
149
  Handle<Script> script =
150
      Handle<Script>(Script::cast(function_info->script()), isolate());
151 152 153 154 155 156 157 158 159 160
  // If the script name isn't set, the boilerplate script should have
  // an undefined name to have the same origin.
  if (name.is_null()) {
    return script->name()->IsUndefined();
  }
  // Do the fast bailout checks first.
  if (line_offset != script->line_offset()->value()) return false;
  if (column_offset != script->column_offset()->value()) return false;
  // Check that both names are strings. If not, no match.
  if (!name->IsString() || !script->name()->IsString()) return false;
161 162
  // Were both scripts tagged by the embedder as being shared cross-origin?
  if (is_shared_cross_origin != script->is_shared_cross_origin()) return false;
163 164 165 166 167
  // Compare the two name strings for equality.
  return String::cast(*name)->Equals(String::cast(script->name()));
}


168 169 170 171
// TODO(245): Need to allow identical code from different contexts to
// be cached in the same script generation. Currently the first use
// will be cached, but subsequent code from different source / line
// won't.
172 173 174 175 176
Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(
    Handle<String> source,
    Handle<Object> name,
    int line_offset,
    int column_offset,
177
    bool is_shared_cross_origin,
178
    Handle<Context> context) {
179
  Object* result = NULL;
180
  int generation;
181 182 183

  // Probe the script generation tables. Make sure not to leak handles
  // into the caller's handle scope.
184
  { HandleScope scope(isolate());
185
    for (generation = 0; generation < generations(); generation++) {
186
      Handle<CompilationCacheTable> table = GetTable(generation);
187
      Handle<Object> probe(table->Lookup(*source, *context), isolate());
188 189 190
      if (probe->IsSharedFunctionInfo()) {
        Handle<SharedFunctionInfo> function_info =
            Handle<SharedFunctionInfo>::cast(probe);
191
        // Break when we've found a suitable shared function info that
192
        // matches the origin.
193 194 195 196 197
        if (HasOrigin(function_info,
                      name,
                      line_offset,
                      column_offset,
                      is_shared_cross_origin)) {
198
          result = *function_info;
199 200 201 202 203 204
          break;
        }
      }
    }
  }

205
  if (!script_histogram_initialized_) {
206
    script_histogram_ = isolate()->stats_table()->CreateHistogram(
207 208 209 210 211 212
        "V8.ScriptCache",
        0,
        kScriptGenerations,
        kScriptGenerations + 1);
    script_histogram_initialized_ = true;
  }
213

214
  if (script_histogram_ != NULL) {
215
    // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss.
216
    isolate()->stats_table()->AddHistogramSample(script_histogram_, generation);
217 218
  }

whesse@chromium.org's avatar
whesse@chromium.org committed
219
  // Once outside the manacles of the handle scope, we need to recheck
220 221 222
  // to see if we actually found a cached script. If so, we return a
  // handle created in the caller's handle scope.
  if (result != NULL) {
223 224
    Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result),
                                      isolate());
225 226 227 228 229
    ASSERT(HasOrigin(shared,
                     name,
                     line_offset,
                     column_offset,
                     is_shared_cross_origin));
230 231
    // If the script was found in a later generation, we promote it to
    // the first generation to let it survive longer in the cache.
232
    if (generation != 0) Put(source, context, shared);
233
    isolate()->counters()->compilation_cache_hits()->Increment();
234
    return shared;
235
  } else {
236
    isolate()->counters()->compilation_cache_misses()->Increment();
237
    return Handle<SharedFunctionInfo>::null();
238
  }
239 240 241
}


242
MaybeObject* CompilationCacheScript::TryTablePut(
243
    Handle<String> source,
244
    Handle<Context> context,
245 246
    Handle<SharedFunctionInfo> function_info) {
  Handle<CompilationCacheTable> table = GetFirstTable();
247
  return table->Put(*source, *context, *function_info);
248 249 250
}


251 252
Handle<CompilationCacheTable> CompilationCacheScript::TablePut(
    Handle<String> source,
253
    Handle<Context> context,
254
    Handle<SharedFunctionInfo> function_info) {
255
  CALL_HEAP_FUNCTION(isolate(),
256
                     TryTablePut(source, context, function_info),
257
                     CompilationCacheTable);
258 259 260
}


261
void CompilationCacheScript::Put(Handle<String> source,
262
                                 Handle<Context> context,
263
                                 Handle<SharedFunctionInfo> function_info) {
264
  HandleScope scope(isolate());
265
  SetFirstTable(TablePut(source, context, function_info));
266 267 268
}


269
Handle<SharedFunctionInfo> CompilationCacheEval::Lookup(
270 271
    Handle<String> source,
    Handle<Context> context,
272
    LanguageMode language_mode,
273
    int scope_position) {
274 275 276 277 278
  // Make sure not to leak the table into the surrounding handle
  // scope. Otherwise, we risk keeping old tables around even after
  // having cleared the cache.
  Object* result = NULL;
  int generation;
279
  { HandleScope scope(isolate());
280 281
    for (generation = 0; generation < generations(); generation++) {
      Handle<CompilationCacheTable> table = GetTable(generation);
282
      result = table->LookupEval(
283
          *source, *context, language_mode, scope_position);
284
      if (result->IsSharedFunctionInfo()) {
285 286 287 288
        break;
      }
    }
  }
289 290
  if (result->IsSharedFunctionInfo()) {
    Handle<SharedFunctionInfo>
291
        function_info(SharedFunctionInfo::cast(result), isolate());
292
    if (generation != 0) {
293
      Put(source, context, function_info, scope_position);
294
    }
295
    isolate()->counters()->compilation_cache_hits()->Increment();
296
    return function_info;
297
  } else {
298
    isolate()->counters()->compilation_cache_misses()->Increment();
299
    return Handle<SharedFunctionInfo>::null();
300 301 302 303
  }
}


304
MaybeObject* CompilationCacheEval::TryTablePut(
305 306
    Handle<String> source,
    Handle<Context> context,
307 308
    Handle<SharedFunctionInfo> function_info,
    int scope_position) {
309
  Handle<CompilationCacheTable> table = GetFirstTable();
310
  return table->PutEval(*source, *context, *function_info, scope_position);
311 312 313
}


314 315 316
Handle<CompilationCacheTable> CompilationCacheEval::TablePut(
    Handle<String> source,
    Handle<Context> context,
317 318
    Handle<SharedFunctionInfo> function_info,
    int scope_position) {
319
  CALL_HEAP_FUNCTION(isolate(),
320 321
                     TryTablePut(
                         source, context, function_info, scope_position),
322 323 324 325
                     CompilationCacheTable);
}


326 327
void CompilationCacheEval::Put(Handle<String> source,
                               Handle<Context> context,
328 329
                               Handle<SharedFunctionInfo> function_info,
                               int scope_position) {
330
  HandleScope scope(isolate());
331
  SetFirstTable(TablePut(source, context, function_info, scope_position));
332 333 334 335 336 337 338 339 340 341
}


Handle<FixedArray> CompilationCacheRegExp::Lookup(Handle<String> source,
                                                  JSRegExp::Flags flags) {
  // Make sure not to leak the table into the surrounding handle
  // scope. Otherwise, we risk keeping old tables around even after
  // having cleared the cache.
  Object* result = NULL;
  int generation;
342
  { HandleScope scope(isolate());
343 344 345 346 347 348 349 350 351
    for (generation = 0; generation < generations(); generation++) {
      Handle<CompilationCacheTable> table = GetTable(generation);
      result = table->LookupRegExp(*source, flags);
      if (result->IsFixedArray()) {
        break;
      }
    }
  }
  if (result->IsFixedArray()) {
352
    Handle<FixedArray> data(FixedArray::cast(result), isolate());
353 354 355
    if (generation != 0) {
      Put(source, flags, data);
    }
356
    isolate()->counters()->compilation_cache_hits()->Increment();
357 358
    return data;
  } else {
359
    isolate()->counters()->compilation_cache_misses()->Increment();
360 361 362 363 364
    return Handle<FixedArray>::null();
  }
}


365
MaybeObject* CompilationCacheRegExp::TryTablePut(
366 367 368 369 370 371 372 373
    Handle<String> source,
    JSRegExp::Flags flags,
    Handle<FixedArray> data) {
  Handle<CompilationCacheTable> table = GetFirstTable();
  return table->PutRegExp(*source, flags, *data);
}


374 375 376 377
Handle<CompilationCacheTable> CompilationCacheRegExp::TablePut(
    Handle<String> source,
    JSRegExp::Flags flags,
    Handle<FixedArray> data) {
378
  CALL_HEAP_FUNCTION(isolate(),
379 380
                     TryTablePut(source, flags, data),
                     CompilationCacheTable);
381 382 383
}


384 385 386
void CompilationCacheRegExp::Put(Handle<String> source,
                                 JSRegExp::Flags flags,
                                 Handle<FixedArray> data) {
387
  HandleScope scope(isolate());
388
  SetFirstTable(TablePut(source, flags, data));
389 390 391
}


392 393 394
void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) {
  if (!IsEnabled()) return;

395 396 397
  eval_global_.Remove(function_info);
  eval_contextual_.Remove(function_info);
  script_.Remove(function_info);
398 399 400
}


401 402 403 404 405
Handle<SharedFunctionInfo> CompilationCache::LookupScript(
    Handle<String> source,
    Handle<Object> name,
    int line_offset,
    int column_offset,
406
    bool is_shared_cross_origin,
407
    Handle<Context> context) {
408
  if (!IsEnabled()) {
409
    return Handle<SharedFunctionInfo>::null();
410 411
  }

412 413 414 415 416 417
  return script_.Lookup(source,
                        name,
                        line_offset,
                        column_offset,
                        is_shared_cross_origin,
                        context);
418 419 420
}


421 422 423 424
Handle<SharedFunctionInfo> CompilationCache::LookupEval(
    Handle<String> source,
    Handle<Context> context,
    bool is_global,
425
    LanguageMode language_mode,
426
    int scope_position) {
427
  if (!IsEnabled()) {
428
    return Handle<SharedFunctionInfo>::null();
429 430
  }

431
  Handle<SharedFunctionInfo> result;
432
  if (is_global) {
433 434
    result = eval_global_.Lookup(
        source, context, language_mode, scope_position);
435
  } else {
436 437
    ASSERT(scope_position != RelocInfo::kNoPosition);
    result = eval_contextual_.Lookup(
438
        source, context, language_mode, scope_position);
439 440 441 442 443
  }
  return result;
}


444 445
Handle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source,
                                                  JSRegExp::Flags flags) {
446 447 448 449
  if (!IsEnabled()) {
    return Handle<FixedArray>::null();
  }

450
  return reg_exp_.Lookup(source, flags);
451 452 453
}


454
void CompilationCache::PutScript(Handle<String> source,
455
                                 Handle<Context> context,
456
                                 Handle<SharedFunctionInfo> function_info) {
457 458 459 460
  if (!IsEnabled()) {
    return;
  }

461
  script_.Put(source, context, function_info);
462 463 464 465 466
}


void CompilationCache::PutEval(Handle<String> source,
                               Handle<Context> context,
467
                               bool is_global,
468 469
                               Handle<SharedFunctionInfo> function_info,
                               int scope_position) {
470 471 472 473
  if (!IsEnabled()) {
    return;
  }

474
  HandleScope scope(isolate());
475
  if (is_global) {
476
    eval_global_.Put(source, context, function_info, scope_position);
477
  } else {
478 479
    ASSERT(scope_position != RelocInfo::kNoPosition);
    eval_contextual_.Put(source, context, function_info, scope_position);
480
  }
481 482 483
}


484 485 486 487

void CompilationCache::PutRegExp(Handle<String> source,
                                 JSRegExp::Flags flags,
                                 Handle<FixedArray> data) {
488 489 490 491
  if (!IsEnabled()) {
    return;
  }

492
  reg_exp_.Put(source, flags, data);
493 494 495
}


496
void CompilationCache::Clear() {
497
  for (int i = 0; i < kSubCacheCount; i++) {
498
    subcaches_[i]->Clear();
499 500 501
  }
}

502

503 504
void CompilationCache::Iterate(ObjectVisitor* v) {
  for (int i = 0; i < kSubCacheCount; i++) {
505
    subcaches_[i]->Iterate(v);
506
  }
507 508 509
}


510
void CompilationCache::IterateFunctions(ObjectVisitor* v) {
511
  for (int i = 0; i < kSubCacheCount; i++) {
512
    subcaches_[i]->IterateFunctions(v);
513
  }
514 515 516 517
}


void CompilationCache::MarkCompactPrologue() {
518
  for (int i = 0; i < kSubCacheCount; i++) {
519
    subcaches_[i]->Age();
520
  }
521 522 523
}


524
void CompilationCache::Enable() {
525
  enabled_ = true;
526 527 528 529
}


void CompilationCache::Disable() {
530
  enabled_ = false;
531 532 533 534
  Clear();
}


535
} }  // namespace v8::internal