kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +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" |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 31 | #include "serialize.h" |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 32 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 35 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +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. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +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 |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +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; |
| 46 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 47 | // Initial size of each compilation cache table allocated. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 48 | static const int kInitialCacheSize = 64; |
| 49 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 50 | // Index for the first generation in the cache. |
| 51 | static const int kFirstGeneration = 0; |
| 52 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +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 |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +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. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 58 | class CompilationSubCache { |
| 59 | public: |
| 60 | explicit CompilationSubCache(int generations): generations_(generations) { |
| 61 | tables_ = NewArray<Object*>(generations); |
| 62 | } |
| 63 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 64 | ~CompilationSubCache() { DeleteArray(tables_); } |
| 65 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 66 | // Get the compilation cache tables for a specific generation. |
| 67 | Handle<CompilationCacheTable> GetTable(int generation); |
| 68 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [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 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +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); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 84 | void IterateFunctions(ObjectVisitor* v); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +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); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 100 | // Sub-cache for scripts. |
| 101 | class CompilationCacheScript : public CompilationSubCache { |
| 102 | public: |
| 103 | explicit CompilationCacheScript(int generations) |
| 104 | : CompilationSubCache(generations) { } |
| 105 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [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); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 111 | |
| 112 | private: |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 113 | MUST_USE_RESULT MaybeObject* TryTablePut( |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 114 | Handle<String> source, Handle<SharedFunctionInfo> function_info); |
| 115 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 116 | // Note: Returns a new hash table if operation results in expansion. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 117 | Handle<CompilationCacheTable> TablePut( |
| 118 | Handle<String> source, Handle<SharedFunctionInfo> function_info); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 119 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 120 | bool HasOrigin(Handle<SharedFunctionInfo> function_info, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +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 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 135 | Handle<SharedFunctionInfo> Lookup(Handle<String> source, |
| 136 | Handle<Context> context); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 137 | |
| 138 | void Put(Handle<String> source, |
| 139 | Handle<Context> context, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 140 | Handle<SharedFunctionInfo> function_info); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 141 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 142 | private: |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 143 | MUST_USE_RESULT MaybeObject* TryTablePut( |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 144 | Handle<String> source, |
| 145 | Handle<Context> context, |
| 146 | Handle<SharedFunctionInfo> function_info); |
| 147 | |
| 148 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 149 | // Note: Returns a new hash table if operation results in expansion. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 150 | Handle<CompilationCacheTable> TablePut( |
| 151 | Handle<String> source, |
| 152 | Handle<Context> context, |
| 153 | Handle<SharedFunctionInfo> function_info); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 154 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 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); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 170 | private: |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 171 | MUST_USE_RESULT MaybeObject* TryTablePut(Handle<String> source, |
| 172 | JSRegExp::Flags flags, |
| 173 | Handle<FixedArray> data); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 174 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [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); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 199 | |
| 200 | static Handle<CompilationCacheTable> AllocateTable(int size) { |
| 201 | CALL_HEAP_FUNCTION(CompilationCacheTable::Allocate(size), |
| 202 | CompilationCacheTable); |
| 203 | } |
| 204 | |
| 205 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 206 | Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) { |
| 207 | ASSERT(generation < generations_); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 208 | Handle<CompilationCacheTable> result; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 209 | if (tables_[generation]->IsUndefined()) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 210 | result = AllocateTable(kInitialCacheSize); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 211 | tables_[generation] = *result; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 212 | } else { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 213 | CompilationCacheTable* table = |
| 214 | CompilationCacheTable::cast(tables_[generation]); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 215 | result = Handle<CompilationCacheTable>(table); |
| 216 | } |
| 217 | return result; |
| 218 | } |
| 219 | |
| 220 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 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]; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 225 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 226 | |
| 227 | // Set the first generation as unborn. |
| 228 | tables_[0] = Heap::undefined_value(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [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 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 242 | void CompilationSubCache::Iterate(ObjectVisitor* v) { |
| 243 | v->VisitPointers(&tables_[0], &tables_[generations_]); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void CompilationSubCache::Clear() { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 248 | MemsetPointer(tables_, Heap::undefined_value(), generations_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 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. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 255 | bool CompilationCacheScript::HasOrigin( |
| 256 | Handle<SharedFunctionInfo> function_info, |
| 257 | Handle<Object> name, |
| 258 | int line_offset, |
| 259 | int column_offset) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 260 | Handle<Script> script = |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 261 | Handle<Script>(Script::cast(function_info->script())); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +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 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 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. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 281 | Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(Handle<String> source, |
| 282 | Handle<Object> name, |
| 283 | int line_offset, |
| 284 | int column_offset) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 285 | Object* result = NULL; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 286 | int generation; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 287 | |
| 288 | // Probe the script generation tables. Make sure not to leak handles |
| 289 | // into the caller's handle scope. |
| 290 | { HandleScope scope; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 291 | for (generation = 0; generation < generations(); generation++) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 292 | Handle<CompilationCacheTable> table = GetTable(generation); |
| 293 | Handle<Object> probe(table->Lookup(*source)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 294 | if (probe->IsSharedFunctionInfo()) { |
| 295 | Handle<SharedFunctionInfo> function_info = |
| 296 | Handle<SharedFunctionInfo>::cast(probe); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 297 | // Break when we've found a suitable shared function info that |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 298 | // matches the origin. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 299 | if (HasOrigin(function_info, name, line_offset, column_offset)) { |
| 300 | result = *function_info; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 304 | } |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 305 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 306 | |
kasperl@chromium.org | 31ca888 | 2009-05-26 09:36:02 +0000 | [diff] [blame] | 307 | static void* script_histogram = StatsTable::CreateHistogram( |
| 308 | "V8.ScriptCache", |
| 309 | 0, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 310 | kScriptGenerations, |
| 311 | kScriptGenerations + 1); |
kasperl@chromium.org | 31ca888 | 2009-05-26 09:36:02 +0000 | [diff] [blame] | 312 | |
| 313 | if (script_histogram != NULL) { |
| 314 | // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 315 | StatsTable::AddHistogramSample(script_histogram, generation); |
kasperl@chromium.org | 31ca888 | 2009-05-26 09:36:02 +0000 | [diff] [blame] | 316 | } |
| 317 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 318 | // 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] | 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) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 322 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result)); |
| 323 | ASSERT(HasOrigin(shared, name, line_offset, column_offset)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +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. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 326 | if (generation != 0) Put(source, shared); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 327 | Counters::compilation_cache_hits.Increment(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 328 | return shared; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 329 | } else { |
| 330 | Counters::compilation_cache_misses.Increment(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 331 | return Handle<SharedFunctionInfo>::null(); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 332 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 336 | MaybeObject* CompilationCacheScript::TryTablePut( |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 337 | Handle<String> source, |
| 338 | Handle<SharedFunctionInfo> function_info) { |
| 339 | Handle<CompilationCacheTable> table = GetFirstTable(); |
| 340 | return table->Put(*source, *function_info); |
| 341 | } |
| 342 | |
| 343 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 344 | Handle<CompilationCacheTable> CompilationCacheScript::TablePut( |
| 345 | Handle<String> source, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 346 | Handle<SharedFunctionInfo> function_info) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 347 | CALL_HEAP_FUNCTION(TryTablePut(source, function_info), CompilationCacheTable); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 351 | void CompilationCacheScript::Put(Handle<String> source, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 352 | Handle<SharedFunctionInfo> function_info) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 353 | HandleScope scope; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 354 | SetFirstTable(TablePut(source, function_info)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 358 | Handle<SharedFunctionInfo> CompilationCacheEval::Lookup( |
| 359 | Handle<String> source, Handle<Context> context) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +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); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 369 | if (result->IsSharedFunctionInfo()) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 370 | break; |
| 371 | } |
| 372 | } |
| 373 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 374 | if (result->IsSharedFunctionInfo()) { |
| 375 | Handle<SharedFunctionInfo> |
| 376 | function_info(SharedFunctionInfo::cast(result)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 377 | if (generation != 0) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 378 | Put(source, context, function_info); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 379 | } |
| 380 | Counters::compilation_cache_hits.Increment(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 381 | return function_info; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 382 | } else { |
| 383 | Counters::compilation_cache_misses.Increment(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 384 | return Handle<SharedFunctionInfo>::null(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 389 | MaybeObject* CompilationCacheEval::TryTablePut( |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 398 | Handle<CompilationCacheTable> CompilationCacheEval::TablePut( |
| 399 | Handle<String> source, |
| 400 | Handle<Context> context, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 401 | Handle<SharedFunctionInfo> function_info) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 402 | CALL_HEAP_FUNCTION(TryTablePut(source, context, function_info), |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 403 | CompilationCacheTable); |
| 404 | } |
| 405 | |
| 406 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 407 | void CompilationCacheEval::Put(Handle<String> source, |
| 408 | Handle<Context> context, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 409 | Handle<SharedFunctionInfo> function_info) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 410 | HandleScope scope; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 411 | SetFirstTable(TablePut(source, context, function_info)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +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 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 445 | MaybeObject* CompilationCacheRegExp::TryTablePut( |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 454 | Handle<CompilationCacheTable> CompilationCacheRegExp::TablePut( |
| 455 | Handle<String> source, |
| 456 | JSRegExp::Flags flags, |
| 457 | Handle<FixedArray> data) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 458 | CALL_HEAP_FUNCTION(TryTablePut(source, flags, data), CompilationCacheTable); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 462 | void CompilationCacheRegExp::Put(Handle<String> source, |
| 463 | JSRegExp::Flags flags, |
| 464 | Handle<FixedArray> data) { |
| 465 | HandleScope scope; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 466 | SetFirstTable(TablePut(source, flags, data)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 470 | Handle<SharedFunctionInfo> CompilationCache::LookupScript(Handle<String> source, |
| 471 | Handle<Object> name, |
| 472 | int line_offset, |
| 473 | int column_offset) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 474 | if (!IsEnabled()) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 475 | return Handle<SharedFunctionInfo>::null(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 476 | } |
| 477 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 478 | return script.Lookup(source, name, line_offset, column_offset); |
| 479 | } |
| 480 | |
| 481 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 482 | Handle<SharedFunctionInfo> CompilationCache::LookupEval(Handle<String> source, |
| 483 | Handle<Context> context, |
| 484 | bool is_global) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 485 | if (!IsEnabled()) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 486 | return Handle<SharedFunctionInfo>::null(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 487 | } |
| 488 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 489 | Handle<SharedFunctionInfo> result; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 490 | if (is_global) { |
| 491 | result = eval_global.Lookup(source, context); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 492 | } else { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 493 | result = eval_contextual.Lookup(source, context); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 494 | } |
| 495 | return result; |
| 496 | } |
| 497 | |
| 498 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 499 | Handle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source, |
| 500 | JSRegExp::Flags flags) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 501 | if (!IsEnabled()) { |
| 502 | return Handle<FixedArray>::null(); |
| 503 | } |
| 504 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 505 | return reg_exp.Lookup(source, flags); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 509 | void CompilationCache::PutScript(Handle<String> source, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 510 | Handle<SharedFunctionInfo> function_info) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 511 | if (!IsEnabled()) { |
| 512 | return; |
| 513 | } |
| 514 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 515 | script.Put(source, function_info); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | |
| 519 | void CompilationCache::PutEval(Handle<String> source, |
| 520 | Handle<Context> context, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 521 | bool is_global, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 522 | Handle<SharedFunctionInfo> function_info) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 523 | if (!IsEnabled()) { |
| 524 | return; |
| 525 | } |
| 526 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 527 | HandleScope scope; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 528 | if (is_global) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 529 | eval_global.Put(source, context, function_info); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 530 | } else { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 531 | eval_contextual.Put(source, context, function_info); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 532 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 536 | |
| 537 | void CompilationCache::PutRegExp(Handle<String> source, |
| 538 | JSRegExp::Flags flags, |
| 539 | Handle<FixedArray> data) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 540 | if (!IsEnabled()) { |
| 541 | return; |
| 542 | } |
| 543 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 544 | reg_exp.Put(source, flags, data); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 548 | void CompilationCache::Clear() { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 549 | for (int i = 0; i < kSubCacheCount; i++) { |
| 550 | subcaches[i]->Clear(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 554 | void CompilationCache::Iterate(ObjectVisitor* v) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 555 | for (int i = 0; i < kSubCacheCount; i++) { |
| 556 | subcaches[i]->Iterate(v); |
| 557 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [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 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 568 | void CompilationCache::MarkCompactPrologue() { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 569 | for (int i = 0; i < kSubCacheCount; i++) { |
| 570 | subcaches[i]->Age(); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 571 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 575 | void CompilationCache::Enable() { |
| 576 | enabled = true; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | void CompilationCache::Disable() { |
| 581 | enabled = false; |
| 582 | Clear(); |
| 583 | } |
| 584 | |
| 585 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 586 | } } // namespace v8::internal |