Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5 | #include "src/compilation-cache.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/assembler.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include "src/counters.h" |
| 9 | #include "src/factory.h" |
| 10 | #include "src/objects-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11 | |
| 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15 | |
| 16 | // The number of generations for each sub cache. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17 | static const int kRegExpGenerations = 2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 19 | // Initial size of each compilation cache table allocated. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 20 | static const int kInitialCacheSize = 64; |
| 21 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 22 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 23 | CompilationCache::CompilationCache(Isolate* isolate) |
| 24 | : isolate_(isolate), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 25 | script_(isolate, 1), |
| 26 | eval_global_(isolate, 1), |
| 27 | eval_contextual_(isolate, 1), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 28 | reg_exp_(isolate, kRegExpGenerations), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 29 | enabled_(true) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 30 | CompilationSubCache* subcaches[kSubCacheCount] = |
| 31 | {&script_, &eval_global_, &eval_contextual_, ®_exp_}; |
| 32 | for (int i = 0; i < kSubCacheCount; ++i) { |
| 33 | subcaches_[i] = subcaches[i]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 38 | CompilationCache::~CompilationCache() {} |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 39 | |
| 40 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 42 | DCHECK(generation < generations_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 43 | Handle<CompilationCacheTable> result; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 44 | if (tables_[generation]->IsUndefined(isolate())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 45 | result = CompilationCacheTable::New(isolate(), kInitialCacheSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | tables_[generation] = *result; |
| 47 | } else { |
| 48 | CompilationCacheTable* table = |
| 49 | CompilationCacheTable::cast(tables_[generation]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 50 | result = Handle<CompilationCacheTable>(table, isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 51 | } |
| 52 | return result; |
| 53 | } |
| 54 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 55 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | void CompilationSubCache::Age() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 57 | // Don't directly age single-generation caches. |
| 58 | if (generations_ == 1) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 59 | if (!tables_[0]->IsUndefined(isolate())) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 60 | CompilationCacheTable::cast(tables_[0])->Age(); |
| 61 | } |
| 62 | return; |
| 63 | } |
| 64 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 65 | // Age the generations implicitly killing off the oldest. |
| 66 | for (int i = generations_ - 1; i > 0; i--) { |
| 67 | tables_[i] = tables_[i - 1]; |
| 68 | } |
| 69 | |
| 70 | // Set the first generation as unborn. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 71 | tables_[0] = isolate()->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 75 | void CompilationSubCache::IterateFunctions(ObjectVisitor* v) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 76 | Object* undefined = isolate()->heap()->undefined_value(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 77 | for (int i = 0; i < generations_; i++) { |
| 78 | if (tables_[i] != undefined) { |
| 79 | reinterpret_cast<CompilationCacheTable*>(tables_[i])->IterateElements(v); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | void CompilationSubCache::Iterate(ObjectVisitor* v) { |
| 86 | v->VisitPointers(&tables_[0], &tables_[generations_]); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void CompilationSubCache::Clear() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 91 | MemsetPointer(tables_, isolate()->heap()->undefined_value(), generations_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 95 | void CompilationSubCache::Remove(Handle<SharedFunctionInfo> function_info) { |
| 96 | // Probe the script generation tables. Make sure not to leak handles |
| 97 | // into the caller's handle scope. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 98 | { HandleScope scope(isolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 99 | for (int generation = 0; generation < generations(); generation++) { |
| 100 | Handle<CompilationCacheTable> table = GetTable(generation); |
| 101 | table->Remove(*function_info); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 107 | CompilationCacheScript::CompilationCacheScript(Isolate* isolate, |
| 108 | int generations) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 109 | : CompilationSubCache(isolate, generations) {} |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 110 | |
| 111 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | // We only re-use a cached function for some script source code if the |
| 113 | // script originates from the same place. This is to avoid issues |
| 114 | // when reporting errors, etc. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 115 | bool CompilationCacheScript::HasOrigin(Handle<SharedFunctionInfo> function_info, |
| 116 | Handle<Object> name, int line_offset, |
| 117 | int column_offset, |
| 118 | ScriptOriginOptions resource_options) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 119 | Handle<Script> script = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 120 | Handle<Script>(Script::cast(function_info->script()), isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | // If the script name isn't set, the boilerplate script should have |
| 122 | // an undefined name to have the same origin. |
| 123 | if (name.is_null()) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 124 | return script->name()->IsUndefined(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 125 | } |
| 126 | // Do the fast bailout checks first. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 127 | if (line_offset != script->line_offset()) return false; |
| 128 | if (column_offset != script->column_offset()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | // Check that both names are strings. If not, no match. |
| 130 | if (!name->IsString() || !script->name()->IsString()) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 131 | // Are the origin_options same? |
| 132 | if (resource_options.Flags() != script->origin_options().Flags()) |
| 133 | return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 134 | // Compare the two name strings for equality. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 135 | return String::Equals(Handle<String>::cast(name), |
| 136 | Handle<String>(String::cast(script->name()))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | // TODO(245): Need to allow identical code from different contexts to |
| 141 | // be cached in the same script generation. Currently the first use |
| 142 | // will be cached, but subsequent code from different source / line |
| 143 | // won't. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 144 | Handle<SharedFunctionInfo> CompilationCacheScript::Lookup( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 145 | Handle<String> source, Handle<Object> name, int line_offset, |
| 146 | int column_offset, ScriptOriginOptions resource_options, |
| 147 | Handle<Context> context, LanguageMode language_mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | Object* result = NULL; |
| 149 | int generation; |
| 150 | |
| 151 | // Probe the script generation tables. Make sure not to leak handles |
| 152 | // into the caller's handle scope. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 153 | { HandleScope scope(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 154 | for (generation = 0; generation < generations(); generation++) { |
| 155 | Handle<CompilationCacheTable> table = GetTable(generation); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 156 | Handle<Object> probe = table->Lookup(source, context, language_mode); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 157 | if (probe->IsSharedFunctionInfo()) { |
| 158 | Handle<SharedFunctionInfo> function_info = |
| 159 | Handle<SharedFunctionInfo>::cast(probe); |
| 160 | // Break when we've found a suitable shared function info that |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | // matches the origin. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 162 | if (HasOrigin(function_info, name, line_offset, column_offset, |
| 163 | resource_options)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 164 | result = *function_info; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 165 | break; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | // Once outside the manacles of the handle scope, we need to recheck |
| 172 | // to see if we actually found a cached script. If so, we return a |
| 173 | // handle created in the caller's handle scope. |
| 174 | if (result != NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 175 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result), |
| 176 | isolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 177 | DCHECK( |
| 178 | HasOrigin(shared, name, line_offset, column_offset, resource_options)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 179 | // If the script was found in a later generation, we promote it to |
| 180 | // the first generation to let it survive longer in the cache. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 181 | if (generation != 0) Put(source, context, language_mode, shared); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 182 | isolate()->counters()->compilation_cache_hits()->Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 183 | return shared; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 184 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 185 | isolate()->counters()->compilation_cache_misses()->Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 186 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 191 | void CompilationCacheScript::Put(Handle<String> source, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 192 | Handle<Context> context, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 193 | LanguageMode language_mode, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 194 | Handle<SharedFunctionInfo> function_info) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 195 | HandleScope scope(isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 196 | Handle<CompilationCacheTable> table = GetFirstTable(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 197 | SetFirstTable(CompilationCacheTable::Put(table, source, context, |
| 198 | language_mode, function_info)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 202 | MaybeHandle<SharedFunctionInfo> CompilationCacheEval::Lookup( |
| 203 | Handle<String> source, Handle<SharedFunctionInfo> outer_info, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 204 | LanguageMode language_mode, int scope_position) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 205 | HandleScope scope(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 206 | // Make sure not to leak the table into the surrounding handle |
| 207 | // scope. Otherwise, we risk keeping old tables around even after |
| 208 | // having cleared the cache. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 209 | Handle<Object> result = isolate()->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 210 | int generation; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 211 | for (generation = 0; generation < generations(); generation++) { |
| 212 | Handle<CompilationCacheTable> table = GetTable(generation); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 213 | result = |
| 214 | table->LookupEval(source, outer_info, language_mode, scope_position); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 215 | if (result->IsSharedFunctionInfo()) break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 216 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 217 | if (result->IsSharedFunctionInfo()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 218 | Handle<SharedFunctionInfo> function_info = |
| 219 | Handle<SharedFunctionInfo>::cast(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 220 | if (generation != 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 221 | Put(source, outer_info, function_info, scope_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 222 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 223 | isolate()->counters()->compilation_cache_hits()->Increment(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 224 | return scope.CloseAndEscape(function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 225 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 226 | isolate()->counters()->compilation_cache_misses()->Increment(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 227 | return MaybeHandle<SharedFunctionInfo>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | |
| 232 | void CompilationCacheEval::Put(Handle<String> source, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 233 | Handle<SharedFunctionInfo> outer_info, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 234 | Handle<SharedFunctionInfo> function_info, |
| 235 | int scope_position) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 236 | HandleScope scope(isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 237 | Handle<CompilationCacheTable> table = GetFirstTable(); |
| 238 | table = CompilationCacheTable::PutEval(table, source, outer_info, |
| 239 | function_info, scope_position); |
| 240 | SetFirstTable(table); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 244 | MaybeHandle<FixedArray> CompilationCacheRegExp::Lookup( |
| 245 | Handle<String> source, |
| 246 | JSRegExp::Flags flags) { |
| 247 | HandleScope scope(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 248 | // Make sure not to leak the table into the surrounding handle |
| 249 | // scope. Otherwise, we risk keeping old tables around even after |
| 250 | // having cleared the cache. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 251 | Handle<Object> result = isolate()->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 252 | int generation; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 253 | for (generation = 0; generation < generations(); generation++) { |
| 254 | Handle<CompilationCacheTable> table = GetTable(generation); |
| 255 | result = table->LookupRegExp(source, flags); |
| 256 | if (result->IsFixedArray()) break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 257 | } |
| 258 | if (result->IsFixedArray()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 259 | Handle<FixedArray> data = Handle<FixedArray>::cast(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 260 | if (generation != 0) { |
| 261 | Put(source, flags, data); |
| 262 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 263 | isolate()->counters()->compilation_cache_hits()->Increment(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 264 | return scope.CloseAndEscape(data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 266 | isolate()->counters()->compilation_cache_misses()->Increment(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 267 | return MaybeHandle<FixedArray>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | |
| 272 | void CompilationCacheRegExp::Put(Handle<String> source, |
| 273 | JSRegExp::Flags flags, |
| 274 | Handle<FixedArray> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 275 | HandleScope scope(isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 276 | Handle<CompilationCacheTable> table = GetFirstTable(); |
| 277 | SetFirstTable(CompilationCacheTable::PutRegExp(table, source, flags, data)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 281 | void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) { |
| 282 | if (!IsEnabled()) return; |
| 283 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 284 | eval_global_.Remove(function_info); |
| 285 | eval_contextual_.Remove(function_info); |
| 286 | script_.Remove(function_info); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 290 | MaybeHandle<SharedFunctionInfo> CompilationCache::LookupScript( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 291 | Handle<String> source, Handle<Object> name, int line_offset, |
| 292 | int column_offset, ScriptOriginOptions resource_options, |
| 293 | Handle<Context> context, LanguageMode language_mode) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 294 | if (!IsEnabled()) return MaybeHandle<SharedFunctionInfo>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 295 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 296 | return script_.Lookup(source, name, line_offset, column_offset, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 297 | resource_options, context, language_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 301 | MaybeHandle<SharedFunctionInfo> CompilationCache::LookupEval( |
| 302 | Handle<String> source, Handle<SharedFunctionInfo> outer_info, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 303 | Handle<Context> context, LanguageMode language_mode, int scope_position) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 304 | if (!IsEnabled()) return MaybeHandle<SharedFunctionInfo>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 305 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 306 | MaybeHandle<SharedFunctionInfo> result; |
| 307 | if (context->IsNativeContext()) { |
| 308 | result = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 309 | eval_global_.Lookup(source, outer_info, language_mode, scope_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 311 | DCHECK(scope_position != RelocInfo::kNoPosition); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 312 | result = eval_contextual_.Lookup(source, outer_info, language_mode, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 313 | scope_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 314 | } |
| 315 | return result; |
| 316 | } |
| 317 | |
| 318 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 319 | MaybeHandle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source, |
| 320 | JSRegExp::Flags flags) { |
| 321 | if (!IsEnabled()) return MaybeHandle<FixedArray>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 322 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 323 | return reg_exp_.Lookup(source, flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | |
| 327 | void CompilationCache::PutScript(Handle<String> source, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 328 | Handle<Context> context, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 329 | LanguageMode language_mode, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 330 | Handle<SharedFunctionInfo> function_info) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 331 | if (!IsEnabled()) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 332 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 333 | script_.Put(source, context, language_mode, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | |
| 337 | void CompilationCache::PutEval(Handle<String> source, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 338 | Handle<SharedFunctionInfo> outer_info, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 339 | Handle<Context> context, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 340 | Handle<SharedFunctionInfo> function_info, |
| 341 | int scope_position) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 342 | if (!IsEnabled()) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 343 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 344 | HandleScope scope(isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 345 | if (context->IsNativeContext()) { |
| 346 | eval_global_.Put(source, outer_info, function_info, scope_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 347 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 348 | DCHECK(scope_position != RelocInfo::kNoPosition); |
| 349 | eval_contextual_.Put(source, outer_info, function_info, scope_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | |
| 354 | |
| 355 | void CompilationCache::PutRegExp(Handle<String> source, |
| 356 | JSRegExp::Flags flags, |
| 357 | Handle<FixedArray> data) { |
| 358 | if (!IsEnabled()) { |
| 359 | return; |
| 360 | } |
| 361 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 362 | reg_exp_.Put(source, flags, data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | |
| 366 | void CompilationCache::Clear() { |
| 367 | for (int i = 0; i < kSubCacheCount; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 368 | subcaches_[i]->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 372 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 373 | void CompilationCache::Iterate(ObjectVisitor* v) { |
| 374 | for (int i = 0; i < kSubCacheCount; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 375 | subcaches_[i]->Iterate(v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 380 | void CompilationCache::IterateFunctions(ObjectVisitor* v) { |
| 381 | for (int i = 0; i < kSubCacheCount; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 382 | subcaches_[i]->IterateFunctions(v); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 387 | void CompilationCache::MarkCompactPrologue() { |
| 388 | for (int i = 0; i < kSubCacheCount; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 389 | subcaches_[i]->Age(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | |
| 394 | void CompilationCache::Enable() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 395 | enabled_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | |
| 399 | void CompilationCache::Disable() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 400 | enabled_ = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 401 | Clear(); |
| 402 | } |
| 403 | |
| 404 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 405 | } // namespace internal |
| 406 | } // namespace v8 |