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 | |
| 82 | // GC support. |
| 83 | void Iterate(ObjectVisitor* v); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 84 | void IterateFunctions(ObjectVisitor* v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | |
| 86 | // Clear this sub-cache evicting all its content. |
| 87 | void Clear(); |
| 88 | |
| 89 | // Number of generations in this sub-cache. |
| 90 | inline int generations() { return generations_; } |
| 91 | |
| 92 | private: |
| 93 | int generations_; // Number of generations. |
| 94 | Object** tables_; // Compilation cache tables - one for each generation. |
| 95 | |
| 96 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache); |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | // Sub-cache for scripts. |
| 101 | class CompilationCacheScript : public CompilationSubCache { |
| 102 | public: |
| 103 | explicit CompilationCacheScript(int generations) |
| 104 | : CompilationSubCache(generations) { } |
| 105 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 106 | Handle<SharedFunctionInfo> Lookup(Handle<String> source, |
| 107 | Handle<Object> name, |
| 108 | int line_offset, |
| 109 | int column_offset); |
| 110 | void Put(Handle<String> source, Handle<SharedFunctionInfo> function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 111 | |
| 112 | private: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 113 | // Note: Returns a new hash table if operation results in expansion. |
| 114 | Handle<CompilationCacheTable> TablePut( |
| 115 | Handle<String> source, Handle<SharedFunctionInfo> function_info); |
| 116 | |
| 117 | bool HasOrigin(Handle<SharedFunctionInfo> function_info, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 118 | Handle<Object> name, |
| 119 | int line_offset, |
| 120 | int column_offset); |
| 121 | |
| 122 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript); |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | // Sub-cache for eval scripts. |
| 127 | class CompilationCacheEval: public CompilationSubCache { |
| 128 | public: |
| 129 | explicit CompilationCacheEval(int generations) |
| 130 | : CompilationSubCache(generations) { } |
| 131 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 132 | Handle<SharedFunctionInfo> Lookup(Handle<String> source, |
| 133 | Handle<Context> context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 134 | |
| 135 | void Put(Handle<String> source, |
| 136 | Handle<Context> context, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 137 | Handle<SharedFunctionInfo> function_info); |
| 138 | |
| 139 | private: |
| 140 | // Note: Returns a new hash table if operation results in expansion. |
| 141 | Handle<CompilationCacheTable> TablePut( |
| 142 | Handle<String> source, |
| 143 | Handle<Context> context, |
| 144 | Handle<SharedFunctionInfo> function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | |
| 146 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval); |
| 147 | }; |
| 148 | |
| 149 | |
| 150 | // Sub-cache for regular expressions. |
| 151 | class CompilationCacheRegExp: public CompilationSubCache { |
| 152 | public: |
| 153 | explicit CompilationCacheRegExp(int generations) |
| 154 | : CompilationSubCache(generations) { } |
| 155 | |
| 156 | Handle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags); |
| 157 | |
| 158 | void Put(Handle<String> source, |
| 159 | JSRegExp::Flags flags, |
| 160 | Handle<FixedArray> data); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 161 | private: |
| 162 | // Note: Returns a new hash table if operation results in expansion. |
| 163 | Handle<CompilationCacheTable> TablePut(Handle<String> source, |
| 164 | JSRegExp::Flags flags, |
| 165 | Handle<FixedArray> data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 166 | |
| 167 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp); |
| 168 | }; |
| 169 | |
| 170 | |
| 171 | // Statically allocate all the sub-caches. |
| 172 | static CompilationCacheScript script(kScriptGenerations); |
| 173 | static CompilationCacheEval eval_global(kEvalGlobalGenerations); |
| 174 | static CompilationCacheEval eval_contextual(kEvalContextualGenerations); |
| 175 | static CompilationCacheRegExp reg_exp(kRegExpGenerations); |
| 176 | static CompilationSubCache* subcaches[kSubCacheCount] = |
| 177 | {&script, &eval_global, &eval_contextual, ®_exp}; |
| 178 | |
| 179 | |
| 180 | // Current enable state of the compilation cache. |
| 181 | static bool enabled = true; |
| 182 | static inline bool IsEnabled() { |
| 183 | return FLAG_compilation_cache && enabled; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static Handle<CompilationCacheTable> AllocateTable(int size) { |
| 188 | CALL_HEAP_FUNCTION(CompilationCacheTable::Allocate(size), |
| 189 | CompilationCacheTable); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) { |
| 194 | ASSERT(generation < generations_); |
| 195 | Handle<CompilationCacheTable> result; |
| 196 | if (tables_[generation]->IsUndefined()) { |
| 197 | result = AllocateTable(kInitialCacheSize); |
| 198 | tables_[generation] = *result; |
| 199 | } else { |
| 200 | CompilationCacheTable* table = |
| 201 | CompilationCacheTable::cast(tables_[generation]); |
| 202 | result = Handle<CompilationCacheTable>(table); |
| 203 | } |
| 204 | return result; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | void CompilationSubCache::Age() { |
| 209 | // Age the generations implicitly killing off the oldest. |
| 210 | for (int i = generations_ - 1; i > 0; i--) { |
| 211 | tables_[i] = tables_[i - 1]; |
| 212 | } |
| 213 | |
| 214 | // Set the first generation as unborn. |
| 215 | tables_[0] = Heap::undefined_value(); |
| 216 | } |
| 217 | |
| 218 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 219 | void CompilationSubCache::IterateFunctions(ObjectVisitor* v) { |
| 220 | Object* undefined = Heap::raw_unchecked_undefined_value(); |
| 221 | for (int i = 0; i < generations_; i++) { |
| 222 | if (tables_[i] != undefined) { |
| 223 | reinterpret_cast<CompilationCacheTable*>(tables_[i])->IterateElements(v); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 229 | void CompilationSubCache::Iterate(ObjectVisitor* v) { |
| 230 | v->VisitPointers(&tables_[0], &tables_[generations_]); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | void CompilationSubCache::Clear() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 235 | MemsetPointer(tables_, Heap::undefined_value(), generations_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
| 239 | // We only re-use a cached function for some script source code if the |
| 240 | // script originates from the same place. This is to avoid issues |
| 241 | // when reporting errors, etc. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 242 | bool CompilationCacheScript::HasOrigin( |
| 243 | Handle<SharedFunctionInfo> function_info, |
| 244 | Handle<Object> name, |
| 245 | int line_offset, |
| 246 | int column_offset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 247 | Handle<Script> script = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 248 | Handle<Script>(Script::cast(function_info->script())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 249 | // If the script name isn't set, the boilerplate script should have |
| 250 | // an undefined name to have the same origin. |
| 251 | if (name.is_null()) { |
| 252 | return script->name()->IsUndefined(); |
| 253 | } |
| 254 | // Do the fast bailout checks first. |
| 255 | if (line_offset != script->line_offset()->value()) return false; |
| 256 | if (column_offset != script->column_offset()->value()) return false; |
| 257 | // Check that both names are strings. If not, no match. |
| 258 | if (!name->IsString() || !script->name()->IsString()) return false; |
| 259 | // Compare the two name strings for equality. |
| 260 | return String::cast(*name)->Equals(String::cast(script->name())); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | // TODO(245): Need to allow identical code from different contexts to |
| 265 | // be cached in the same script generation. Currently the first use |
| 266 | // will be cached, but subsequent code from different source / line |
| 267 | // won't. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 268 | Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(Handle<String> source, |
| 269 | Handle<Object> name, |
| 270 | int line_offset, |
| 271 | int column_offset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 272 | Object* result = NULL; |
| 273 | int generation; |
| 274 | |
| 275 | // Probe the script generation tables. Make sure not to leak handles |
| 276 | // into the caller's handle scope. |
| 277 | { HandleScope scope; |
| 278 | for (generation = 0; generation < generations(); generation++) { |
| 279 | Handle<CompilationCacheTable> table = GetTable(generation); |
| 280 | Handle<Object> probe(table->Lookup(*source)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 281 | if (probe->IsSharedFunctionInfo()) { |
| 282 | Handle<SharedFunctionInfo> function_info = |
| 283 | Handle<SharedFunctionInfo>::cast(probe); |
| 284 | // Break when we've found a suitable shared function info that |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 285 | // matches the origin. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 286 | if (HasOrigin(function_info, name, line_offset, column_offset)) { |
| 287 | result = *function_info; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static void* script_histogram = StatsTable::CreateHistogram( |
| 295 | "V8.ScriptCache", |
| 296 | 0, |
| 297 | kScriptGenerations, |
| 298 | kScriptGenerations + 1); |
| 299 | |
| 300 | if (script_histogram != NULL) { |
| 301 | // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss. |
| 302 | StatsTable::AddHistogramSample(script_histogram, generation); |
| 303 | } |
| 304 | |
| 305 | // Once outside the manacles of the handle scope, we need to recheck |
| 306 | // to see if we actually found a cached script. If so, we return a |
| 307 | // handle created in the caller's handle scope. |
| 308 | if (result != NULL) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 309 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result)); |
| 310 | ASSERT(HasOrigin(shared, name, line_offset, column_offset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 311 | // If the script was found in a later generation, we promote it to |
| 312 | // the first generation to let it survive longer in the cache. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 313 | if (generation != 0) Put(source, shared); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 314 | Counters::compilation_cache_hits.Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 315 | return shared; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 316 | } else { |
| 317 | Counters::compilation_cache_misses.Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 318 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 323 | Handle<CompilationCacheTable> CompilationCacheScript::TablePut( |
| 324 | Handle<String> source, |
| 325 | Handle<SharedFunctionInfo> function_info) { |
| 326 | CALL_HEAP_FUNCTION(GetFirstTable()->Put(*source, *function_info), |
| 327 | CompilationCacheTable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 331 | void CompilationCacheScript::Put(Handle<String> source, |
| 332 | Handle<SharedFunctionInfo> function_info) { |
| 333 | HandleScope scope; |
| 334 | SetFirstTable(TablePut(source, function_info)); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | Handle<SharedFunctionInfo> CompilationCacheEval::Lookup( |
| 339 | Handle<String> source, Handle<Context> context) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 340 | // Make sure not to leak the table into the surrounding handle |
| 341 | // scope. Otherwise, we risk keeping old tables around even after |
| 342 | // having cleared the cache. |
| 343 | Object* result = NULL; |
| 344 | int generation; |
| 345 | { HandleScope scope; |
| 346 | for (generation = 0; generation < generations(); generation++) { |
| 347 | Handle<CompilationCacheTable> table = GetTable(generation); |
| 348 | result = table->LookupEval(*source, *context); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 349 | if (result->IsSharedFunctionInfo()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | } |
| 353 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 354 | if (result->IsSharedFunctionInfo()) { |
| 355 | Handle<SharedFunctionInfo> |
| 356 | function_info(SharedFunctionInfo::cast(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 357 | if (generation != 0) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 358 | Put(source, context, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 359 | } |
| 360 | Counters::compilation_cache_hits.Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 361 | return function_info; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 362 | } else { |
| 363 | Counters::compilation_cache_misses.Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 364 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
| 368 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 369 | Handle<CompilationCacheTable> CompilationCacheEval::TablePut( |
| 370 | Handle<String> source, |
| 371 | Handle<Context> context, |
| 372 | Handle<SharedFunctionInfo> function_info) { |
| 373 | CALL_HEAP_FUNCTION(GetFirstTable()->PutEval(*source, |
| 374 | *context, |
| 375 | *function_info), |
| 376 | CompilationCacheTable); |
| 377 | } |
| 378 | |
| 379 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 380 | void CompilationCacheEval::Put(Handle<String> source, |
| 381 | Handle<Context> context, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 382 | Handle<SharedFunctionInfo> function_info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 383 | HandleScope scope; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 384 | SetFirstTable(TablePut(source, context, function_info)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | |
| 388 | Handle<FixedArray> CompilationCacheRegExp::Lookup(Handle<String> source, |
| 389 | JSRegExp::Flags flags) { |
| 390 | // Make sure not to leak the table into the surrounding handle |
| 391 | // scope. Otherwise, we risk keeping old tables around even after |
| 392 | // having cleared the cache. |
| 393 | Object* result = NULL; |
| 394 | int generation; |
| 395 | { HandleScope scope; |
| 396 | for (generation = 0; generation < generations(); generation++) { |
| 397 | Handle<CompilationCacheTable> table = GetTable(generation); |
| 398 | result = table->LookupRegExp(*source, flags); |
| 399 | if (result->IsFixedArray()) { |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | if (result->IsFixedArray()) { |
| 405 | Handle<FixedArray> data(FixedArray::cast(result)); |
| 406 | if (generation != 0) { |
| 407 | Put(source, flags, data); |
| 408 | } |
| 409 | Counters::compilation_cache_hits.Increment(); |
| 410 | return data; |
| 411 | } else { |
| 412 | Counters::compilation_cache_misses.Increment(); |
| 413 | return Handle<FixedArray>::null(); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 418 | Handle<CompilationCacheTable> CompilationCacheRegExp::TablePut( |
| 419 | Handle<String> source, |
| 420 | JSRegExp::Flags flags, |
| 421 | Handle<FixedArray> data) { |
| 422 | CALL_HEAP_FUNCTION(GetFirstTable()->PutRegExp(*source, flags, *data), |
| 423 | CompilationCacheTable); |
| 424 | } |
| 425 | |
| 426 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 427 | void CompilationCacheRegExp::Put(Handle<String> source, |
| 428 | JSRegExp::Flags flags, |
| 429 | Handle<FixedArray> data) { |
| 430 | HandleScope scope; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 431 | SetFirstTable(TablePut(source, flags, data)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 435 | Handle<SharedFunctionInfo> CompilationCache::LookupScript(Handle<String> source, |
| 436 | Handle<Object> name, |
| 437 | int line_offset, |
| 438 | int column_offset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 439 | if (!IsEnabled()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 440 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | return script.Lookup(source, name, line_offset, column_offset); |
| 444 | } |
| 445 | |
| 446 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 447 | Handle<SharedFunctionInfo> CompilationCache::LookupEval(Handle<String> source, |
| 448 | Handle<Context> context, |
| 449 | bool is_global) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 450 | if (!IsEnabled()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 451 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 454 | Handle<SharedFunctionInfo> result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 455 | if (is_global) { |
| 456 | result = eval_global.Lookup(source, context); |
| 457 | } else { |
| 458 | result = eval_contextual.Lookup(source, context); |
| 459 | } |
| 460 | return result; |
| 461 | } |
| 462 | |
| 463 | |
| 464 | Handle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source, |
| 465 | JSRegExp::Flags flags) { |
| 466 | if (!IsEnabled()) { |
| 467 | return Handle<FixedArray>::null(); |
| 468 | } |
| 469 | |
| 470 | return reg_exp.Lookup(source, flags); |
| 471 | } |
| 472 | |
| 473 | |
| 474 | void CompilationCache::PutScript(Handle<String> source, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 475 | Handle<SharedFunctionInfo> function_info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 476 | if (!IsEnabled()) { |
| 477 | return; |
| 478 | } |
| 479 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 480 | script.Put(source, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | |
| 484 | void CompilationCache::PutEval(Handle<String> source, |
| 485 | Handle<Context> context, |
| 486 | bool is_global, |
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 | |
| 492 | HandleScope scope; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 493 | if (is_global) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 494 | eval_global.Put(source, context, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 495 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 496 | eval_contextual.Put(source, context, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | |
| 501 | |
| 502 | void CompilationCache::PutRegExp(Handle<String> source, |
| 503 | JSRegExp::Flags flags, |
| 504 | Handle<FixedArray> data) { |
| 505 | if (!IsEnabled()) { |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | reg_exp.Put(source, flags, data); |
| 510 | } |
| 511 | |
| 512 | |
| 513 | void CompilationCache::Clear() { |
| 514 | for (int i = 0; i < kSubCacheCount; i++) { |
| 515 | subcaches[i]->Clear(); |
| 516 | } |
| 517 | } |
| 518 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 519 | void CompilationCache::Iterate(ObjectVisitor* v) { |
| 520 | for (int i = 0; i < kSubCacheCount; i++) { |
| 521 | subcaches[i]->Iterate(v); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 526 | void CompilationCache::IterateFunctions(ObjectVisitor* v) { |
| 527 | for (int i = 0; i < kSubCacheCount; i++) { |
| 528 | subcaches[i]->IterateFunctions(v); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 533 | void CompilationCache::MarkCompactPrologue() { |
| 534 | for (int i = 0; i < kSubCacheCount; i++) { |
| 535 | subcaches[i]->Age(); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | |
| 540 | void CompilationCache::Enable() { |
| 541 | enabled = true; |
| 542 | } |
| 543 | |
| 544 | |
| 545 | void CompilationCache::Disable() { |
| 546 | enabled = false; |
| 547 | Clear(); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | } } // namespace v8::internal |