From 015af31d5fd655bdfbc680863dedfa2b54203239 Mon Sep 17 00:00:00 2001 From: "bak@chromium.org" Date: Thu, 9 Oct 2008 13:34:17 +0000 Subject: [PATCH] - Optimized JSArray allocation in runtime system by using NewJSArrayWithElements. Review URL: http://codereview.chromium.org/7013 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@479 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/factory.cc | 10 ++++++---- src/handles.cc | 9 ++------- src/jsregexp.cc | 32 ++++++++++++++++---------------- src/objects.cc | 1 - 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/factory.cc b/src/factory.cc index 2ef40c45..2eeb3b9 100644 --- a/src/factory.cc +++ b/src/factory.cc @@ -283,10 +283,12 @@ Handle Factory::NewReferenceError(Handle message) { Handle Factory::NewError(const char* maker, const char* type, Vector< Handle > args) { HandleScope scope; - Handle array = NewJSArray(args.length()); - for (int i = 0; i < args.length(); i++) - SetElement(array, i, args[i]); - Handle result = NewError(maker, type, array); + Handle array = Factory::NewFixedArray(args.length()); + for (int i = 0; i < args.length(); i++) { + array->set(i, *args[i]); + } + Handle object = Factory::NewJSArrayWithElements(array); + Handle result = NewError(maker, type, object); return result.EscapeFrom(&scope); } diff --git a/src/handles.cc b/src/handles.cc index 4f7c0d1..5f86792 100644 --- a/src/handles.cc +++ b/src/handles.cc @@ -391,13 +391,8 @@ Handle GetKeysInFixedArrayFor(Handle object) { Handle GetKeysFor(Handle object) { Counters::for_in.Increment(); - - Handle content = GetKeysInFixedArrayFor(object); - - // Allocate the JSArray with the result. - Handle obj = Factory::NewJSArray(content->length()); - Handle::cast(obj)->SetContent(*content); - return Handle::cast(obj); + Handle elements = GetKeysInFixedArrayFor(object); + return Factory::NewJSArrayWithElements(elements); } diff --git a/src/jsregexp.cc b/src/jsregexp.cc index 854c136..768c719 100644 --- a/src/jsregexp.cc +++ b/src/jsregexp.cc @@ -220,10 +220,11 @@ Handle RegExpImpl::AtomExec(Handle re, LOG(RegExpExecEvent(re, start_index, subject)); int value = Runtime::StringMatch(subject, needle, start_index); if (value == -1) return Factory::null_value(); - Handle result = Factory::NewJSArray(2); - SetElement(result, 0, Handle(Smi::FromInt(value))); - SetElement(result, 1, Handle(Smi::FromInt(value + needle->length()))); - return result; + + Handle array = Factory::NewFixedArray(2); + array->set(0, Smi::FromInt(value)); + array->set(1, Smi::FromInt(value + needle->length())); + return Factory::NewJSArrayWithElements(array); } @@ -244,14 +245,15 @@ Handle RegExpImpl::AtomExecGlobal(Handle re, if (value == -1) break; HandleScope scope; int end = value + needle_length; - Handle pair = Factory::NewJSArray(2); - SetElement(pair, 0, Handle(Smi::FromInt(value))); - SetElement(pair, 1, Handle(Smi::FromInt(end))); + + Handle array = Factory::NewFixedArray(2); + array->set(0, Smi::FromInt(value)); + array->set(1, Smi::FromInt(end)); + Handle pair = Factory::NewJSArrayWithElements(array); SetElement(result, match_count, pair); match_count++; index = end; - if (needle_length == 0) - index++; + if (needle_length == 0) index++; } return result; } @@ -311,7 +313,6 @@ Handle RegExpImpl::JsreCompile(Handle re, } ASSERT(code != NULL); - // Convert the return address to a ByteArray pointer. Handle internal( ByteArray::FromDataStartAddress(reinterpret_cast
(code))); @@ -368,14 +369,13 @@ Handle RegExpImpl::JsreExecOnce(Handle regexp, return Handle(Top::Throw(*regexp_err)); } - Handle result = Factory::NewJSArray(2 * (num_captures+1)); - + Handle array = Factory::NewFixedArray(2 * (num_captures+1)); // The captures come in (start, end+1) pairs. for (int i = 0; i < 2 * (num_captures+1); i += 2) { - SetElement(result, i, Handle(Smi::FromInt(offsets_vector[i]))); - SetElement(result, i+1, Handle(Smi::FromInt(offsets_vector[i+1]))); + array->set(i, Smi::FromInt(offsets_vector[i])); + array->set(i+1, Smi::FromInt(offsets_vector[i+1])); } - return result; + return Factory::NewJSArrayWithElements(array); } @@ -450,7 +450,7 @@ Handle RegExpImpl::JsreExecGlobal(Handle regexp, int previous_index = 0; - Handle result = Factory::NewJSArray(0); + Handle result = Factory::NewJSArray(0); int i = 0; Handle matches; diff --git a/src/objects.cc b/src/objects.cc index 69e7df2..1bc6f42 100644 --- a/src/objects.cc +++ b/src/objects.cc @@ -4695,7 +4695,6 @@ Object* JSObject::SetFastElement(uint32_t index, Object* value) { return SetElement(index, value); } - Object* JSObject::SetElement(uint32_t index, Object* value) { // Check access rights if needed. if (IsAccessCheckNeeded() && -- 2.7.4