// 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/codegen/compilation-cache.h" #include "src/codegen/script-details.h" #include "src/common/globals.h" #include "src/heap/factory.h" #include "src/logging/counters.h" #include "src/logging/log.h" #include "src/objects/compilation-cache-table-inl.h" #include "src/objects/objects-inl.h" #include "src/objects/slots.h" #include "src/objects/visitors.h" #include "src/utils/ostreams.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_script_and_eval_(true) { CompilationSubCache* subcaches[kSubCacheCount] = { &script_, &eval_global_, &eval_contextual_, ®_exp_}; for (int i = 0; i < kSubCacheCount; ++i) { subcaches_[i] = subcaches[i]; } } Handle CompilationSubCache::GetTable(int generation) { DCHECK_LT(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; } // static void CompilationSubCache::AgeByGeneration(CompilationSubCache* c) { DCHECK_GT(c->generations(), 1); // Age the generations implicitly killing off the oldest. for (int i = c->generations() - 1; i > 0; i--) { c->tables_[i] = c->tables_[i - 1]; } // Set the first generation as unborn. c->tables_[0] = ReadOnlyRoots(c->isolate()).undefined_value(); } // static void CompilationSubCache::AgeCustom(CompilationSubCache* c) { DCHECK_EQ(c->generations(), 1); if (c->tables_[0].IsUndefined(c->isolate())) return; CompilationCacheTable::cast(c->tables_[0]).Age(c->isolate()); } void CompilationCacheScript::Age() { if (FLAG_isolate_script_cache_ageing) AgeCustom(this); } void CompilationCacheEval::Age() { AgeCustom(this); } void CompilationCacheRegExp::Age() { AgeByGeneration(this); } void CompilationSubCache::Iterate(RootVisitor* v) { v->VisitRootPointers(Root::kCompilationCache, nullptr, FullObjectSlot(&tables_[0]), FullObjectSlot(&tables_[generations()])); } void CompilationSubCache::Clear() { MemsetPointer(reinterpret_cast(tables_), ReadOnlyRoots(isolate()).undefined_value().ptr(), 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) {} namespace { // 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 HasOrigin(Isolate* isolate, Handle function_info, const ScriptDetails& script_details) { Handle