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