Commit 3ba71e1b authored by yangguo's avatar yangguo Committed by Commit bot

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}
parent 0327f8de
...@@ -363,7 +363,7 @@ StartupData V8::CreateSnapshotDataBlob(const char* custom_source) { ...@@ -363,7 +363,7 @@ StartupData V8::CreateSnapshotDataBlob(const char* custom_source) {
{ {
HandleScope scope(isolate); HandleScope scope(isolate);
for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) { 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 // If we don't do this then we end up with a stray root pointing at the
......
...@@ -26,12 +26,29 @@ Bootstrapper::Bootstrapper(Isolate* isolate) ...@@ -26,12 +26,29 @@ Bootstrapper::Bootstrapper(Isolate* isolate)
extensions_cache_(Script::TYPE_EXTENSION) {} extensions_cache_(Script::TYPE_EXTENSION) {}
Handle<String> Bootstrapper::NativesSourceLookup(int index) { template <class Source>
DCHECK(0 <= index && index < Natives::GetBuiltinsCount()); 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(); 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. // We can use external strings for the natives.
Vector<const char> source = Natives::GetScriptSource(index); Vector<const char> source = Source::GetScriptSource(index);
NativesExternalStringResource* resource = NativesExternalStringResource* resource =
new NativesExternalStringResource(source.start(), source.length()); new NativesExternalStringResource(source.start(), source.length());
// We do not expect this to throw an exception. Change this if it does. // We do not expect this to throw an exception. Change this if it does.
...@@ -40,14 +57,18 @@ Handle<String> Bootstrapper::NativesSourceLookup(int index) { ...@@ -40,14 +57,18 @@ Handle<String> Bootstrapper::NativesSourceLookup(int index) {
.ToHandleChecked(); .ToHandleChecked();
// Mark this external string with a special map. // Mark this external string with a special map.
source_code->set_map(isolate_->heap()->native_source_string_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), Handle<Object> cached_source(GetCache<Source>(heap)->get(index), isolate_);
isolate_);
return Handle<String>::cast(cached_source); 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) { void Bootstrapper::Initialize(bool create_heap_objects) {
extensions_cache_.Initialize(isolate_, create_heap_objects); extensions_cache_.Initialize(isolate_, create_heap_objects);
} }
...@@ -94,12 +115,11 @@ void Bootstrapper::TearDownExtensions() { ...@@ -94,12 +115,11 @@ void Bootstrapper::TearDownExtensions() {
} }
void Bootstrapper::TearDown() { void DeleteNativeSources(Object* maybe_array) {
Object* natives_source_cache = isolate_->heap()->natives_source_cache(); if (maybe_array->IsFixedArray()) {
if (natives_source_cache->IsFixedArray()) { FixedArray* array = FixedArray::cast(maybe_array);
FixedArray* natives_source_array = FixedArray::cast(natives_source_cache); for (int i = 0; i < array->length(); i++) {
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { Object* natives_source = array->get(i);
Object* natives_source = natives_source_array->get(i);
if (!natives_source->IsUndefined()) { if (!natives_source->IsUndefined()) {
const NativesExternalStringResource* resource = const NativesExternalStringResource* resource =
reinterpret_cast<const NativesExternalStringResource*>( reinterpret_cast<const NativesExternalStringResource*>(
...@@ -108,7 +128,12 @@ void Bootstrapper::TearDown() { ...@@ -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 extensions_cache_.Initialize(isolate_, false); // Yes, symmetrical
} }
...@@ -1409,19 +1434,15 @@ void Genesis::InitializeExperimentalGlobal() { ...@@ -1409,19 +1434,15 @@ void Genesis::InitializeExperimentalGlobal() {
bool Genesis::CompileBuiltin(Isolate* isolate, int index) { bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
Vector<const char> name = Natives::GetScriptName(index); Vector<const char> name = Natives::GetScriptName(index);
Handle<String> source_code = Handle<String> source_code =
isolate->bootstrapper()->NativesSourceLookup(index); isolate->bootstrapper()->SourceLookup<Natives>(index);
return CompileNative(isolate, name, source_code); return CompileNative(isolate, name, source_code);
} }
bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) { bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
Vector<const char> name = ExperimentalNatives::GetScriptName(index); Vector<const char> name = ExperimentalNatives::GetScriptName(index);
Factory* factory = isolate->factory(); Handle<String> source_code =
Handle<String> source_code; isolate->bootstrapper()->SourceLookup<ExperimentalNatives>(index);
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
isolate, source_code,
factory->NewStringFromAscii(ExperimentalNatives::GetScriptSource(index)),
false);
return CompileNative(isolate, name, source_code); return CompileNative(isolate, name, source_code);
} }
......
...@@ -87,7 +87,8 @@ class Bootstrapper final { ...@@ -87,7 +87,8 @@ class Bootstrapper final {
void Iterate(ObjectVisitor* v); void Iterate(ObjectVisitor* v);
// Accessor for the native scripts source code. // 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. // Tells whether bootstrapping is active.
bool IsActive() const { return nesting_ != 0; } bool IsActive() const { return nesting_ != 0; }
......
...@@ -636,7 +636,7 @@ bool Debug::CompileDebuggerScript(Isolate* isolate, int index) { ...@@ -636,7 +636,7 @@ bool Debug::CompileDebuggerScript(Isolate* isolate, int index) {
// Find source and name for the requested script. // Find source and name for the requested script.
Handle<String> source_code = Handle<String> source_code =
isolate->bootstrapper()->NativesSourceLookup(index); isolate->bootstrapper()->SourceLookup<Natives>(index);
Vector<const char> name = Natives::GetScriptName(index); Vector<const char> name = Natives::GetScriptName(index);
Handle<String> script_name = Handle<String> script_name =
factory->NewStringFromAscii(name).ToHandleChecked(); factory->NewStringFromAscii(name).ToHandleChecked();
......
...@@ -3069,6 +3069,9 @@ void Heap::CreateInitialObjects() { ...@@ -3069,6 +3069,9 @@ void Heap::CreateInitialObjects() {
set_natives_source_cache( set_natives_source_cache(
*factory->NewFixedArray(Natives::GetBuiltinsCount())); *factory->NewFixedArray(Natives::GetBuiltinsCount()));
set_experimental_natives_source_cache(
*factory->NewFixedArray(ExperimentalNatives::GetBuiltinsCount()));
set_undefined_cell(*factory->NewCell(factory->undefined_value())); set_undefined_cell(*factory->NewCell(factory->undefined_value()));
// The symbol registry is initialized lazily. // The symbol registry is initialized lazily.
......
...@@ -173,6 +173,8 @@ namespace internal { ...@@ -173,6 +173,8 @@ namespace internal {
V(Code, js_entry_code, JsEntryCode) \ V(Code, js_entry_code, JsEntryCode) \
V(Code, js_construct_entry_code, JsConstructEntryCode) \ V(Code, js_construct_entry_code, JsConstructEntryCode) \
V(FixedArray, natives_source_cache, NativesSourceCache) \ V(FixedArray, natives_source_cache, NativesSourceCache) \
V(FixedArray, experimental_natives_source_cache, \
ExperimentalNativesSourceCache) \
V(Script, empty_script, EmptyScript) \ V(Script, empty_script, EmptyScript) \
V(NameDictionary, intrinsic_function_names, IntrinsicFunctionNames) \ V(NameDictionary, intrinsic_function_names, IntrinsicFunctionNames) \
V(Cell, undefined_cell, UndefinedCell) \ V(Cell, undefined_cell, UndefinedCell) \
......
...@@ -296,7 +296,7 @@ UNINITIALIZED_TEST(PartialSerialization) { ...@@ -296,7 +296,7 @@ UNINITIALIZED_TEST(PartialSerialization) {
{ {
HandleScope scope(isolate); HandleScope scope(isolate);
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
isolate->bootstrapper()->NativesSourceLookup(i); isolate->bootstrapper()->SourceLookup<Natives>(i);
} }
} }
heap->CollectAllGarbage(); heap->CollectAllGarbage();
...@@ -419,7 +419,7 @@ UNINITIALIZED_TEST(ContextSerialization) { ...@@ -419,7 +419,7 @@ UNINITIALIZED_TEST(ContextSerialization) {
{ {
HandleScope scope(isolate); HandleScope scope(isolate);
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { 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 // If we don't do this then we end up with a stray root pointing at the
...@@ -554,7 +554,7 @@ UNINITIALIZED_TEST(CustomContextSerialization) { ...@@ -554,7 +554,7 @@ UNINITIALIZED_TEST(CustomContextSerialization) {
{ {
HandleScope scope(isolate); HandleScope scope(isolate);
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { 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 // If we don't do this then we end up with a stray root pointing at the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment