Commit bbd5889f authored by vitalyr@chromium.org's avatar vitalyr@chromium.org

Trim fast elements tail on significant length decreases.

Runtime_RegExpExecMultiple had to be updated because it assumed
setting an array's length to zero still keeps some capacity in the
backing store.

R=ager@chromium.org

Review URL: http://codereview.chromium.org/7237004

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8396 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent b652d79b
......@@ -7533,12 +7533,24 @@ MaybeObject* JSObject::SetElementsLength(Object* len) {
{ MaybeObject* maybe_obj = EnsureWritableFastElements();
if (!maybe_obj->ToObject(&obj)) return maybe_obj;
}
int old_length = FastD2I(JSArray::cast(this)->length()->Number());
// NOTE: We may be able to optimize this by removing the
// last part of the elements backing storage array and
// setting the capacity to the new size.
for (int i = value; i < old_length; i++) {
FixedArray::cast(elements())->set_the_hole(i);
FixedArray* fast_elements = FixedArray::cast(elements());
if (2 * value <= old_capacity) {
// If more than half the elements won't be used, trim the array.
if (value == 0) {
initialize_elements();
} else {
fast_elements->set_length(value);
Address filler_start = fast_elements->address() +
FixedArray::OffsetOfElementAt(value);
int filler_size = (old_capacity - value) * kPointerSize;
GetHeap()->CreateFillerObjectAt(filler_start, filler_size);
}
} else {
// Otherwise, fill the unused tail with holes.
int old_length = FastD2I(JSArray::cast(this)->length()->Number());
for (int i = value; i < old_length; i++) {
fast_elements->set_the_hole(i);
}
}
JSArray::cast(this)->set_length(Smi::cast(smi_length));
}
......
......@@ -3532,7 +3532,8 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExecMultiple) {
if (result_array->HasFastElements()) {
result_elements =
Handle<FixedArray>(FixedArray::cast(result_array->elements()));
} else {
}
if (result_elements.is_null() || result_elements->length() < 16) {
result_elements = isolate->factory()->NewFixedArrayWithHoles(16);
}
FixedArrayBuilder builder(result_elements);
......
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