Cache experimental natives sources as external strings.

R=ulan@chromium.org
BUG=v8:4054
LOG=N

Review URL: https://codereview.chromium.org/1114043002

Cr-Commit-Position: refs/heads/master@{#28176}
This commit is contained in:
yangguo 2015-04-30 08:10:43 -07:00 committed by Commit bot
parent 0327f8de2e
commit 3ba71e1bbd
7 changed files with 53 additions and 26 deletions

View File

@ -363,7 +363,7 @@ StartupData V8::CreateSnapshotDataBlob(const char* custom_source) {
{
HandleScope scope(isolate);
for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) {
internal_isolate->bootstrapper()->NativesSourceLookup(i);
internal_isolate->bootstrapper()->SourceLookup<i::Natives>(i);
}
}
// If we don't do this then we end up with a stray root pointing at the

View File

@ -26,12 +26,29 @@ Bootstrapper::Bootstrapper(Isolate* isolate)
extensions_cache_(Script::TYPE_EXTENSION) {}
Handle<String> Bootstrapper::NativesSourceLookup(int index) {
DCHECK(0 <= index && index < Natives::GetBuiltinsCount());
template <class Source>
inline FixedArray* GetCache(Heap* heap);
template <>
FixedArray* GetCache<Natives>(Heap* heap) {
return heap->natives_source_cache();
}
template <>
FixedArray* GetCache<ExperimentalNatives>(Heap* heap) {
return heap->experimental_natives_source_cache();
}
template <class Source>
Handle<String> Bootstrapper::SourceLookup(int index) {
DCHECK(0 <= index && index < Source::GetBuiltinsCount());
Heap* heap = isolate_->heap();
if (heap->natives_source_cache()->get(index)->IsUndefined()) {
if (GetCache<Source>(heap)->get(index)->IsUndefined()) {
// We can use external strings for the natives.
Vector<const char> source = Natives::GetScriptSource(index);
Vector<const char> source = Source::GetScriptSource(index);
NativesExternalStringResource* resource =
new NativesExternalStringResource(source.start(), source.length());
// We do not expect this to throw an exception. Change this if it does.
@ -40,14 +57,18 @@ Handle<String> Bootstrapper::NativesSourceLookup(int index) {
.ToHandleChecked();
// Mark this external string with a special map.
source_code->set_map(isolate_->heap()->native_source_string_map());
heap->natives_source_cache()->set(index, *source_code);
GetCache<Source>(heap)->set(index, *source_code);
}
Handle<Object> cached_source(heap->natives_source_cache()->get(index),
isolate_);
Handle<Object> cached_source(GetCache<Source>(heap)->get(index), isolate_);
return Handle<String>::cast(cached_source);
}
template Handle<String> Bootstrapper::SourceLookup<Natives>(int index);
template Handle<String> Bootstrapper::SourceLookup<ExperimentalNatives>(
int index);
void Bootstrapper::Initialize(bool create_heap_objects) {
extensions_cache_.Initialize(isolate_, create_heap_objects);
}
@ -94,12 +115,11 @@ void Bootstrapper::TearDownExtensions() {
}
void Bootstrapper::TearDown() {
Object* natives_source_cache = isolate_->heap()->natives_source_cache();
if (natives_source_cache->IsFixedArray()) {
FixedArray* natives_source_array = FixedArray::cast(natives_source_cache);
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
Object* natives_source = natives_source_array->get(i);
void DeleteNativeSources(Object* maybe_array) {
if (maybe_array->IsFixedArray()) {
FixedArray* array = FixedArray::cast(maybe_array);
for (int i = 0; i < array->length(); i++) {
Object* natives_source = array->get(i);
if (!natives_source->IsUndefined()) {
const NativesExternalStringResource* resource =
reinterpret_cast<const NativesExternalStringResource*>(
@ -108,7 +128,12 @@ void Bootstrapper::TearDown() {
}
}
}
}
void Bootstrapper::TearDown() {
DeleteNativeSources(isolate_->heap()->natives_source_cache());
DeleteNativeSources(isolate_->heap()->experimental_natives_source_cache());
extensions_cache_.Initialize(isolate_, false); // Yes, symmetrical
}
@ -1409,19 +1434,15 @@ void Genesis::InitializeExperimentalGlobal() {
bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
Vector<const char> name = Natives::GetScriptName(index);
Handle<String> source_code =
isolate->bootstrapper()->NativesSourceLookup(index);
isolate->bootstrapper()->SourceLookup<Natives>(index);
return CompileNative(isolate, name, source_code);
}
bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
Vector<const char> name = ExperimentalNatives::GetScriptName(index);
Factory* factory = isolate->factory();
Handle<String> source_code;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
isolate, source_code,
factory->NewStringFromAscii(ExperimentalNatives::GetScriptSource(index)),
false);
Handle<String> source_code =
isolate->bootstrapper()->SourceLookup<ExperimentalNatives>(index);
return CompileNative(isolate, name, source_code);
}

View File

@ -87,7 +87,8 @@ class Bootstrapper final {
void Iterate(ObjectVisitor* v);
// Accessor for the native scripts source code.
Handle<String> NativesSourceLookup(int index);
template <class Source>
Handle<String> SourceLookup(int index);
// Tells whether bootstrapping is active.
bool IsActive() const { return nesting_ != 0; }

View File

@ -636,7 +636,7 @@ bool Debug::CompileDebuggerScript(Isolate* isolate, int index) {
// Find source and name for the requested script.
Handle<String> source_code =
isolate->bootstrapper()->NativesSourceLookup(index);
isolate->bootstrapper()->SourceLookup<Natives>(index);
Vector<const char> name = Natives::GetScriptName(index);
Handle<String> script_name =
factory->NewStringFromAscii(name).ToHandleChecked();

View File

@ -3069,6 +3069,9 @@ void Heap::CreateInitialObjects() {
set_natives_source_cache(
*factory->NewFixedArray(Natives::GetBuiltinsCount()));
set_experimental_natives_source_cache(
*factory->NewFixedArray(ExperimentalNatives::GetBuiltinsCount()));
set_undefined_cell(*factory->NewCell(factory->undefined_value()));
// The symbol registry is initialized lazily.

View File

@ -173,6 +173,8 @@ namespace internal {
V(Code, js_entry_code, JsEntryCode) \
V(Code, js_construct_entry_code, JsConstructEntryCode) \
V(FixedArray, natives_source_cache, NativesSourceCache) \
V(FixedArray, experimental_natives_source_cache, \
ExperimentalNativesSourceCache) \
V(Script, empty_script, EmptyScript) \
V(NameDictionary, intrinsic_function_names, IntrinsicFunctionNames) \
V(Cell, undefined_cell, UndefinedCell) \

View File

@ -296,7 +296,7 @@ UNINITIALIZED_TEST(PartialSerialization) {
{
HandleScope scope(isolate);
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
isolate->bootstrapper()->NativesSourceLookup(i);
isolate->bootstrapper()->SourceLookup<Natives>(i);
}
}
heap->CollectAllGarbage();
@ -419,7 +419,7 @@ UNINITIALIZED_TEST(ContextSerialization) {
{
HandleScope scope(isolate);
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
isolate->bootstrapper()->NativesSourceLookup(i);
isolate->bootstrapper()->SourceLookup<Natives>(i);
}
}
// If we don't do this then we end up with a stray root pointing at the
@ -554,7 +554,7 @@ UNINITIALIZED_TEST(CustomContextSerialization) {
{
HandleScope scope(isolate);
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
isolate->bootstrapper()->NativesSourceLookup(i);
isolate->bootstrapper()->SourceLookup<Natives>(i);
}
}
// If we don't do this then we end up with a stray root pointing at the