// Copyright 2011 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "src/compilation-cache.h" #include "src/counters.h" #include "src/factory.h" #include "src/globals.h" #include "src/objects-inl.h" namespace v8 { namespace internal { // The number of generations for each sub cache. static const int kRegExpGenerations = 2; // Initial size of each compilation cache table allocated. static const int kInitialCacheSize = 64; CompilationCache::CompilationCache(Isolate* isolate) : isolate_(isolate), script_(isolate, 1), eval_global_(isolate, 1), eval_contextual_(isolate, 1), reg_exp_(isolate, kRegExpGenerations), enabled_(true) { CompilationSubCache* subcaches[kSubCacheCount] = {&script_, &eval_global_, &eval_contextual_, ®_exp_}; for (int i = 0; i < kSubCacheCount; ++i) { subcaches_[i] = subcaches[i]; } } CompilationCache::~CompilationCache() {} Handle CompilationSubCache::GetTable(int generation) { DCHECK(generation < generations_); Handle result; if (tables_[generation]->IsUndefined(isolate())) { result = CompilationCacheTable::New(isolate(), kInitialCacheSize); tables_[generation] = *result; } else { CompilationCacheTable* table = CompilationCacheTable::cast(tables_[generation]); result = Handle(table, isolate()); } return result; } void CompilationSubCache::Age() { // Don't directly age single-generation caches. if (generations_ == 1) { if (!tables_[0]->IsUndefined(isolate())) { CompilationCacheTable::cast(tables_[0])->Age(); } return; } // Age the generations implicitly killing off the oldest. for (int i = generations_ - 1; i > 0; i--) { tables_[i] = tables_[i - 1]; } // Set the first generation as unborn. tables_[0] = isolate()->heap()->undefined_value(); } void CompilationSubCache::IterateFunctions(ObjectVisitor* v) { Object* undefined = isolate()->heap()->undefined_value(); for (int i = 0; i < generations_; i++) { if (tables_[i] != undefined) { reinterpret_cast(tables_[i])->IterateElements(v); } } } void CompilationSubCache::Iterate(ObjectVisitor* v) { v->VisitPointers(&tables_[0], &tables_[generations_]); } void CompilationSubCache::Clear() { MemsetPointer(tables_, isolate()->heap()->undefined_value(), generations_); } void CompilationSubCache::Remove(Handle function_info) { // Probe the script generation tables. Make sure not to leak handles // into the caller's handle scope. { HandleScope scope(isolate()); for (int generation = 0; generation < generations(); generation++) { Handle table = GetTable(generation); table->Remove(*function_info); } } } CompilationCacheScript::CompilationCacheScript(Isolate* isolate, int generations) : CompilationSubCache(isolate, generations) {} // 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. bool CompilationCacheScript::HasOrigin(Handle function_info, Handle name, int line_offset, int column_offset, ScriptOriginOptions resource_options) { Handle