compilation-cache.cc 16.2 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
void CompilationSubCache::Age() {
  // Age the generations implicitly killing off the oldest.
  for (int i = generations_ - 1; i > 0; i--) {
    tables_[i] = tables_[i - 1];
93
  }
94 95

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


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


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


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


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


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


139 140 141
// 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.
142 143 144 145 146
bool CompilationCacheScript::HasOrigin(
    Handle<SharedFunctionInfo> function_info,
    Handle<Object> name,
    int line_offset,
    int column_offset) {
147
  Handle<Script> script =
148
      Handle<Script>(Script::cast(function_info->script()), isolate());
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  // 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;
  // Compare the two name strings for equality.
  return String::cast(*name)->Equals(String::cast(script->name()));
}


164 165 166 167
// 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.
168 169 170 171 172 173
Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(
    Handle<String> source,
    Handle<Object> name,
    int line_offset,
    int column_offset,
    Handle<Context> context) {
174
  Object* result = NULL;
175
  int generation;
176 177 178

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

196
  if (!script_histogram_initialized_) {
197
    script_histogram_ = isolate()->stats_table()->CreateHistogram(
198 199 200 201 202 203
        "V8.ScriptCache",
        0,
        kScriptGenerations,
        kScriptGenerations + 1);
    script_histogram_initialized_ = true;
  }
204

205
  if (script_histogram_ != NULL) {
206
    // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss.
207
    isolate()->stats_table()->AddHistogramSample(script_histogram_, generation);
208 209
  }

whesse@chromium.org's avatar
whesse@chromium.org committed
210
  // Once outside the manacles of the handle scope, we need to recheck
211 212 213
  // 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) {
214 215
    Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result),
                                      isolate());
216
    ASSERT(HasOrigin(shared, name, line_offset, column_offset));
217 218
    // If the script was found in a later generation, we promote it to
    // the first generation to let it survive longer in the cache.
219
    if (generation != 0) Put(source, context, shared);
220
    isolate()->counters()->compilation_cache_hits()->Increment();
221
    return shared;
222
  } else {
223
    isolate()->counters()->compilation_cache_misses()->Increment();
224
    return Handle<SharedFunctionInfo>::null();
225
  }
226 227 228
}


229
MaybeObject* CompilationCacheScript::TryTablePut(
230
    Handle<String> source,
231
    Handle<Context> context,
232 233
    Handle<SharedFunctionInfo> function_info) {
  Handle<CompilationCacheTable> table = GetFirstTable();
234
  return table->Put(*source, *context, *function_info);
235 236 237
}


238 239
Handle<CompilationCacheTable> CompilationCacheScript::TablePut(
    Handle<String> source,
240
    Handle<Context> context,
241
    Handle<SharedFunctionInfo> function_info) {
242
  CALL_HEAP_FUNCTION(isolate(),
243
                     TryTablePut(source, context, function_info),
244
                     CompilationCacheTable);
245 246 247
}


248
void CompilationCacheScript::Put(Handle<String> source,
249
                                 Handle<Context> context,
250
                                 Handle<SharedFunctionInfo> function_info) {
251
  HandleScope scope(isolate());
252
  SetFirstTable(TablePut(source, context, function_info));
253 254 255
}


256
Handle<SharedFunctionInfo> CompilationCacheEval::Lookup(
257 258
    Handle<String> source,
    Handle<Context> context,
259
    LanguageMode language_mode,
260
    int scope_position) {
261 262 263 264 265
  // 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;
266
  { HandleScope scope(isolate());
267 268
    for (generation = 0; generation < generations(); generation++) {
      Handle<CompilationCacheTable> table = GetTable(generation);
269
      result = table->LookupEval(
270
          *source, *context, language_mode, scope_position);
271
      if (result->IsSharedFunctionInfo()) {
272 273 274 275
        break;
      }
    }
  }
276 277
  if (result->IsSharedFunctionInfo()) {
    Handle<SharedFunctionInfo>
278
        function_info(SharedFunctionInfo::cast(result), isolate());
279
    if (generation != 0) {
280
      Put(source, context, function_info, scope_position);
281
    }
282
    isolate()->counters()->compilation_cache_hits()->Increment();
283
    return function_info;
284
  } else {
285
    isolate()->counters()->compilation_cache_misses()->Increment();
286
    return Handle<SharedFunctionInfo>::null();
287 288 289 290
  }
}


291
MaybeObject* CompilationCacheEval::TryTablePut(
292 293
    Handle<String> source,
    Handle<Context> context,
294 295
    Handle<SharedFunctionInfo> function_info,
    int scope_position) {
296
  Handle<CompilationCacheTable> table = GetFirstTable();
297
  return table->PutEval(*source, *context, *function_info, scope_position);
298 299 300
}


301 302 303
Handle<CompilationCacheTable> CompilationCacheEval::TablePut(
    Handle<String> source,
    Handle<Context> context,
304 305
    Handle<SharedFunctionInfo> function_info,
    int scope_position) {
306
  CALL_HEAP_FUNCTION(isolate(),
307 308
                     TryTablePut(
                         source, context, function_info, scope_position),
309 310 311 312
                     CompilationCacheTable);
}


313 314
void CompilationCacheEval::Put(Handle<String> source,
                               Handle<Context> context,
315 316
                               Handle<SharedFunctionInfo> function_info,
                               int scope_position) {
317
  HandleScope scope(isolate());
318
  SetFirstTable(TablePut(source, context, function_info, scope_position));
319 320 321 322 323 324 325 326 327 328
}


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;
329
  { HandleScope scope(isolate());
330 331 332 333 334 335 336 337 338
    for (generation = 0; generation < generations(); generation++) {
      Handle<CompilationCacheTable> table = GetTable(generation);
      result = table->LookupRegExp(*source, flags);
      if (result->IsFixedArray()) {
        break;
      }
    }
  }
  if (result->IsFixedArray()) {
339
    Handle<FixedArray> data(FixedArray::cast(result), isolate());
340 341 342
    if (generation != 0) {
      Put(source, flags, data);
    }
343
    isolate()->counters()->compilation_cache_hits()->Increment();
344 345
    return data;
  } else {
346
    isolate()->counters()->compilation_cache_misses()->Increment();
347 348 349 350 351
    return Handle<FixedArray>::null();
  }
}


352
MaybeObject* CompilationCacheRegExp::TryTablePut(
353 354 355 356 357 358 359 360
    Handle<String> source,
    JSRegExp::Flags flags,
    Handle<FixedArray> data) {
  Handle<CompilationCacheTable> table = GetFirstTable();
  return table->PutRegExp(*source, flags, *data);
}


361 362 363 364
Handle<CompilationCacheTable> CompilationCacheRegExp::TablePut(
    Handle<String> source,
    JSRegExp::Flags flags,
    Handle<FixedArray> data) {
365
  CALL_HEAP_FUNCTION(isolate(),
366 367
                     TryTablePut(source, flags, data),
                     CompilationCacheTable);
368 369 370
}


371 372 373
void CompilationCacheRegExp::Put(Handle<String> source,
                                 JSRegExp::Flags flags,
                                 Handle<FixedArray> data) {
374
  HandleScope scope(isolate());
375
  SetFirstTable(TablePut(source, flags, data));
376 377 378
}


379 380 381
void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) {
  if (!IsEnabled()) return;

382 383 384
  eval_global_.Remove(function_info);
  eval_contextual_.Remove(function_info);
  script_.Remove(function_info);
385 386 387
}


388 389 390 391 392 393
Handle<SharedFunctionInfo> CompilationCache::LookupScript(
    Handle<String> source,
    Handle<Object> name,
    int line_offset,
    int column_offset,
    Handle<Context> context) {
394
  if (!IsEnabled()) {
395
    return Handle<SharedFunctionInfo>::null();
396 397
  }

398
  return script_.Lookup(source, name, line_offset, column_offset, context);
399 400 401
}


402 403 404 405
Handle<SharedFunctionInfo> CompilationCache::LookupEval(
    Handle<String> source,
    Handle<Context> context,
    bool is_global,
406
    LanguageMode language_mode,
407
    int scope_position) {
408
  if (!IsEnabled()) {
409
    return Handle<SharedFunctionInfo>::null();
410 411
  }

412
  Handle<SharedFunctionInfo> result;
413
  if (is_global) {
414 415
    result = eval_global_.Lookup(
        source, context, language_mode, scope_position);
416
  } else {
417 418
    ASSERT(scope_position != RelocInfo::kNoPosition);
    result = eval_contextual_.Lookup(
419
        source, context, language_mode, scope_position);
420 421 422 423 424
  }
  return result;
}


425 426
Handle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source,
                                                  JSRegExp::Flags flags) {
427 428 429 430
  if (!IsEnabled()) {
    return Handle<FixedArray>::null();
  }

431
  return reg_exp_.Lookup(source, flags);
432 433 434
}


435
void CompilationCache::PutScript(Handle<String> source,
436
                                 Handle<Context> context,
437
                                 Handle<SharedFunctionInfo> function_info) {
438 439 440 441
  if (!IsEnabled()) {
    return;
  }

442
  script_.Put(source, context, function_info);
443 444 445 446 447
}


void CompilationCache::PutEval(Handle<String> source,
                               Handle<Context> context,
448
                               bool is_global,
449 450
                               Handle<SharedFunctionInfo> function_info,
                               int scope_position) {
451 452 453 454
  if (!IsEnabled()) {
    return;
  }

455
  HandleScope scope(isolate());
456
  if (is_global) {
457
    eval_global_.Put(source, context, function_info, scope_position);
458
  } else {
459 460
    ASSERT(scope_position != RelocInfo::kNoPosition);
    eval_contextual_.Put(source, context, function_info, scope_position);
461
  }
462 463 464
}


465 466 467 468

void CompilationCache::PutRegExp(Handle<String> source,
                                 JSRegExp::Flags flags,
                                 Handle<FixedArray> data) {
469 470 471 472
  if (!IsEnabled()) {
    return;
  }

473
  reg_exp_.Put(source, flags, data);
474 475 476
}


477
void CompilationCache::Clear() {
478
  for (int i = 0; i < kSubCacheCount; i++) {
479
    subcaches_[i]->Clear();
480 481 482
  }
}

483

484 485
void CompilationCache::Iterate(ObjectVisitor* v) {
  for (int i = 0; i < kSubCacheCount; i++) {
486
    subcaches_[i]->Iterate(v);
487
  }
488 489 490
}


491
void CompilationCache::IterateFunctions(ObjectVisitor* v) {
492
  for (int i = 0; i < kSubCacheCount; i++) {
493
    subcaches_[i]->IterateFunctions(v);
494
  }
495 496 497 498
}


void CompilationCache::MarkCompactPrologue() {
499
  for (int i = 0; i < kSubCacheCount; i++) {
500
    subcaches_[i]->Age();
501
  }
502 503 504
}


505
void CompilationCache::Enable() {
506
  enabled_ = true;
507 508 509 510
}


void CompilationCache::Disable() {
511
  enabled_ = false;
512 513 514 515
  Clear();
}


516
} }  // namespace v8::internal