| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 script_histogram_initialized_(false) { } | 137 script_histogram_initialized_(false) { } |
| 138 | 138 |
| 139 | 139 |
| 140 // We only re-use a cached function for some script source code if the | 140 // We only re-use a cached function for some script source code if the |
| 141 // script originates from the same place. This is to avoid issues | 141 // script originates from the same place. This is to avoid issues |
| 142 // when reporting errors, etc. | 142 // when reporting errors, etc. |
| 143 bool CompilationCacheScript::HasOrigin( | 143 bool CompilationCacheScript::HasOrigin( |
| 144 Handle<SharedFunctionInfo> function_info, | 144 Handle<SharedFunctionInfo> function_info, |
| 145 Handle<Object> name, | 145 Handle<Object> name, |
| 146 int line_offset, | 146 int line_offset, |
| 147 int column_offset) { | 147 int column_offset, |
| 148 bool is_shared_cross_origin) { |
| 148 Handle<Script> script = | 149 Handle<Script> script = |
| 149 Handle<Script>(Script::cast(function_info->script()), isolate()); | 150 Handle<Script>(Script::cast(function_info->script()), isolate()); |
| 150 // If the script name isn't set, the boilerplate script should have | 151 // If the script name isn't set, the boilerplate script should have |
| 151 // an undefined name to have the same origin. | 152 // an undefined name to have the same origin. |
| 152 if (name.is_null()) { | 153 if (name.is_null()) { |
| 153 return script->name()->IsUndefined(); | 154 return script->name()->IsUndefined(); |
| 154 } | 155 } |
| 155 // Do the fast bailout checks first. | 156 // Do the fast bailout checks first. |
| 156 if (line_offset != script->line_offset()->value()) return false; | 157 if (line_offset != script->line_offset()->value()) return false; |
| 157 if (column_offset != script->column_offset()->value()) return false; | 158 if (column_offset != script->column_offset()->value()) return false; |
| 158 // Check that both names are strings. If not, no match. | 159 // Check that both names are strings. If not, no match. |
| 159 if (!name->IsString() || !script->name()->IsString()) return false; | 160 if (!name->IsString() || !script->name()->IsString()) return false; |
| 161 // Were both scripts tagged by the embedder as being shared cross-origin? |
| 162 if (is_shared_cross_origin != script->is_shared_cross_origin()) return false; |
| 160 // Compare the two name strings for equality. | 163 // Compare the two name strings for equality. |
| 161 return String::cast(*name)->Equals(String::cast(script->name())); | 164 return String::cast(*name)->Equals(String::cast(script->name())); |
| 162 } | 165 } |
| 163 | 166 |
| 164 | 167 |
| 165 // TODO(245): Need to allow identical code from different contexts to | 168 // TODO(245): Need to allow identical code from different contexts to |
| 166 // be cached in the same script generation. Currently the first use | 169 // be cached in the same script generation. Currently the first use |
| 167 // will be cached, but subsequent code from different source / line | 170 // will be cached, but subsequent code from different source / line |
| 168 // won't. | 171 // won't. |
| 169 Handle<SharedFunctionInfo> CompilationCacheScript::Lookup( | 172 Handle<SharedFunctionInfo> CompilationCacheScript::Lookup( |
| 170 Handle<String> source, | 173 Handle<String> source, |
| 171 Handle<Object> name, | 174 Handle<Object> name, |
| 172 int line_offset, | 175 int line_offset, |
| 173 int column_offset, | 176 int column_offset, |
| 177 bool is_shared_cross_origin, |
| 174 Handle<Context> context) { | 178 Handle<Context> context) { |
| 175 Object* result = NULL; | 179 Object* result = NULL; |
| 176 int generation; | 180 int generation; |
| 177 | 181 |
| 178 // Probe the script generation tables. Make sure not to leak handles | 182 // Probe the script generation tables. Make sure not to leak handles |
| 179 // into the caller's handle scope. | 183 // into the caller's handle scope. |
| 180 { HandleScope scope(isolate()); | 184 { HandleScope scope(isolate()); |
| 181 for (generation = 0; generation < generations(); generation++) { | 185 for (generation = 0; generation < generations(); generation++) { |
| 182 Handle<CompilationCacheTable> table = GetTable(generation); | 186 Handle<CompilationCacheTable> table = GetTable(generation); |
| 183 Handle<Object> probe(table->Lookup(*source, *context), isolate()); | 187 Handle<Object> probe(table->Lookup(*source, *context), isolate()); |
| 184 if (probe->IsSharedFunctionInfo()) { | 188 if (probe->IsSharedFunctionInfo()) { |
| 185 Handle<SharedFunctionInfo> function_info = | 189 Handle<SharedFunctionInfo> function_info = |
| 186 Handle<SharedFunctionInfo>::cast(probe); | 190 Handle<SharedFunctionInfo>::cast(probe); |
| 187 // Break when we've found a suitable shared function info that | 191 // Break when we've found a suitable shared function info that |
| 188 // matches the origin. | 192 // matches the origin. |
| 189 if (HasOrigin(function_info, name, line_offset, column_offset)) { | 193 if (HasOrigin(function_info, |
| 194 name, |
| 195 line_offset, |
| 196 column_offset, |
| 197 is_shared_cross_origin)) { |
| 190 result = *function_info; | 198 result = *function_info; |
| 191 break; | 199 break; |
| 192 } | 200 } |
| 193 } | 201 } |
| 194 } | 202 } |
| 195 } | 203 } |
| 196 | 204 |
| 197 if (!script_histogram_initialized_) { | 205 if (!script_histogram_initialized_) { |
| 198 script_histogram_ = isolate()->stats_table()->CreateHistogram( | 206 script_histogram_ = isolate()->stats_table()->CreateHistogram( |
| 199 "V8.ScriptCache", | 207 "V8.ScriptCache", |
| 200 0, | 208 0, |
| 201 kScriptGenerations, | 209 kScriptGenerations, |
| 202 kScriptGenerations + 1); | 210 kScriptGenerations + 1); |
| 203 script_histogram_initialized_ = true; | 211 script_histogram_initialized_ = true; |
| 204 } | 212 } |
| 205 | 213 |
| 206 if (script_histogram_ != NULL) { | 214 if (script_histogram_ != NULL) { |
| 207 // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss. | 215 // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss. |
| 208 isolate()->stats_table()->AddHistogramSample(script_histogram_, generation); | 216 isolate()->stats_table()->AddHistogramSample(script_histogram_, generation); |
| 209 } | 217 } |
| 210 | 218 |
| 211 // Once outside the manacles of the handle scope, we need to recheck | 219 // Once outside the manacles of the handle scope, we need to recheck |
| 212 // to see if we actually found a cached script. If so, we return a | 220 // to see if we actually found a cached script. If so, we return a |
| 213 // handle created in the caller's handle scope. | 221 // handle created in the caller's handle scope. |
| 214 if (result != NULL) { | 222 if (result != NULL) { |
| 215 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result), | 223 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result), |
| 216 isolate()); | 224 isolate()); |
| 217 ASSERT(HasOrigin(shared, name, line_offset, column_offset)); | 225 ASSERT(HasOrigin(shared, |
| 226 name, |
| 227 line_offset, |
| 228 column_offset, |
| 229 is_shared_cross_origin)); |
| 218 // If the script was found in a later generation, we promote it to | 230 // If the script was found in a later generation, we promote it to |
| 219 // the first generation to let it survive longer in the cache. | 231 // the first generation to let it survive longer in the cache. |
| 220 if (generation != 0) Put(source, context, shared); | 232 if (generation != 0) Put(source, context, shared); |
| 221 isolate()->counters()->compilation_cache_hits()->Increment(); | 233 isolate()->counters()->compilation_cache_hits()->Increment(); |
| 222 return shared; | 234 return shared; |
| 223 } else { | 235 } else { |
| 224 isolate()->counters()->compilation_cache_misses()->Increment(); | 236 isolate()->counters()->compilation_cache_misses()->Increment(); |
| 225 return Handle<SharedFunctionInfo>::null(); | 237 return Handle<SharedFunctionInfo>::null(); |
| 226 } | 238 } |
| 227 } | 239 } |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 eval_contextual_.Remove(function_info); | 396 eval_contextual_.Remove(function_info); |
| 385 script_.Remove(function_info); | 397 script_.Remove(function_info); |
| 386 } | 398 } |
| 387 | 399 |
| 388 | 400 |
| 389 Handle<SharedFunctionInfo> CompilationCache::LookupScript( | 401 Handle<SharedFunctionInfo> CompilationCache::LookupScript( |
| 390 Handle<String> source, | 402 Handle<String> source, |
| 391 Handle<Object> name, | 403 Handle<Object> name, |
| 392 int line_offset, | 404 int line_offset, |
| 393 int column_offset, | 405 int column_offset, |
| 406 bool is_shared_cross_origin, |
| 394 Handle<Context> context) { | 407 Handle<Context> context) { |
| 395 if (!IsEnabled()) { | 408 if (!IsEnabled()) { |
| 396 return Handle<SharedFunctionInfo>::null(); | 409 return Handle<SharedFunctionInfo>::null(); |
| 397 } | 410 } |
| 398 | 411 |
| 399 return script_.Lookup(source, name, line_offset, column_offset, context); | 412 return script_.Lookup(source, |
| 413 name, |
| 414 line_offset, |
| 415 column_offset, |
| 416 is_shared_cross_origin, |
| 417 context); |
| 400 } | 418 } |
| 401 | 419 |
| 402 | 420 |
| 403 Handle<SharedFunctionInfo> CompilationCache::LookupEval( | 421 Handle<SharedFunctionInfo> CompilationCache::LookupEval( |
| 404 Handle<String> source, | 422 Handle<String> source, |
| 405 Handle<Context> context, | 423 Handle<Context> context, |
| 406 bool is_global, | 424 bool is_global, |
| 407 LanguageMode language_mode, | 425 LanguageMode language_mode, |
| 408 int scope_position) { | 426 int scope_position) { |
| 409 if (!IsEnabled()) { | 427 if (!IsEnabled()) { |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 } | 526 } |
| 509 | 527 |
| 510 | 528 |
| 511 void CompilationCache::Disable() { | 529 void CompilationCache::Disable() { |
| 512 enabled_ = false; | 530 enabled_ = false; |
| 513 Clear(); | 531 Clear(); |
| 514 } | 532 } |
| 515 | 533 |
| 516 | 534 |
| 517 } } // namespace v8::internal | 535 } } // namespace v8::internal |
| OLD | NEW |