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 | |
| 37 | // The number of generations for each sub cache. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 38 | // The number of ScriptGenerations is carefully chosen based on histograms. |
| 39 | // 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] | 40 | static const int kScriptGenerations = 5; |
| 41 | static const int kEvalGlobalGenerations = 2; |
| 42 | static const int kEvalContextualGenerations = 2; |
| 43 | static const int kRegExpGenerations = 2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 45 | // Initial size of each compilation cache table allocated. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | static const int kInitialCacheSize = 64; |
| 47 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 48 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 49 | CompilationCache::CompilationCache(Isolate* isolate) |
| 50 | : isolate_(isolate), |
| 51 | script_(isolate, kScriptGenerations), |
| 52 | eval_global_(isolate, kEvalGlobalGenerations), |
| 53 | eval_contextual_(isolate, kEvalContextualGenerations), |
| 54 | reg_exp_(isolate, kRegExpGenerations), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 55 | enabled_(true) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 56 | CompilationSubCache* subcaches[kSubCacheCount] = |
| 57 | {&script_, &eval_global_, &eval_contextual_, ®_exp_}; |
| 58 | for (int i = 0; i < kSubCacheCount; ++i) { |
| 59 | subcaches_[i] = subcaches[i]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 64 | CompilationCache::~CompilationCache() {} |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 65 | |
| 66 | |
| 67 | static Handle<CompilationCacheTable> AllocateTable(Isolate* isolate, int size) { |
| 68 | CALL_HEAP_FUNCTION(isolate, |
| 69 | CompilationCacheTable::Allocate(size), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | CompilationCacheTable); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) { |
| 75 | ASSERT(generation < generations_); |
| 76 | Handle<CompilationCacheTable> result; |
| 77 | if (tables_[generation]->IsUndefined()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 78 | result = AllocateTable(isolate(), kInitialCacheSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | tables_[generation] = *result; |
| 80 | } else { |
| 81 | CompilationCacheTable* table = |
| 82 | CompilationCacheTable::cast(tables_[generation]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 83 | result = Handle<CompilationCacheTable>(table, isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 84 | } |
| 85 | return result; |
| 86 | } |
| 87 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | void CompilationSubCache::Age() { |
| 89 | // Age the generations implicitly killing off the oldest. |
| 90 | for (int i = generations_ - 1; i > 0; i--) { |
| 91 | tables_[i] = tables_[i - 1]; |
| 92 | } |
| 93 | |
| 94 | // Set the first generation as unborn. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 95 | tables_[0] = isolate()->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 99 | void CompilationSubCache::IterateFunctions(ObjectVisitor* v) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 100 | Object* undefined = isolate()->heap()->raw_unchecked_undefined_value(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 101 | for (int i = 0; i < generations_; i++) { |
| 102 | if (tables_[i] != undefined) { |
| 103 | reinterpret_cast<CompilationCacheTable*>(tables_[i])->IterateElements(v); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 109 | void CompilationSubCache::Iterate(ObjectVisitor* v) { |
| 110 | v->VisitPointers(&tables_[0], &tables_[generations_]); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | void CompilationSubCache::Clear() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 115 | MemsetPointer(tables_, isolate()->heap()->undefined_value(), generations_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 119 | void CompilationSubCache::Remove(Handle<SharedFunctionInfo> function_info) { |
| 120 | // Probe the script generation tables. Make sure not to leak handles |
| 121 | // into the caller's handle scope. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 122 | { HandleScope scope(isolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 123 | for (int generation = 0; generation < generations(); generation++) { |
| 124 | Handle<CompilationCacheTable> table = GetTable(generation); |
| 125 | table->Remove(*function_info); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 131 | CompilationCacheScript::CompilationCacheScript(Isolate* isolate, |
| 132 | int generations) |
| 133 | : CompilationSubCache(isolate, generations), |
| 134 | script_histogram_(NULL), |
| 135 | script_histogram_initialized_(false) { } |
| 136 | |
| 137 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 138 | // We only re-use a cached function for some script source code if the |
| 139 | // script originates from the same place. This is to avoid issues |
| 140 | // when reporting errors, etc. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 141 | bool CompilationCacheScript::HasOrigin( |
| 142 | Handle<SharedFunctionInfo> function_info, |
| 143 | Handle<Object> name, |
| 144 | int line_offset, |
| 145 | int column_offset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | Handle<Script> script = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 147 | Handle<Script>(Script::cast(function_info->script()), isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | // If the script name isn't set, the boilerplate script should have |
| 149 | // an undefined name to have the same origin. |
| 150 | if (name.is_null()) { |
| 151 | return script->name()->IsUndefined(); |
| 152 | } |
| 153 | // Do the fast bailout checks first. |
| 154 | if (line_offset != script->line_offset()->value()) return false; |
| 155 | if (column_offset != script->column_offset()->value()) return false; |
| 156 | // Check that both names are strings. If not, no match. |
| 157 | if (!name->IsString() || !script->name()->IsString()) return false; |
| 158 | // Compare the two name strings for equality. |
| 159 | return String::cast(*name)->Equals(String::cast(script->name())); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | // TODO(245): Need to allow identical code from different contexts to |
| 164 | // be cached in the same script generation. Currently the first use |
| 165 | // will be cached, but subsequent code from different source / line |
| 166 | // won't. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 167 | Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(Handle<String> source, |
| 168 | Handle<Object> name, |
| 169 | int line_offset, |
| 170 | int column_offset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | Object* result = NULL; |
| 172 | int generation; |
| 173 | |
| 174 | // Probe the script generation tables. Make sure not to leak handles |
| 175 | // into the caller's handle scope. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 176 | { HandleScope scope(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | for (generation = 0; generation < generations(); generation++) { |
| 178 | Handle<CompilationCacheTable> table = GetTable(generation); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 179 | Handle<Object> probe(table->Lookup(*source), isolate()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 180 | if (probe->IsSharedFunctionInfo()) { |
| 181 | Handle<SharedFunctionInfo> function_info = |
| 182 | Handle<SharedFunctionInfo>::cast(probe); |
| 183 | // Break when we've found a suitable shared function info that |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 184 | // matches the origin. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 185 | if (HasOrigin(function_info, name, line_offset, column_offset)) { |
| 186 | result = *function_info; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | break; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 193 | if (!script_histogram_initialized_) { |
| 194 | script_histogram_ = isolate()->stats_table()->CreateHistogram( |
| 195 | "V8.ScriptCache", |
| 196 | 0, |
| 197 | kScriptGenerations, |
| 198 | kScriptGenerations + 1); |
| 199 | script_histogram_initialized_ = true; |
| 200 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 201 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 202 | if (script_histogram_ != NULL) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 204 | isolate()->stats_table()->AddHistogramSample(script_histogram_, generation); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Once outside the manacles of the handle scope, we need to recheck |
| 208 | // to see if we actually found a cached script. If so, we return a |
| 209 | // handle created in the caller's handle scope. |
| 210 | if (result != NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 211 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result), |
| 212 | isolate()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 213 | ASSERT(HasOrigin(shared, name, line_offset, column_offset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 214 | // If the script was found in a later generation, we promote it to |
| 215 | // the first generation to let it survive longer in the cache. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 216 | if (generation != 0) Put(source, shared); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 217 | isolate()->counters()->compilation_cache_hits()->Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 218 | return shared; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 219 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 220 | isolate()->counters()->compilation_cache_misses()->Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 221 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 226 | MaybeObject* CompilationCacheScript::TryTablePut( |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 227 | Handle<String> source, |
| 228 | Handle<SharedFunctionInfo> function_info) { |
| 229 | Handle<CompilationCacheTable> table = GetFirstTable(); |
| 230 | return table->Put(*source, *function_info); |
| 231 | } |
| 232 | |
| 233 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 234 | Handle<CompilationCacheTable> CompilationCacheScript::TablePut( |
| 235 | Handle<String> source, |
| 236 | Handle<SharedFunctionInfo> function_info) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 237 | CALL_HEAP_FUNCTION(isolate(), |
| 238 | TryTablePut(source, function_info), |
| 239 | CompilationCacheTable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 243 | void CompilationCacheScript::Put(Handle<String> source, |
| 244 | Handle<SharedFunctionInfo> function_info) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 245 | HandleScope scope(isolate()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 246 | SetFirstTable(TablePut(source, function_info)); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | Handle<SharedFunctionInfo> CompilationCacheEval::Lookup( |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 251 | Handle<String> source, |
| 252 | Handle<Context> context, |
| 253 | StrictModeFlag strict_mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 254 | // Make sure not to leak the table into the surrounding handle |
| 255 | // scope. Otherwise, we risk keeping old tables around even after |
| 256 | // having cleared the cache. |
| 257 | Object* result = NULL; |
| 258 | int generation; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 259 | { HandleScope scope(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 260 | for (generation = 0; generation < generations(); generation++) { |
| 261 | Handle<CompilationCacheTable> table = GetTable(generation); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 262 | result = table->LookupEval(*source, *context, strict_mode); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 263 | if (result->IsSharedFunctionInfo()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 264 | break; |
| 265 | } |
| 266 | } |
| 267 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 268 | if (result->IsSharedFunctionInfo()) { |
| 269 | Handle<SharedFunctionInfo> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 270 | function_info(SharedFunctionInfo::cast(result), isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 271 | if (generation != 0) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 272 | Put(source, context, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 273 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 274 | isolate()->counters()->compilation_cache_hits()->Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 275 | return function_info; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 276 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 277 | isolate()->counters()->compilation_cache_misses()->Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 278 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 283 | MaybeObject* CompilationCacheEval::TryTablePut( |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 284 | Handle<String> source, |
| 285 | Handle<Context> context, |
| 286 | Handle<SharedFunctionInfo> function_info) { |
| 287 | Handle<CompilationCacheTable> table = GetFirstTable(); |
| 288 | return table->PutEval(*source, *context, *function_info); |
| 289 | } |
| 290 | |
| 291 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 292 | Handle<CompilationCacheTable> CompilationCacheEval::TablePut( |
| 293 | Handle<String> source, |
| 294 | Handle<Context> context, |
| 295 | Handle<SharedFunctionInfo> function_info) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 296 | CALL_HEAP_FUNCTION(isolate(), |
| 297 | TryTablePut(source, context, function_info), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 298 | CompilationCacheTable); |
| 299 | } |
| 300 | |
| 301 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 302 | void CompilationCacheEval::Put(Handle<String> source, |
| 303 | Handle<Context> context, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 304 | Handle<SharedFunctionInfo> function_info) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 305 | HandleScope scope(isolate()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 306 | SetFirstTable(TablePut(source, context, function_info)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
| 310 | Handle<FixedArray> CompilationCacheRegExp::Lookup(Handle<String> source, |
| 311 | JSRegExp::Flags flags) { |
| 312 | // Make sure not to leak the table into the surrounding handle |
| 313 | // scope. Otherwise, we risk keeping old tables around even after |
| 314 | // having cleared the cache. |
| 315 | Object* result = NULL; |
| 316 | int generation; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 317 | { HandleScope scope(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 318 | for (generation = 0; generation < generations(); generation++) { |
| 319 | Handle<CompilationCacheTable> table = GetTable(generation); |
| 320 | result = table->LookupRegExp(*source, flags); |
| 321 | if (result->IsFixedArray()) { |
| 322 | break; |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | if (result->IsFixedArray()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 327 | Handle<FixedArray> data(FixedArray::cast(result), isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 328 | if (generation != 0) { |
| 329 | Put(source, flags, data); |
| 330 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 331 | isolate()->counters()->compilation_cache_hits()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 332 | return data; |
| 333 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 334 | isolate()->counters()->compilation_cache_misses()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 335 | return Handle<FixedArray>::null(); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 340 | MaybeObject* CompilationCacheRegExp::TryTablePut( |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 341 | Handle<String> source, |
| 342 | JSRegExp::Flags flags, |
| 343 | Handle<FixedArray> data) { |
| 344 | Handle<CompilationCacheTable> table = GetFirstTable(); |
| 345 | return table->PutRegExp(*source, flags, *data); |
| 346 | } |
| 347 | |
| 348 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 349 | Handle<CompilationCacheTable> CompilationCacheRegExp::TablePut( |
| 350 | Handle<String> source, |
| 351 | JSRegExp::Flags flags, |
| 352 | Handle<FixedArray> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 353 | CALL_HEAP_FUNCTION(isolate(), |
| 354 | TryTablePut(source, flags, data), |
| 355 | CompilationCacheTable); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 359 | void CompilationCacheRegExp::Put(Handle<String> source, |
| 360 | JSRegExp::Flags flags, |
| 361 | Handle<FixedArray> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 362 | HandleScope scope(isolate()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 363 | SetFirstTable(TablePut(source, flags, data)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 367 | void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) { |
| 368 | if (!IsEnabled()) return; |
| 369 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 370 | eval_global_.Remove(function_info); |
| 371 | eval_contextual_.Remove(function_info); |
| 372 | script_.Remove(function_info); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 376 | Handle<SharedFunctionInfo> CompilationCache::LookupScript(Handle<String> source, |
| 377 | Handle<Object> name, |
| 378 | int line_offset, |
| 379 | int column_offset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 380 | if (!IsEnabled()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 381 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 384 | return script_.Lookup(source, name, line_offset, column_offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 388 | Handle<SharedFunctionInfo> CompilationCache::LookupEval( |
| 389 | Handle<String> source, |
| 390 | Handle<Context> context, |
| 391 | bool is_global, |
| 392 | StrictModeFlag strict_mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 393 | if (!IsEnabled()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 394 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 397 | Handle<SharedFunctionInfo> result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 398 | if (is_global) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 399 | result = eval_global_.Lookup(source, context, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 400 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 401 | result = eval_contextual_.Lookup(source, context, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 402 | } |
| 403 | return result; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | Handle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source, |
| 408 | JSRegExp::Flags flags) { |
| 409 | if (!IsEnabled()) { |
| 410 | return Handle<FixedArray>::null(); |
| 411 | } |
| 412 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 413 | return reg_exp_.Lookup(source, flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | |
| 417 | void CompilationCache::PutScript(Handle<String> source, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 418 | Handle<SharedFunctionInfo> function_info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 419 | if (!IsEnabled()) { |
| 420 | return; |
| 421 | } |
| 422 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 423 | script_.Put(source, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
| 427 | void CompilationCache::PutEval(Handle<String> source, |
| 428 | Handle<Context> context, |
| 429 | bool is_global, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 430 | Handle<SharedFunctionInfo> function_info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 431 | if (!IsEnabled()) { |
| 432 | return; |
| 433 | } |
| 434 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 435 | HandleScope scope(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 436 | if (is_global) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 437 | eval_global_.Put(source, context, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 438 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 439 | eval_contextual_.Put(source, context, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | |
| 444 | |
| 445 | void CompilationCache::PutRegExp(Handle<String> source, |
| 446 | JSRegExp::Flags flags, |
| 447 | Handle<FixedArray> data) { |
| 448 | if (!IsEnabled()) { |
| 449 | return; |
| 450 | } |
| 451 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 452 | reg_exp_.Put(source, flags, data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
| 456 | void CompilationCache::Clear() { |
| 457 | for (int i = 0; i < kSubCacheCount; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 458 | subcaches_[i]->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 462 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 463 | void CompilationCache::Iterate(ObjectVisitor* v) { |
| 464 | for (int i = 0; i < kSubCacheCount; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 465 | subcaches_[i]->Iterate(v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 470 | void CompilationCache::IterateFunctions(ObjectVisitor* v) { |
| 471 | for (int i = 0; i < kSubCacheCount; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 472 | subcaches_[i]->IterateFunctions(v); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 477 | void CompilationCache::MarkCompactPrologue() { |
| 478 | for (int i = 0; i < kSubCacheCount; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 479 | subcaches_[i]->Age(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
| 483 | |
| 484 | void CompilationCache::Enable() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 485 | enabled_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
| 489 | void CompilationCache::Disable() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 490 | enabled_ = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 491 | Clear(); |
| 492 | } |
| 493 | |
| 494 | |
| 495 | } } // namespace v8::internal |