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

Reland 10309: Ensure large Smi-only arrays don't transition to FAST_DOUBLE_ARRAY

TBR=jkummerow@chromium.org
BUG=none
TEST=none

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@10311 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 5d85a044
...@@ -1219,7 +1219,7 @@ void JSObject::ValidateSmiOnlyElements() { ...@@ -1219,7 +1219,7 @@ void JSObject::ValidateSmiOnlyElements() {
map != heap->free_space_map()) { map != heap->free_space_map()) {
for (int i = 0; i < fixed_array->length(); i++) { for (int i = 0; i < fixed_array->length(); i++) {
Object* current = fixed_array->get(i); Object* current = fixed_array->get(i);
ASSERT(current->IsSmi() || current == heap->the_hole_value()); ASSERT(current->IsSmi() || current->IsTheHole());
} }
} }
} }
...@@ -1290,22 +1290,37 @@ MaybeObject* JSObject::EnsureCanContainElements(FixedArrayBase* elements, ...@@ -1290,22 +1290,37 @@ MaybeObject* JSObject::EnsureCanContainElements(FixedArrayBase* elements,
} }
void JSObject::set_elements(FixedArrayBase* value, WriteBarrierMode mode) { void JSObject::set_map_and_elements(Map* new_map,
FixedArrayBase* value,
WriteBarrierMode mode) {
ASSERT(value->HasValidElements());
#ifdef DEBUG
ValidateSmiOnlyElements();
#endif
if (new_map != NULL) {
if (mode == UPDATE_WRITE_BARRIER) {
set_map(new_map);
} else {
ASSERT(mode == SKIP_WRITE_BARRIER);
set_map_no_write_barrier(new_map);
}
}
ASSERT((map()->has_fast_elements() || ASSERT((map()->has_fast_elements() ||
map()->has_fast_smi_only_elements()) == map()->has_fast_smi_only_elements()) ==
(value->map() == GetHeap()->fixed_array_map() || (value->map() == GetHeap()->fixed_array_map() ||
value->map() == GetHeap()->fixed_cow_array_map())); value->map() == GetHeap()->fixed_cow_array_map()));
ASSERT(map()->has_fast_double_elements() == ASSERT(map()->has_fast_double_elements() ==
value->IsFixedDoubleArray()); value->IsFixedDoubleArray());
ASSERT(value->HasValidElements());
#ifdef DEBUG
ValidateSmiOnlyElements();
#endif
WRITE_FIELD(this, kElementsOffset, value); WRITE_FIELD(this, kElementsOffset, value);
CONDITIONAL_WRITE_BARRIER(GetHeap(), this, kElementsOffset, value, mode); CONDITIONAL_WRITE_BARRIER(GetHeap(), this, kElementsOffset, value, mode);
} }
void JSObject::set_elements(FixedArrayBase* value, WriteBarrierMode mode) {
set_map_and_elements(NULL, value, mode);
}
void JSObject::initialize_properties() { void JSObject::initialize_properties() {
ASSERT(!GetHeap()->InNewSpace(GetHeap()->empty_fixed_array())); ASSERT(!GetHeap()->InNewSpace(GetHeap()->empty_fixed_array()));
WRITE_FIELD(this, kPropertiesOffset, GetHeap()->empty_fixed_array()); WRITE_FIELD(this, kPropertiesOffset, GetHeap()->empty_fixed_array());
......
...@@ -8188,10 +8188,13 @@ MaybeObject* JSObject::SetFastElementsCapacityAndLength( ...@@ -8188,10 +8188,13 @@ MaybeObject* JSObject::SetFastElementsCapacityAndLength(
Map* new_map = NULL; Map* new_map = NULL;
if (elements()->map() != heap->non_strict_arguments_elements_map()) { if (elements()->map() != heap->non_strict_arguments_elements_map()) {
Object* object; Object* object;
// The resized array has FAST_SMI_ONLY_ELEMENTS if the capacity mode forces
// it, or if it's allowed and the old elements array contained only SMIs.
bool has_fast_smi_only_elements = bool has_fast_smi_only_elements =
(set_capacity_mode == kAllowSmiOnlyElements) && (set_capacity_mode == kForceSmiOnlyElements) ||
((set_capacity_mode == kAllowSmiOnlyElements) &&
(elements()->map()->has_fast_smi_only_elements() || (elements()->map()->has_fast_smi_only_elements() ||
elements() == heap->empty_fixed_array()); elements() == heap->empty_fixed_array()));
ElementsKind elements_kind = has_fast_smi_only_elements ElementsKind elements_kind = has_fast_smi_only_elements
? FAST_SMI_ONLY_ELEMENTS ? FAST_SMI_ONLY_ELEMENTS
: FAST_ELEMENTS; : FAST_ELEMENTS;
...@@ -8209,8 +8212,7 @@ MaybeObject* JSObject::SetFastElementsCapacityAndLength( ...@@ -8209,8 +8212,7 @@ MaybeObject* JSObject::SetFastElementsCapacityAndLength(
WriteBarrierMode mode(new_elements->GetWriteBarrierMode(no_gc)); WriteBarrierMode mode(new_elements->GetWriteBarrierMode(no_gc));
CopyFastElementsToFast(FixedArray::cast(old_elements_raw), CopyFastElementsToFast(FixedArray::cast(old_elements_raw),
new_elements, mode); new_elements, mode);
set_map(new_map); set_map_and_elements(new_map, new_elements);
set_elements(new_elements);
break; break;
} }
case DICTIONARY_ELEMENTS: { case DICTIONARY_ELEMENTS: {
...@@ -8219,8 +8221,7 @@ MaybeObject* JSObject::SetFastElementsCapacityAndLength( ...@@ -8219,8 +8221,7 @@ MaybeObject* JSObject::SetFastElementsCapacityAndLength(
CopySlowElementsToFast(NumberDictionary::cast(old_elements_raw), CopySlowElementsToFast(NumberDictionary::cast(old_elements_raw),
new_elements, new_elements,
mode); mode);
set_map(new_map); set_map_and_elements(new_map, new_elements);
set_elements(new_elements);
break; break;
} }
case NON_STRICT_ARGUMENTS_ELEMENTS: { case NON_STRICT_ARGUMENTS_ELEMENTS: {
...@@ -9241,11 +9242,20 @@ MaybeObject* JSObject::SetDictionaryElement(uint32_t index, ...@@ -9241,11 +9242,20 @@ MaybeObject* JSObject::SetDictionaryElement(uint32_t index,
} else { } else {
new_length = dictionary->max_number_key() + 1; new_length = dictionary->max_number_key() + 1;
} }
MaybeObject* result = CanConvertToFastDoubleElements() SetFastElementsCapacityMode set_capacity_mode = FLAG_smi_only_arrays
? kAllowSmiOnlyElements
: kDontAllowSmiOnlyElements;
bool has_smi_only_elements = false;
bool should_convert_to_fast_double_elements =
ShouldConvertToFastDoubleElements(&has_smi_only_elements);
if (has_smi_only_elements) {
set_capacity_mode = kForceSmiOnlyElements;
}
MaybeObject* result = should_convert_to_fast_double_elements
? SetFastDoubleElementsCapacityAndLength(new_length, new_length) ? SetFastDoubleElementsCapacityAndLength(new_length, new_length)
: SetFastElementsCapacityAndLength(new_length, : SetFastElementsCapacityAndLength(new_length,
new_length, new_length,
kDontAllowSmiOnlyElements); set_capacity_mode);
if (result->IsFailure()) return result; if (result->IsFailure()) return result;
#ifdef DEBUG #ifdef DEBUG
if (FLAG_trace_normalization) { if (FLAG_trace_normalization) {
...@@ -9724,17 +9734,25 @@ bool JSObject::ShouldConvertToFastElements() { ...@@ -9724,17 +9734,25 @@ bool JSObject::ShouldConvertToFastElements() {
} }
bool JSObject::CanConvertToFastDoubleElements() { bool JSObject::ShouldConvertToFastDoubleElements(
bool* has_smi_only_elements) {
*has_smi_only_elements = false;
if (FLAG_unbox_double_arrays) { if (FLAG_unbox_double_arrays) {
ASSERT(HasDictionaryElements()); ASSERT(HasDictionaryElements());
NumberDictionary* dictionary = NumberDictionary::cast(elements()); NumberDictionary* dictionary = NumberDictionary::cast(elements());
bool found_double = false;
for (int i = 0; i < dictionary->Capacity(); i++) { for (int i = 0; i < dictionary->Capacity(); i++) {
Object* key = dictionary->KeyAt(i); Object* key = dictionary->KeyAt(i);
if (key->IsNumber()) { if (key->IsNumber()) {
if (!dictionary->ValueAt(i)->IsNumber()) return false; Object* value = dictionary->ValueAt(i);
if (!value->IsNumber()) return false;
if (!value->IsSmi()) {
found_double = true;
} }
} }
return true; }
*has_smi_only_elements = !found_double;
return found_double;
} else { } else {
return false; return false;
} }
......
...@@ -1473,6 +1473,11 @@ class JSObject: public JSReceiver { ...@@ -1473,6 +1473,11 @@ class JSObject: public JSReceiver {
bool HasDictionaryArgumentsElements(); bool HasDictionaryArgumentsElements();
inline NumberDictionary* element_dictionary(); // Gets slow elements. inline NumberDictionary* element_dictionary(); // Gets slow elements.
inline void set_map_and_elements(
Map* map,
FixedArrayBase* value,
WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
// Requires: HasFastElements(). // Requires: HasFastElements().
MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements(); MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements();
...@@ -1644,8 +1649,9 @@ class JSObject: public JSReceiver { ...@@ -1644,8 +1649,9 @@ class JSObject: public JSReceiver {
// elements. // elements.
bool ShouldConvertToFastElements(); bool ShouldConvertToFastElements();
// Returns true if the elements of JSObject contains only values that can be // Returns true if the elements of JSObject contains only values that can be
// represented in a FixedDoubleArray. // represented in a FixedDoubleArray and has at least one value that can only
bool CanConvertToFastDoubleElements(); // be represented as a double and not a Smi.
bool ShouldConvertToFastDoubleElements(bool* has_smi_only_elements);
// Tells whether the index'th element is present. // Tells whether the index'th element is present.
bool HasElementWithReceiver(JSReceiver* receiver, uint32_t index); bool HasElementWithReceiver(JSReceiver* receiver, uint32_t index);
...@@ -1708,6 +1714,7 @@ class JSObject: public JSReceiver { ...@@ -1708,6 +1714,7 @@ class JSObject: public JSReceiver {
enum SetFastElementsCapacityMode { enum SetFastElementsCapacityMode {
kAllowSmiOnlyElements, kAllowSmiOnlyElements,
kForceSmiOnlyElements,
kDontAllowSmiOnlyElements kDontAllowSmiOnlyElements
}; };
......
// Copyright 2011 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.
// See: http://code.google.com/p/v8/issues/detail?id=1878
// Flags: --allow-natives-syntax
var count = 1e5;
var arr = new Array(count);
assertFalse(%HasFastDoubleElements(arr));
for (var i = 0; i < count; i++) {
arr[i] = 0;
}
assertFalse(%HasFastDoubleElements(arr));
assertTrue(%HasFastSmiOnlyElements(arr));
...@@ -32,7 +32,7 @@ function get_double_array() { ...@@ -32,7 +32,7 @@ function get_double_array() {
var i = 0; var i = 0;
while (!%HasFastDoubleElements(a)) { while (!%HasFastDoubleElements(a)) {
a[i] = i; a[i] = i;
i++; i += 0.5;
} }
assertTrue(%HasFastDoubleElements(a)); assertTrue(%HasFastDoubleElements(a));
a.length = 1; a.length = 1;
......
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