Index: src/compiler.cc |
=================================================================== |
--- src/compiler.cc (revision 11527) |
+++ src/compiler.cc (working copy) |
@@ -600,6 +600,50 @@ |
} |
+static void AddToOptimizedCodeMap(Handle<SharedFunctionInfo> shared, |
Michael Starzinger
2012/05/23 11:16:29
Can we move this into the the SharedFunctionInfo c
fschneider
2012/06/14 11:08:23
Done.
|
+ Handle<Context> global_context, |
+ Handle<Code> code, |
+ Handle<FixedArray> literals) { |
+ ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION); |
+ ASSERT(global_context->IsGlobalContext()); |
+ const int kEntryLength = 3; |
Michael Starzinger
2012/05/23 11:16:29
Moving these constants into SharedFunctionInfo and
fschneider
2012/06/14 11:08:23
Done.
|
+ Object* value = shared->optimized_code_map(); |
+ Handle<FixedArray> new_map; |
+ if (value->IsSmi()) { |
+ // No optimized code map. |
+ ASSERT_EQ(0, Smi::cast(value)->value()); |
+ // Crate 3 entries per context {context, code, literals}. |
+ new_map = FACTORY->NewFixedArray(kEntryLength); |
+ new_map->set(0, *global_context); |
+ new_map->set(1, *code); |
+ new_map->set(2, *literals); |
+ } else { |
+ Handle<FixedArray> old_map(FixedArray::cast(value)); |
+ ASSERT_EQ(-1, shared->SearchOptimizedCodeMap(*global_context)); |
+ int old_length = old_map->length(); |
+ int new_length = old_length + kEntryLength; |
+ new_map = FACTORY->NewFixedArray(new_length); |
+ for (int i = 0; i < old_length; i += kEntryLength) { |
Michael Starzinger
2012/05/23 11:16:29
Can we use FixedArray::CopyTo or even a handlified
fschneider
2012/06/14 11:08:23
Done.
|
+ new_map->set(i, old_map->get(i)); |
+ new_map->set(i + 1, old_map->get(i + 1)); |
+ new_map->set(i + 2, old_map->get(i + 2)); |
+ } |
+ new_map->set(old_length, *global_context); |
+ new_map->set(old_length + 1, *code); |
+ new_map->set(old_length + 2, *literals); |
+ } |
+#ifdef DEBUG |
+ for (int i = 0; i < new_map->length(); i += kEntryLength) { |
+ ASSERT(new_map->get(i)->IsGlobalContext()); |
+ ASSERT(new_map->get(i + 1)->IsCode()); |
+ ASSERT(Code::cast(new_map->get(i + 1))->kind() == Code::OPTIMIZED_FUNCTION); |
+ ASSERT(new_map->get(i + 2)->IsFixedArray()); |
+ } |
+#endif |
+ shared->set_optimized_code_map(*new_map); |
+} |
+ |
+ |
bool Compiler::CompileLazy(CompilationInfo* info) { |
Isolate* isolate = info->isolate(); |
@@ -614,6 +658,25 @@ |
int compiled_size = shared->end_position() - shared->start_position(); |
isolate->counters()->total_compile_size()->Increment(compiled_size); |
+ if (FLAG_cache_optimized_code && info->IsOptimizing()) { |
+ Handle<JSFunction> function = info->closure(); |
+ ASSERT(!function.is_null()); |
+ Handle<Context> global_context(function->context()->global_context()); |
+ int index = function->shared()->SearchOptimizedCodeMap(*global_context); |
+ if (index > 0) { |
+ if (FLAG_trace_opt) { |
+ PrintF(" [Found optimized code for"); |
+ function->PrintName(); |
+ PrintF("\n"); |
+ } |
+ Code* code = Code::cast( |
+ FixedArray::cast(shared->optimized_code_map())->get(index)); |
+ ASSERT(code != NULL); |
+ function->ReplaceCode(code); |
+ return true; |
+ } |
+ } |
+ |
// Generate the AST for the lazily compiled function. |
if (ParserApi::Parse(info, kNoParsingFlags)) { |
// Measure how long it takes to do the lazy compilation; only take the |
@@ -645,6 +708,25 @@ |
if (info->IsOptimizing()) { |
ASSERT(shared->scope_info() != ScopeInfo::Empty()); |
function->ReplaceCode(*code); |
+ if (FLAG_cache_optimized_code && |
+ code->kind() == Code::OPTIMIZED_FUNCTION) { |
+ Handle<SharedFunctionInfo> shared(function->shared()); |
+ Handle<Context> global_context(function->context()->global_context()); |
+ |
+ // Create literals array that will be shared for this global context. |
+ int number_of_literals = shared->num_literals(); |
+ Handle<FixedArray> literals = |
+ isolate->factory()->NewFixedArray(number_of_literals); |
+ if (number_of_literals > 0) { |
+ // Store the object, regexp and array functions in the literals |
+ // array prefix. These functions will be used when creating |
+ // object, regexp and array literals in this function. |
+ literals->set(JSFunction::kLiteralGlobalContextIndex, |
+ function->context()->global_context()); |
+ } |
+ |
+ AddToOptimizedCodeMap(shared, global_context, code, literals); |
+ } |
} else { |
// Update the shared function info with the compiled code and the |
// scope info. Please note, that the order of the shared function |