// 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/globals.h" #include "src/heap/factory.h" #include "src/objects-inl.h" #include "src/objects/compilation-cache-inl.h" #include "src/visitors.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), eval_global_(isolate), eval_contextual_(isolate), 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] = ReadOnlyRoots(isolate()).undefined_value(); } void CompilationSubCache::Iterate(RootVisitor* v) { v->VisitRootPointers(Root::kCompilationCache, nullptr, &tables_[0], &tables_[generations_]); } void CompilationSubCache::Clear() { MemsetPointer(tables_, ReadOnlyRoots(isolate()).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) : CompilationSubCache(isolate, 1) {} // 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, MaybeHandle maybe_name, int line_offset, int column_offset, ScriptOriginOptions resource_options) { Handle