Refactor copying of maps and descriptor arrays.

Mainly ensure we don't copy descriptor arrays we'll throw away immediately afterwards.

Review URL: https://chromiumcodereview.appspot.com/10700160

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@12044 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
verwaest@chromium.org 2012-07-11 14:29:16 +00:00
parent 432576b7c8
commit ef701cb7ba
6 changed files with 119 additions and 128 deletions

View File

@ -465,14 +465,15 @@ Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
} }
Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) { Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map); CALL_HEAP_FUNCTION(
isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
} }
Handle<Map> Factory::CopyMap(Handle<Map> src, Handle<Map> Factory::CopyMap(Handle<Map> src,
int extra_inobject_properties) { int extra_inobject_properties) {
Handle<Map> copy = CopyMapDropDescriptors(src); Handle<Map> copy = CopyWithPreallocatedFieldDescriptors(src);
// Check that we do not overflow the instance size when adding the // Check that we do not overflow the instance size when adding the
// extra inobject properties. // extra inobject properties.
int instance_size_delta = extra_inobject_properties * kPointerSize; int instance_size_delta = extra_inobject_properties * kPointerSize;

View File

@ -222,7 +222,7 @@ class Factory {
Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function); Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
Handle<Map> CopyMapDropDescriptors(Handle<Map> map); Handle<Map> CopyWithPreallocatedFieldDescriptors(Handle<Map> map);
// Copy the map adding more inobject properties if possible without // Copy the map adding more inobject properties if possible without
// overflowing the instance size. // overflowing the instance size.

View File

@ -4180,10 +4180,10 @@ MaybeObject* Heap::AllocateGlobalObject(JSFunction* constructor) {
InitializeJSObjectFromMap(global, dictionary, map); InitializeJSObjectFromMap(global, dictionary, map);
// Create a new map for the global object. // Create a new map for the global object.
{ MaybeObject* maybe_obj = map->CopyDropDescriptors(); Map* new_map;
if (!maybe_obj->ToObject(&obj)) return maybe_obj; { MaybeObject* maybe_map = map->CopyDropDescriptors();
if (!maybe_map->To(&new_map)) return maybe_map;
} }
Map* new_map = Map::cast(obj);
// Set up the global object as a normalized object. // Set up the global object as a normalized object.
global->set_map(new_map); global->set_map(new_map);

View File

@ -514,11 +514,11 @@ MaybeObject* JSObject::DeleteNormalizedProperty(String* name, DeleteMode mode) {
// map change to invalidate any ICs that think they can load // map change to invalidate any ICs that think they can load
// from the DontDelete cell without checking if it contains // from the DontDelete cell without checking if it contains
// the hole value. // the hole value.
Object* new_map; Map* new_map;
{ MaybeObject* maybe_new_map = map()->CopyDropDescriptors(); { MaybeObject* maybe_new_map = map()->CopyDropDescriptors();
if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; if (!maybe_new_map->To(&new_map)) return maybe_new_map;
} }
set_map(Map::cast(new_map)); set_map(new_map);
} }
JSGlobalPropertyCell* cell = JSGlobalPropertyCell* cell =
JSGlobalPropertyCell::cast(dictionary->ValueAt(entry)); JSGlobalPropertyCell::cast(dictionary->ValueAt(entry));
@ -1526,13 +1526,18 @@ MaybeObject* JSObject::AddFastProperty(String* name,
PropertyAttributes attributes, PropertyAttributes attributes,
StoreFromKeyed store_mode) { StoreFromKeyed store_mode) {
ASSERT(!IsJSGlobalProxy()); ASSERT(!IsJSGlobalProxy());
ASSERT(map()->instance_descriptors()->Search(name) ==
DescriptorArray::kNotFound);
// Normalize the object if the name is an actual string (not the // Normalize the object if the name is an actual string (not the
// hidden symbols) and is not a real identifier. // hidden symbols) and is not a real identifier.
// Normalize the object if it will have too many fast properties.
Isolate* isolate = GetHeap()->isolate(); Isolate* isolate = GetHeap()->isolate();
StringInputBuffer buffer(name); StringInputBuffer buffer(name);
if (!IsIdentifier(isolate->unicode_cache(), &buffer) if ((!IsIdentifier(isolate->unicode_cache(), &buffer)
&& name != isolate->heap()->hidden_symbol()) { && name != isolate->heap()->hidden_symbol()) ||
(map()->unused_property_fields() == 0 &&
TooManyFastProperties(properties()->length(), store_mode))) {
Object* obj; Object* obj;
{ MaybeObject* maybe_obj = { MaybeObject* maybe_obj =
NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
@ -1559,16 +1564,15 @@ MaybeObject* JSObject::AddFastProperty(String* name,
bool allow_map_transition = bool allow_map_transition =
(isolate->context()->global_context()->object_function()->map() != map()); (isolate->context()->global_context()->object_function()->map() != map());
ASSERT(old_descriptors->Search(name) == DescriptorArray::kNotFound);
ASSERT(index < map()->inobject_properties() || ASSERT(index < map()->inobject_properties() ||
(index - map()->inobject_properties()) < properties()->length() || (index - map()->inobject_properties()) < properties()->length() ||
map()->unused_property_fields() == 0); map()->unused_property_fields() == 0);
// Allocate a new map for the object. // Allocate a new map for the object.
Object* r; Map* new_map;
{ MaybeObject* maybe_r = map()->CopyDropDescriptors(); { MaybeObject* maybe_r = map()->CopyReplaceDescriptors(new_descriptors);
if (!maybe_r->ToObject(&r)) return maybe_r; if (!maybe_r->To(&new_map)) return maybe_r;
} }
Map* new_map = Map::cast(r);
TransitionArray* new_transitions = NULL; TransitionArray* new_transitions = NULL;
if (allow_map_transition) { if (allow_map_transition) {
@ -1579,31 +1583,24 @@ MaybeObject* JSObject::AddFastProperty(String* name,
} }
if (map()->unused_property_fields() == 0) { if (map()->unused_property_fields() == 0) {
if (TooManyFastProperties(properties()->length(), store_mode)) {
Object* obj;
{ MaybeObject* maybe_obj =
NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
if (!maybe_obj->ToObject(&obj)) return maybe_obj;
}
return AddSlowProperty(name, value, attributes);
}
// Make room for the new value // Make room for the new value
Object* values; FixedArray* values;
{ MaybeObject* maybe_values = { MaybeObject* maybe_values =
properties()->CopySize(properties()->length() + kFieldsAdded); properties()->CopySize(properties()->length() + kFieldsAdded);
if (!maybe_values->ToObject(&values)) return maybe_values; if (!maybe_values->To(&values)) return maybe_values;
} }
set_properties(FixedArray::cast(values)); set_properties(values);
new_map->set_unused_property_fields(kFieldsAdded - 1); new_map->set_unused_property_fields(kFieldsAdded - 1);
} else { } else {
new_map->set_unused_property_fields(map()->unused_property_fields() - 1); new_map->set_unused_property_fields(map()->unused_property_fields() - 1);
} }
// Apply all changes at once, so they are atomic. // Apply all changes at once, so they are atomic.
if (allow_map_transition) { if (allow_map_transition) {
MaybeObject* transition_added = map()->set_transitions(new_transitions); MaybeObject* transition_added = map()->set_transitions(new_transitions);
if (transition_added->IsFailure()) return transition_added; if (transition_added->IsFailure()) return transition_added;
} }
new_map->set_instance_descriptors(new_descriptors);
new_map->SetBackPointer(map()); new_map->SetBackPointer(map());
set_map(new_map); set_map(new_map);
return FastPropertyAtPut(index, value); return FastPropertyAtPut(index, value);
@ -1626,11 +1623,10 @@ MaybeObject* JSObject::AddConstantFunctionProperty(
// Allocate a new map for the object. // Allocate a new map for the object.
Map* new_map; Map* new_map;
{ MaybeObject* maybe_new_map = map()->CopyDropDescriptors(); { MaybeObject* maybe_new_map = map()->CopyReplaceDescriptors(new_descriptors);
if (!maybe_new_map->To(&new_map)) return maybe_new_map; if (!maybe_new_map->To(&new_map)) return maybe_new_map;
} }
new_map->set_instance_descriptors(new_descriptors);
Map* old_map = map(); Map* old_map = map();
set_map(new_map); set_map(new_map);
@ -1860,25 +1856,17 @@ MaybeObject* JSObject::ConvertDescriptorToField(String* name,
int index = map()->NextFreePropertyIndex(); int index = map()->NextFreePropertyIndex();
FieldDescriptor new_field(name, index, attributes, 0); FieldDescriptor new_field(name, index, attributes, 0);
// Make a new DescriptorArray replacing an entry with FieldDescriptor. // Make a new DescriptorArray replacing an entry with FieldDescriptor.
Object* descriptors_unchecked; DescriptorArray* new_descriptors;
{ MaybeObject* maybe_descriptors_unchecked = { MaybeObject* maybe_descriptors =
map()->instance_descriptors()->CopyInsert(&new_field); map()->instance_descriptors()->CopyInsert(&new_field);
if (!maybe_descriptors_unchecked->ToObject(&descriptors_unchecked)) { if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors;
return maybe_descriptors_unchecked;
}
} }
DescriptorArray* new_descriptors =
DescriptorArray::cast(descriptors_unchecked);
// Make a new map for the object. // Make a new map for the object.
Object* new_map_unchecked; Map* new_map;
{ MaybeObject* maybe_new_map_unchecked = map()->CopyDropDescriptors(); { MaybeObject* maybe_new_map = map()->CopyReplaceDescriptors(new_descriptors);
if (!maybe_new_map_unchecked->ToObject(&new_map_unchecked)) { if (!maybe_new_map->To(&new_map)) return maybe_new_map;
return maybe_new_map_unchecked;
}
} }
Map* new_map = Map::cast(new_map_unchecked);
new_map->set_instance_descriptors(new_descriptors);
// Make new properties array if necessary. // Make new properties array if necessary.
FixedArray* new_properties = 0; // Will always be NULL or a valid pointer. FixedArray* new_properties = 0; // Will always be NULL or a valid pointer.
@ -4612,10 +4600,9 @@ static MaybeObject* CreateFreshAccessor(JSObject* obj,
// step 3: create a new map with the new descriptors // step 3: create a new map with the new descriptors
Map* map2; Map* map2;
{ MaybeObject* maybe_map2 = map1->CopyDropDescriptors(); { MaybeObject* maybe_map2 = map1->CopyReplaceDescriptors(descriptors2);
if (!maybe_map2->To(&map2)) return maybe_map2; if (!maybe_map2->To(&map2)) return maybe_map2;
} }
map2->set_instance_descriptors(descriptors2);
// step 4: create a new getter/setter pair with a transition to the new map // step 4: create a new getter/setter pair with a transition to the new map
AccessorPair* accessors1; AccessorPair* accessors1;
@ -4649,9 +4636,9 @@ static bool TransitionToSameAccessor(Object* map,
AccessorComponent component, AccessorComponent component,
Object* accessor, Object* accessor,
PropertyAttributes attributes ) { PropertyAttributes attributes ) {
DescriptorArray* descs = Map::cast(map)->instance_descriptors(); Map* transitioned_map = Map::cast(map);
DescriptorArray* descs = transitioned_map->instance_descriptors();
int number = descs->LastAdded(); int number = descs->LastAdded();
ASSERT(number != DescriptorArray::kNotFound);
Object* target_accessor = Object* target_accessor =
AccessorPair::cast(descs->GetCallbacksObject(number))->get(component); AccessorPair::cast(descs->GetCallbacksObject(number))->get(component);
PropertyAttributes target_attributes = descs->GetDetails(number).attributes(); PropertyAttributes target_attributes = descs->GetDetails(number).attributes();
@ -4683,10 +4670,9 @@ static MaybeObject* NewCallbackTransition(JSObject* obj,
// step 3: create a new map with the new descriptors // step 3: create a new map with the new descriptors
Map* map3; Map* map3;
{ MaybeObject* maybe_map3 = map2->CopyDropDescriptors(); { MaybeObject* maybe_map3 = map2->CopyReplaceDescriptors(descriptors3);
if (!maybe_map3->To(&map3)) return maybe_map3; if (!maybe_map3->To(&map3)) return maybe_map3;
} }
map3->set_instance_descriptors(descriptors3);
// step 4: add a new transition to the new map // step 4: add a new transition to the new map
TransitionArray* new_transitions; TransitionArray* new_transitions;
@ -4909,41 +4895,18 @@ Object* JSObject::SlowReverseLookup(Object* value) {
} }
MaybeObject* Map::CopyDropDescriptors() { MaybeObject* Map::RawCopy(int instance_size) {
Heap* heap = GetHeap(); Map* result;
Object* result;
{ MaybeObject* maybe_result = { MaybeObject* maybe_result =
heap->AllocateMap(instance_type(), instance_size()); GetHeap()->AllocateMap(instance_type(), instance_size);
if (!maybe_result->ToObject(&result)) return maybe_result; if (!maybe_result->To(&result)) return maybe_result;
} }
Map::cast(result)->set_prototype(prototype());
Map::cast(result)->set_constructor(constructor());
// Please note instance_type and instance_size are set when allocated. result->set_prototype(prototype());
Map::cast(result)->set_inobject_properties(inobject_properties()); result->set_constructor(constructor());
Map::cast(result)->set_unused_property_fields(unused_property_fields()); result->set_bit_field(bit_field());
result->set_bit_field2(bit_field2());
// If the map has pre-allocated properties always start out with a descriptor result->set_bit_field3(bit_field3());
// array describing these properties.
if (pre_allocated_property_fields() > 0) {
ASSERT(constructor()->IsJSFunction());
JSFunction* ctor = JSFunction::cast(constructor());
Object* descriptors;
{ MaybeObject* maybe_descriptors =
ctor->initial_map()->instance_descriptors()->Copy(
DescriptorArray::MAY_BE_SHARED);
if (!maybe_descriptors->ToObject(&descriptors)) return maybe_descriptors;
}
Map::cast(result)->set_instance_descriptors(
DescriptorArray::cast(descriptors));
Map::cast(result)->set_pre_allocated_property_fields(
pre_allocated_property_fields());
}
Map::cast(result)->set_bit_field(bit_field());
Map::cast(result)->set_bit_field2(bit_field2());
Map::cast(result)->set_bit_field3(bit_field3());
Map::cast(result)->set_is_shared(false);
Map::cast(result)->ClearCodeCache(heap);
return result; return result;
} }
@ -4955,29 +4918,21 @@ MaybeObject* Map::CopyNormalized(PropertyNormalizationMode mode,
new_instance_size -= inobject_properties() * kPointerSize; new_instance_size -= inobject_properties() * kPointerSize;
} }
Object* result; Map* result;
{ MaybeObject* maybe_result = { MaybeObject* maybe_result = RawCopy(new_instance_size);
GetHeap()->AllocateMap(instance_type(), new_instance_size); if (!maybe_result->To(&result)) return maybe_result;
if (!maybe_result->ToObject(&result)) return maybe_result;
} }
if (mode != CLEAR_INOBJECT_PROPERTIES) { if (mode != CLEAR_INOBJECT_PROPERTIES) {
Map::cast(result)->set_inobject_properties(inobject_properties()); result->set_inobject_properties(inobject_properties());
} }
Map::cast(result)->set_prototype(prototype()); result->set_code_cache(code_cache());
Map::cast(result)->set_constructor(constructor()); result->set_is_shared(sharing == SHARED_NORMALIZED_MAP);
Map::cast(result)->set_bit_field(bit_field());
Map::cast(result)->set_bit_field2(bit_field2());
Map::cast(result)->set_bit_field3(bit_field3());
Map::cast(result)->set_code_cache(code_cache());
Map::cast(result)->set_is_shared(sharing == SHARED_NORMALIZED_MAP);
#ifdef DEBUG #ifdef DEBUG
if (FLAG_verify_heap && Map::cast(result)->is_shared()) { if (FLAG_verify_heap && Map::cast(result)->is_shared()) {
Map::cast(result)->SharedMapVerify(); result->SharedMapVerify();
} }
#endif #endif
@ -4985,19 +4940,56 @@ MaybeObject* Map::CopyNormalized(PropertyNormalizationMode mode,
} }
MaybeObject* Map::CopyDropTransitions( MaybeObject* Map::CopyDropDescriptors() {
DescriptorArray::SharedMode shared_mode) { Map* result;
Object* new_map; { MaybeObject* maybe_result = RawCopy(instance_size());
{ MaybeObject* maybe_new_map = CopyDropDescriptors(); if (!maybe_result->To(&result)) return maybe_result;
if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map;
} }
Object* descriptors;
// Please note instance_type and instance_size are set when allocated.
result->set_inobject_properties(inobject_properties());
result->set_unused_property_fields(unused_property_fields());
result->set_pre_allocated_property_fields(pre_allocated_property_fields());
result->set_is_shared(false);
result->ClearCodeCache(GetHeap());
return result;
}
MaybeObject* Map::CopyReplaceDescriptors(DescriptorArray* descriptors) {
Map* result;
{ MaybeObject* maybe_result = CopyDropDescriptors();
if (!maybe_result->To(&result)) return maybe_result;
}
result->set_instance_descriptors(descriptors);
return result;
}
MaybeObject* Map::CopyWithPreallocatedFieldDescriptors() {
if (pre_allocated_property_fields() == 0) return CopyDropDescriptors();
// If the map has pre-allocated properties always start out with a descriptor
// array describing these properties.
ASSERT(constructor()->IsJSFunction());
JSFunction* ctor = JSFunction::cast(constructor());
DescriptorArray* descriptors;
{ MaybeObject* maybe_descriptors = { MaybeObject* maybe_descriptors =
instance_descriptors()->Copy(shared_mode); ctor->initial_map()->instance_descriptors()->Copy(
if (!maybe_descriptors->ToObject(&descriptors)) return maybe_descriptors; DescriptorArray::MAY_BE_SHARED);
if (!maybe_descriptors->To(&descriptors)) return maybe_descriptors;
} }
cast(new_map)->set_instance_descriptors(DescriptorArray::cast(descriptors)); return CopyReplaceDescriptors(descriptors);
return new_map; }
MaybeObject* Map::CopyDropTransitions(DescriptorArray::SharedMode shared_mode) {
DescriptorArray* descriptors;
{ MaybeObject* maybe_descriptors = instance_descriptors()->Copy(shared_mode);
if (!maybe_descriptors->To(&descriptors)) return maybe_descriptors;
}
return CopyReplaceDescriptors(descriptors);
} }
@ -5928,6 +5920,7 @@ MaybeObject* DescriptorArray::CopyInsert(Descriptor* descriptor) {
ASSERT(insertion_index < new_descriptors->number_of_descriptors()); ASSERT(insertion_index < new_descriptors->number_of_descriptors());
new_descriptors->Set(insertion_index, descriptor, witness); new_descriptors->Set(insertion_index, descriptor, witness);
if (!replacing) { if (!replacing) {
new_descriptors->SetLastAdded(insertion_index); new_descriptors->SetLastAdded(insertion_index);
} else { } else {
@ -12766,15 +12759,16 @@ MaybeObject* StringDictionary::TransformPropertiesToFastFor(
descriptors->Sort(witness); descriptors->Sort(witness);
// Allocate new map. // Allocate new map.
Object* new_map; Map* new_map;
{ MaybeObject* maybe_new_map = obj->map()->CopyDropDescriptors(); { MaybeObject* maybe_new_map =
if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; obj->map()->CopyReplaceDescriptors(descriptors);
if (!maybe_new_map->To(&new_map)) return maybe_new_map;
} }
new_map->set_unused_property_fields(unused_property_fields);
// Transform the object. // Transform the object.
obj->set_map(Map::cast(new_map)); obj->set_map(new_map);
obj->map()->set_instance_descriptors(descriptors);
obj->map()->set_unused_property_fields(unused_property_fields);
obj->set_properties(FixedArray::cast(fields)); obj->set_properties(FixedArray::cast(fields));
ASSERT(obj->IsJSObject()); ASSERT(obj->IsJSObject());

View File

@ -4910,7 +4910,11 @@ class Map: public HeapObject {
String* name, String* name,
LookupResult* result); LookupResult* result);
MUST_USE_RESULT MaybeObject* RawCopy(int instance_size);
MUST_USE_RESULT MaybeObject* CopyWithPreallocatedFieldDescriptors();
MUST_USE_RESULT MaybeObject* CopyDropDescriptors(); MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
MUST_USE_RESULT MaybeObject* CopyReplaceDescriptors(
DescriptorArray* descriptors);
MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode, MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
NormalizedMapSharingMode sharing); NormalizedMapSharingMode sharing);

View File

@ -2180,24 +2180,16 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetReadOnlyPrototype) {
static_cast<PropertyAttributes>(details.attributes() | READ_ONLY), static_cast<PropertyAttributes>(details.attributes() | READ_ONLY),
details.index()); details.index());
// Construct a new field descriptors array containing the new descriptor. // Construct a new field descriptors array containing the new descriptor.
Object* descriptors_unchecked; DescriptorArray* new_descriptors;
{ MaybeObject* maybe_descriptors_unchecked = { MaybeObject* maybe_descriptors = instance_desc->CopyInsert(&new_desc);
instance_desc->CopyInsert(&new_desc); if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors;
if (!maybe_descriptors_unchecked->ToObject(&descriptors_unchecked)) {
return maybe_descriptors_unchecked;
}
} }
DescriptorArray* new_descriptors =
DescriptorArray::cast(descriptors_unchecked);
// Create a new map featuring the new field descriptors array. // Create a new map featuring the new field descriptors array.
Map* new_map; Map* new_map;
{ MaybeObject* maybe_map_unchecked = { MaybeObject* maybe_map =
function->map()->CopyDropDescriptors(); function->map()->CopyReplaceDescriptors(new_descriptors);
if (!maybe_map_unchecked->To(&new_map)) { if (!maybe_map->To(&new_map)) return maybe_map;
return maybe_map_unchecked;
}
} }
new_map->set_instance_descriptors(new_descriptors);
function->set_map(new_map); function->set_map(new_map);
} else { // Dictionary properties. } else { // Dictionary properties.
// Directly manipulate the property details. // Directly manipulate the property details.