Commit b3337196 authored by danno@chromium.org's avatar danno@chromium.org

Properly handle FixedDoubleArrays in sort()

R=jkummerow@chromium.org
BUG=91008
TEST=regress-91008.js

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8782 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 9226cfe5
...@@ -10679,19 +10679,19 @@ MaybeObject* JSObject::PrepareElementsForSort(uint32_t limit) { ...@@ -10679,19 +10679,19 @@ MaybeObject* JSObject::PrepareElementsForSort(uint32_t limit) {
set_map(new_map); set_map(new_map);
set_elements(fast_elements); set_elements(fast_elements);
} else { } else if (!HasFastDoubleElements()) {
Object* obj; Object* obj;
{ MaybeObject* maybe_obj = EnsureWritableFastElements(); { MaybeObject* maybe_obj = EnsureWritableFastElements();
if (!maybe_obj->ToObject(&obj)) return maybe_obj; if (!maybe_obj->ToObject(&obj)) return maybe_obj;
} }
} }
ASSERT(HasFastElements()); ASSERT(HasFastElements() || HasFastDoubleElements());
// Collect holes at the end, undefined before that and the rest at the // Collect holes at the end, undefined before that and the rest at the
// start, and return the number of non-hole, non-undefined values. // start, and return the number of non-hole, non-undefined values.
FixedArray* elements = FixedArray::cast(this->elements()); FixedArrayBase* elements_base = FixedArrayBase::cast(this->elements());
uint32_t elements_length = static_cast<uint32_t>(elements->length()); uint32_t elements_length = static_cast<uint32_t>(elements_base->length());
if (limit > elements_length) { if (limit > elements_length) {
limit = elements_length ; limit = elements_length ;
} }
...@@ -10710,47 +10710,78 @@ MaybeObject* JSObject::PrepareElementsForSort(uint32_t limit) { ...@@ -10710,47 +10710,78 @@ MaybeObject* JSObject::PrepareElementsForSort(uint32_t limit) {
result_double = HeapNumber::cast(new_double); result_double = HeapNumber::cast(new_double);
} }
AssertNoAllocation no_alloc; uint32_t result = 0;
if (elements_base->map() == heap->fixed_double_array_map()) {
// Split elements into defined, undefined and the_hole, in that order. FixedDoubleArray* elements = FixedDoubleArray::cast(elements_base);
// Only count locations for undefined and the hole, and fill them afterwards. // Split elements into defined and the_hole, in that order.
WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_alloc); unsigned int holes = limit;
unsigned int undefs = limit; // Assume most arrays contain no holes and undefined values, so minimize the
unsigned int holes = limit; // number of stores of non-undefined, non-the-hole values.
// Assume most arrays contain no holes and undefined values, so minimize the for (unsigned int i = 0; i < holes; i++) {
// number of stores of non-undefined, non-the-hole values. if (elements->is_the_hole(i)) {
for (unsigned int i = 0; i < undefs; i++) { holes--;
Object* current = elements->get(i); } else {
if (current->IsTheHole()) { continue;
holes--; }
undefs--; // Position i needs to be filled.
} else if (current->IsUndefined()) { while (holes > i) {
undefs--; if (elements->is_the_hole(holes)) {
} else { holes--;
continue; } else {
elements->set(i, elements->get(holes));
break;
}
}
} }
// Position i needs to be filled. result = holes;
while (undefs > i) { while (holes < limit) {
current = elements->get(undefs); elements->set_the_hole(holes);
holes++;
}
} else {
FixedArray* elements = FixedArray::cast(elements_base);
AssertNoAllocation no_alloc;
// Split elements into defined, undefined and the_hole, in that order. Only
// count locations for undefined and the hole, and fill them afterwards.
WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_alloc);
unsigned int undefs = limit;
unsigned int holes = limit;
// Assume most arrays contain no holes and undefined values, so minimize the
// number of stores of non-undefined, non-the-hole values.
for (unsigned int i = 0; i < undefs; i++) {
Object* current = elements->get(i);
if (current->IsTheHole()) { if (current->IsTheHole()) {
holes--; holes--;
undefs--; undefs--;
} else if (current->IsUndefined()) { } else if (current->IsUndefined()) {
undefs--; undefs--;
} else { } else {
elements->set(i, current, write_barrier); continue;
break; }
// Position i needs to be filled.
while (undefs > i) {
current = elements->get(undefs);
if (current->IsTheHole()) {
holes--;
undefs--;
} else if (current->IsUndefined()) {
undefs--;
} else {
elements->set(i, current, write_barrier);
break;
}
} }
} }
} result = undefs;
uint32_t result = undefs; while (undefs < holes) {
while (undefs < holes) { elements->set_undefined(undefs);
elements->set_undefined(undefs); undefs++;
undefs++; }
} while (holes < limit) {
while (holes < limit) { elements->set_the_hole(holes);
elements->set_the_hole(holes); holes++;
holes++; }
} }
if (result <= static_cast<uint32_t>(Smi::kMaxValue)) { if (result <= static_cast<uint32_t>(Smi::kMaxValue)) {
......
// Copyright 2010 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
function testsort(n) {
var numbers=new Array(n);
for (var i=0;i<n;i++) numbers[i]=i;
delete numbers[50];
delete numbers[150];
delete numbers[25000];
delete numbers[n-1];
delete numbers[n-2];
delete numbers[30];
delete numbers[2];
delete numbers[1];
delete numbers[0];
numbers.sort();
}
testsort(100000)
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