// Copyright 2008 the V8 project authors. All rights reserved. // 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" #include "compilation-cache.h" namespace v8 { namespace internal { // The number of sub caches covering the different types to cache. static const int kSubCacheCount = 4; // The number of generations for each sub cache. #if defined(ANDROID) static const int kScriptGenerations = 1; static const int kEvalGlobalGenerations = 1; static const int kEvalContextualGenerations = 1; static const int kRegExpGenerations = 1; #else // The number of ScriptGenerations is carefully chosen based on histograms. // See issue 458: http://code.google.com/p/v8/issues/detail?id=458 static const int kScriptGenerations = 5; static const int kEvalGlobalGenerations = 2; static const int kEvalContextualGenerations = 2; static const int kRegExpGenerations = 2; #endif // Initial size of each compilation cache table allocated. static const int kInitialCacheSize = 64; // The compilation cache consists of several generational sub-caches which uses // this class as a base class. A sub-cache contains a compilation cache tables // for each generation of the sub-cache. Since the same source code string has // different compiled code for scripts and evals, we use separate sub-caches // for different compilation modes, to avoid retrieving the wrong result. class CompilationSubCache { public: explicit CompilationSubCache(int generations): generations_(generations) { tables_ = NewArray(generations); } ~CompilationSubCache() { DeleteArray(tables_); } // Get the compilation cache tables for a specific generation. Handle GetTable(int generation); // Age the sub-cache by evicting the oldest generation and creating a new // young generation. void Age(); // GC support. void Iterate(ObjectVisitor* v); // Clear this sub-cache evicting all its content. void Clear(); // Number of generations in this sub-cache. inline int generations() { return generations_; } private: int generations_; // Number of generations. Object** tables_; // Compilation cache tables - one for each generation. DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache); }; // Sub-cache for scripts. class CompilationCacheScript : public CompilationSubCache { public: explicit CompilationCacheScript(int generations) : CompilationSubCache(generations) { } Handle Lookup(Handle source, Handle name, int line_offset, int column_offset); void Put(Handle source, Handle boilerplate); private: bool HasOrigin(Handle boilerplate, Handle name, int line_offset, int column_offset); DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript); }; // Sub-cache for eval scripts. class CompilationCacheEval: public CompilationSubCache { public: explicit CompilationCacheEval(int generations) : CompilationSubCache(generations) { } Handle Lookup(Handle source, Handle context); void Put(Handle source, Handle context, Handle boilerplate); DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval); }; // Sub-cache for regular expressions. class CompilationCacheRegExp: public CompilationSubCache { public: explicit CompilationCacheRegExp(int generations) : CompilationSubCache(generations) { } Handle Lookup(Handle source, JSRegExp::Flags flags); void Put(Handle source, JSRegExp::Flags flags, Handle data); DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp); }; // Statically allocate all the sub-caches. static CompilationCacheScript script(kScriptGenerations); static CompilationCacheEval eval_global(kEvalGlobalGenerations); static CompilationCacheEval eval_contextual(kEvalContextualGenerations); static CompilationCacheRegExp reg_exp(kRegExpGenerations); static CompilationSubCache* subcaches[kSubCacheCount] = {&script, &eval_global, &eval_contextual, ®_exp}; // Current enable state of the compilation cache. static bool enabled = true; static inline bool IsEnabled() { return FLAG_compilation_cache && enabled; } static Handle AllocateTable(int size) { CALL_HEAP_FUNCTION(CompilationCacheTable::Allocate(size), CompilationCacheTable); } Handle CompilationSubCache::GetTable(int generation) { ASSERT(generation < generations_); Handle result; if (tables_[generation]->IsUndefined()) { result = AllocateTable(kInitialCacheSize); tables_[generation] = *result; } else { CompilationCacheTable* table = CompilationCacheTable::cast(tables_[generation]); result = Handle(table); } return result; } void CompilationSubCache::Age() { // 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] = Heap::undefined_value(); } void CompilationSubCache::Iterate(ObjectVisitor* v) { v->VisitPointers(&tables_[0], &tables_[generations_]); } void CompilationSubCache::Clear() { for (int i = 0; i < generations_; i++) { tables_[i] = Heap::undefined_value(); } } // 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 boilerplate, Handle name, int line_offset, int column_offset) { Handle