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