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