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