Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "compilation-cache.h" |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 31 | #include "serialize.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | |
| 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | // The number of sub caches covering the different types to cache. |
| 37 | static const int kSubCacheCount = 4; |
| 38 | |
| 39 | // The number of generations for each sub cache. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 40 | // The number of ScriptGenerations is carefully chosen based on histograms. |
| 41 | // See issue 458: http://code.google.com/p/v8/issues/detail?id=458 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | static const int kScriptGenerations = 5; |
| 43 | static const int kEvalGlobalGenerations = 2; |
| 44 | static const int kEvalContextualGenerations = 2; |
| 45 | static const int kRegExpGenerations = 2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 47 | // Initial size of each compilation cache table allocated. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | static const int kInitialCacheSize = 64; |
| 49 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 50 | // Index for the first generation in the cache. |
| 51 | static const int kFirstGeneration = 0; |
| 52 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 53 | // The compilation cache consists of several generational sub-caches which uses |
| 54 | // this class as a base class. A sub-cache contains a compilation cache tables |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 55 | // for each generation of the sub-cache. Since the same source code string has |
| 56 | // different compiled code for scripts and evals, we use separate sub-caches |
| 57 | // for different compilation modes, to avoid retrieving the wrong result. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | class CompilationSubCache { |
| 59 | public: |
| 60 | explicit CompilationSubCache(int generations): generations_(generations) { |
| 61 | tables_ = NewArray<Object*>(generations); |
| 62 | } |
| 63 | |
| 64 | ~CompilationSubCache() { DeleteArray(tables_); } |
| 65 | |
| 66 | // Get the compilation cache tables for a specific generation. |
| 67 | Handle<CompilationCacheTable> GetTable(int generation); |
| 68 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 69 | // Accessors for first generation. |
| 70 | Handle<CompilationCacheTable> GetFirstTable() { |
| 71 | return GetTable(kFirstGeneration); |
| 72 | } |
| 73 | void SetFirstTable(Handle<CompilationCacheTable> value) { |
| 74 | ASSERT(kFirstGeneration < generations_); |
| 75 | tables_[kFirstGeneration] = *value; |
| 76 | } |
| 77 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | // Age the sub-cache by evicting the oldest generation and creating a new |
| 79 | // young generation. |
| 80 | void Age(); |
| 81 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 82 | bool HasFunction(SharedFunctionInfo* function_info); |
| 83 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 84 | // GC support. |
| 85 | void Iterate(ObjectVisitor* v); |
| 86 | |
| 87 | // Clear this sub-cache evicting all its content. |
| 88 | void Clear(); |
| 89 | |
| 90 | // Number of generations in this sub-cache. |
| 91 | inline int generations() { return generations_; } |
| 92 | |
| 93 | private: |
| 94 | int generations_; // Number of generations. |
| 95 | Object** tables_; // Compilation cache tables - one for each generation. |
| 96 | |
| 97 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache); |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | // Sub-cache for scripts. |
| 102 | class CompilationCacheScript : public CompilationSubCache { |
| 103 | public: |
| 104 | explicit CompilationCacheScript(int generations) |
| 105 | : CompilationSubCache(generations) { } |
| 106 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 107 | Handle<SharedFunctionInfo> Lookup(Handle<String> source, |
| 108 | Handle<Object> name, |
| 109 | int line_offset, |
| 110 | int column_offset); |
| 111 | void Put(Handle<String> source, Handle<SharedFunctionInfo> function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | |
| 113 | private: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 114 | // Note: Returns a new hash table if operation results in expansion. |
| 115 | Handle<CompilationCacheTable> TablePut( |
| 116 | Handle<String> source, Handle<SharedFunctionInfo> function_info); |
| 117 | |
| 118 | bool HasOrigin(Handle<SharedFunctionInfo> function_info, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 119 | Handle<Object> name, |
| 120 | int line_offset, |
| 121 | int column_offset); |
| 122 | |
| 123 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript); |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | // Sub-cache for eval scripts. |
| 128 | class CompilationCacheEval: public CompilationSubCache { |
| 129 | public: |
| 130 | explicit CompilationCacheEval(int generations) |
| 131 | : CompilationSubCache(generations) { } |
| 132 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 133 | Handle<SharedFunctionInfo> Lookup(Handle<String> source, |
| 134 | Handle<Context> context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 135 | |
| 136 | void Put(Handle<String> source, |
| 137 | Handle<Context> context, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 138 | Handle<SharedFunctionInfo> function_info); |
| 139 | |
| 140 | private: |
| 141 | // Note: Returns a new hash table if operation results in expansion. |
| 142 | Handle<CompilationCacheTable> TablePut( |
| 143 | Handle<String> source, |
| 144 | Handle<Context> context, |
| 145 | Handle<SharedFunctionInfo> function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | |
| 147 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval); |
| 148 | }; |
| 149 | |
| 150 | |
| 151 | // Sub-cache for regular expressions. |
| 152 | class CompilationCacheRegExp: public CompilationSubCache { |
| 153 | public: |
| 154 | explicit CompilationCacheRegExp(int generations) |
| 155 | : CompilationSubCache(generations) { } |
| 156 | |
| 157 | Handle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags); |
| 158 | |
| 159 | void Put(Handle<String> source, |
| 160 | JSRegExp::Flags flags, |
| 161 | Handle<FixedArray> data); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 162 | private: |
| 163 | // Note: Returns a new hash table if operation results in expansion. |
| 164 | Handle<CompilationCacheTable> TablePut(Handle<String> source, |
| 165 | JSRegExp::Flags flags, |
| 166 | Handle<FixedArray> data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | |
| 168 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp); |
| 169 | }; |
| 170 | |
| 171 | |
| 172 | // Statically allocate all the sub-caches. |
| 173 | static CompilationCacheScript script(kScriptGenerations); |
| 174 | static CompilationCacheEval eval_global(kEvalGlobalGenerations); |
| 175 | static CompilationCacheEval eval_contextual(kEvalContextualGenerations); |
| 176 | static CompilationCacheRegExp reg_exp(kRegExpGenerations); |
| 177 | static CompilationSubCache* subcaches[kSubCacheCount] = |
| 178 | {&script, &eval_global, &eval_contextual, ®_exp}; |
| 179 | |
| 180 | |
| 181 | // Current enable state of the compilation cache. |
| 182 | static bool enabled = true; |
| 183 | static inline bool IsEnabled() { |
| 184 | return FLAG_compilation_cache && enabled; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | static Handle<CompilationCacheTable> AllocateTable(int size) { |
| 189 | CALL_HEAP_FUNCTION(CompilationCacheTable::Allocate(size), |
| 190 | CompilationCacheTable); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) { |
| 195 | ASSERT(generation < generations_); |
| 196 | Handle<CompilationCacheTable> result; |
| 197 | if (tables_[generation]->IsUndefined()) { |
| 198 | result = AllocateTable(kInitialCacheSize); |
| 199 | tables_[generation] = *result; |
| 200 | } else { |
| 201 | CompilationCacheTable* table = |
| 202 | CompilationCacheTable::cast(tables_[generation]); |
| 203 | result = Handle<CompilationCacheTable>(table); |
| 204 | } |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 209 | bool CompilationSubCache::HasFunction(SharedFunctionInfo* function_info) { |
| 210 | if (function_info->script()->IsUndefined() || |
| 211 | Script::cast(function_info->script())->source()->IsUndefined()) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | String* source = |
| 216 | String::cast(Script::cast(function_info->script())->source()); |
| 217 | // Check all generations. |
| 218 | for (int generation = 0; generation < generations(); generation++) { |
| 219 | if (tables_[generation]->IsUndefined()) continue; |
| 220 | |
| 221 | CompilationCacheTable* table = |
| 222 | CompilationCacheTable::cast(tables_[generation]); |
| 223 | Object* object = table->Lookup(source); |
| 224 | if (object->IsSharedFunctionInfo()) return true; |
| 225 | } |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 230 | void CompilationSubCache::Age() { |
| 231 | // Age the generations implicitly killing off the oldest. |
| 232 | for (int i = generations_ - 1; i > 0; i--) { |
| 233 | tables_[i] = tables_[i - 1]; |
| 234 | } |
| 235 | |
| 236 | // Set the first generation as unborn. |
| 237 | tables_[0] = Heap::undefined_value(); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | void CompilationSubCache::Iterate(ObjectVisitor* v) { |
| 242 | v->VisitPointers(&tables_[0], &tables_[generations_]); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | void CompilationSubCache::Clear() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 247 | MemsetPointer(tables_, Heap::undefined_value(), generations_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | |
| 251 | // We only re-use a cached function for some script source code if the |
| 252 | // script originates from the same place. This is to avoid issues |
| 253 | // when reporting errors, etc. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 254 | bool CompilationCacheScript::HasOrigin( |
| 255 | Handle<SharedFunctionInfo> function_info, |
| 256 | Handle<Object> name, |
| 257 | int line_offset, |
| 258 | int column_offset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 259 | Handle<Script> script = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 260 | Handle<Script>(Script::cast(function_info->script())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 261 | // If the script name isn't set, the boilerplate script should have |
| 262 | // an undefined name to have the same origin. |
| 263 | if (name.is_null()) { |
| 264 | return script->name()->IsUndefined(); |
| 265 | } |
| 266 | // Do the fast bailout checks first. |
| 267 | if (line_offset != script->line_offset()->value()) return false; |
| 268 | if (column_offset != script->column_offset()->value()) return false; |
| 269 | // Check that both names are strings. If not, no match. |
| 270 | if (!name->IsString() || !script->name()->IsString()) return false; |
| 271 | // Compare the two name strings for equality. |
| 272 | return String::cast(*name)->Equals(String::cast(script->name())); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | // TODO(245): Need to allow identical code from different contexts to |
| 277 | // be cached in the same script generation. Currently the first use |
| 278 | // will be cached, but subsequent code from different source / line |
| 279 | // won't. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 280 | Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(Handle<String> source, |
| 281 | Handle<Object> name, |
| 282 | int line_offset, |
| 283 | int column_offset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 284 | Object* result = NULL; |
| 285 | int generation; |
| 286 | |
| 287 | // Probe the script generation tables. Make sure not to leak handles |
| 288 | // into the caller's handle scope. |
| 289 | { HandleScope scope; |
| 290 | for (generation = 0; generation < generations(); generation++) { |
| 291 | Handle<CompilationCacheTable> table = GetTable(generation); |
| 292 | Handle<Object> probe(table->Lookup(*source)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 293 | if (probe->IsSharedFunctionInfo()) { |
| 294 | Handle<SharedFunctionInfo> function_info = |
| 295 | Handle<SharedFunctionInfo>::cast(probe); |
| 296 | // Break when we've found a suitable shared function info that |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 297 | // matches the origin. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 298 | if (HasOrigin(function_info, name, line_offset, column_offset)) { |
| 299 | result = *function_info; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | break; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static void* script_histogram = StatsTable::CreateHistogram( |
| 307 | "V8.ScriptCache", |
| 308 | 0, |
| 309 | kScriptGenerations, |
| 310 | kScriptGenerations + 1); |
| 311 | |
| 312 | if (script_histogram != NULL) { |
| 313 | // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss. |
| 314 | StatsTable::AddHistogramSample(script_histogram, generation); |
| 315 | } |
| 316 | |
| 317 | // Once outside the manacles of the handle scope, we need to recheck |
| 318 | // to see if we actually found a cached script. If so, we return a |
| 319 | // handle created in the caller's handle scope. |
| 320 | if (result != NULL) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 321 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result)); |
| 322 | ASSERT(HasOrigin(shared, name, line_offset, column_offset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | // If the script was found in a later generation, we promote it to |
| 324 | // the first generation to let it survive longer in the cache. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 325 | if (generation != 0) Put(source, shared); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 326 | Counters::compilation_cache_hits.Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 327 | return shared; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 328 | } else { |
| 329 | Counters::compilation_cache_misses.Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 330 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 335 | Handle<CompilationCacheTable> CompilationCacheScript::TablePut( |
| 336 | Handle<String> source, |
| 337 | Handle<SharedFunctionInfo> function_info) { |
| 338 | CALL_HEAP_FUNCTION(GetFirstTable()->Put(*source, *function_info), |
| 339 | CompilationCacheTable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 343 | void CompilationCacheScript::Put(Handle<String> source, |
| 344 | Handle<SharedFunctionInfo> function_info) { |
| 345 | HandleScope scope; |
| 346 | SetFirstTable(TablePut(source, function_info)); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | Handle<SharedFunctionInfo> CompilationCacheEval::Lookup( |
| 351 | Handle<String> source, Handle<Context> context) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 352 | // Make sure not to leak the table into the surrounding handle |
| 353 | // scope. Otherwise, we risk keeping old tables around even after |
| 354 | // having cleared the cache. |
| 355 | Object* result = NULL; |
| 356 | int generation; |
| 357 | { HandleScope scope; |
| 358 | for (generation = 0; generation < generations(); generation++) { |
| 359 | Handle<CompilationCacheTable> table = GetTable(generation); |
| 360 | result = table->LookupEval(*source, *context); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 361 | if (result->IsSharedFunctionInfo()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | } |
| 365 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 366 | if (result->IsSharedFunctionInfo()) { |
| 367 | Handle<SharedFunctionInfo> |
| 368 | function_info(SharedFunctionInfo::cast(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 369 | if (generation != 0) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 370 | Put(source, context, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 371 | } |
| 372 | Counters::compilation_cache_hits.Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 373 | return function_info; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 374 | } else { |
| 375 | Counters::compilation_cache_misses.Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 376 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 381 | Handle<CompilationCacheTable> CompilationCacheEval::TablePut( |
| 382 | Handle<String> source, |
| 383 | Handle<Context> context, |
| 384 | Handle<SharedFunctionInfo> function_info) { |
| 385 | CALL_HEAP_FUNCTION(GetFirstTable()->PutEval(*source, |
| 386 | *context, |
| 387 | *function_info), |
| 388 | CompilationCacheTable); |
| 389 | } |
| 390 | |
| 391 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 392 | void CompilationCacheEval::Put(Handle<String> source, |
| 393 | Handle<Context> context, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 394 | Handle<SharedFunctionInfo> function_info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 395 | HandleScope scope; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 396 | SetFirstTable(TablePut(source, context, function_info)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | |
| 400 | Handle<FixedArray> CompilationCacheRegExp::Lookup(Handle<String> source, |
| 401 | JSRegExp::Flags flags) { |
| 402 | // Make sure not to leak the table into the surrounding handle |
| 403 | // scope. Otherwise, we risk keeping old tables around even after |
| 404 | // having cleared the cache. |
| 405 | Object* result = NULL; |
| 406 | int generation; |
| 407 | { HandleScope scope; |
| 408 | for (generation = 0; generation < generations(); generation++) { |
| 409 | Handle<CompilationCacheTable> table = GetTable(generation); |
| 410 | result = table->LookupRegExp(*source, flags); |
| 411 | if (result->IsFixedArray()) { |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | if (result->IsFixedArray()) { |
| 417 | Handle<FixedArray> data(FixedArray::cast(result)); |
| 418 | if (generation != 0) { |
| 419 | Put(source, flags, data); |
| 420 | } |
| 421 | Counters::compilation_cache_hits.Increment(); |
| 422 | return data; |
| 423 | } else { |
| 424 | Counters::compilation_cache_misses.Increment(); |
| 425 | return Handle<FixedArray>::null(); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 430 | Handle<CompilationCacheTable> CompilationCacheRegExp::TablePut( |
| 431 | Handle<String> source, |
| 432 | JSRegExp::Flags flags, |
| 433 | Handle<FixedArray> data) { |
| 434 | CALL_HEAP_FUNCTION(GetFirstTable()->PutRegExp(*source, flags, *data), |
| 435 | CompilationCacheTable); |
| 436 | } |
| 437 | |
| 438 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 439 | void CompilationCacheRegExp::Put(Handle<String> source, |
| 440 | JSRegExp::Flags flags, |
| 441 | Handle<FixedArray> data) { |
| 442 | HandleScope scope; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 443 | SetFirstTable(TablePut(source, flags, data)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 447 | Handle<SharedFunctionInfo> CompilationCache::LookupScript(Handle<String> source, |
| 448 | Handle<Object> name, |
| 449 | int line_offset, |
| 450 | int column_offset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 451 | if (!IsEnabled()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 452 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | return script.Lookup(source, name, line_offset, column_offset); |
| 456 | } |
| 457 | |
| 458 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 459 | Handle<SharedFunctionInfo> CompilationCache::LookupEval(Handle<String> source, |
| 460 | Handle<Context> context, |
| 461 | bool is_global) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 462 | if (!IsEnabled()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 463 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 466 | Handle<SharedFunctionInfo> result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 467 | if (is_global) { |
| 468 | result = eval_global.Lookup(source, context); |
| 469 | } else { |
| 470 | result = eval_contextual.Lookup(source, context); |
| 471 | } |
| 472 | return result; |
| 473 | } |
| 474 | |
| 475 | |
| 476 | Handle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source, |
| 477 | JSRegExp::Flags flags) { |
| 478 | if (!IsEnabled()) { |
| 479 | return Handle<FixedArray>::null(); |
| 480 | } |
| 481 | |
| 482 | return reg_exp.Lookup(source, flags); |
| 483 | } |
| 484 | |
| 485 | |
| 486 | void CompilationCache::PutScript(Handle<String> source, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 487 | Handle<SharedFunctionInfo> function_info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 488 | if (!IsEnabled()) { |
| 489 | return; |
| 490 | } |
| 491 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 492 | script.Put(source, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | |
| 496 | void CompilationCache::PutEval(Handle<String> source, |
| 497 | Handle<Context> context, |
| 498 | bool is_global, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 499 | Handle<SharedFunctionInfo> function_info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 500 | if (!IsEnabled()) { |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | HandleScope scope; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 505 | if (is_global) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 506 | eval_global.Put(source, context, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 507 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 508 | eval_contextual.Put(source, context, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | |
| 513 | |
| 514 | void CompilationCache::PutRegExp(Handle<String> source, |
| 515 | JSRegExp::Flags flags, |
| 516 | Handle<FixedArray> data) { |
| 517 | if (!IsEnabled()) { |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | reg_exp.Put(source, flags, data); |
| 522 | } |
| 523 | |
| 524 | |
| 525 | void CompilationCache::Clear() { |
| 526 | for (int i = 0; i < kSubCacheCount; i++) { |
| 527 | subcaches[i]->Clear(); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 532 | bool CompilationCache::HasFunction(SharedFunctionInfo* function_info) { |
| 533 | return script.HasFunction(function_info); |
| 534 | } |
| 535 | |
| 536 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 537 | void CompilationCache::Iterate(ObjectVisitor* v) { |
| 538 | for (int i = 0; i < kSubCacheCount; i++) { |
| 539 | subcaches[i]->Iterate(v); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | |
| 544 | void CompilationCache::MarkCompactPrologue() { |
| 545 | for (int i = 0; i < kSubCacheCount; i++) { |
| 546 | subcaches[i]->Age(); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | |
| 551 | void CompilationCache::Enable() { |
| 552 | enabled = true; |
| 553 | } |
| 554 | |
| 555 | |
| 556 | void CompilationCache::Disable() { |
| 557 | enabled = false; |
| 558 | Clear(); |
| 559 | } |
| 560 | |
| 561 | |
| 562 | } } // namespace v8::internal |