Commit 0d1e0a15 authored by franzih's avatar franzih Committed by Commit bot

[ast] Count index keys in AST not runtime.

We do not want to reserve space in the backing store for index keys.
Count index keys during creation of the BoilerplateDescription, and
substract them for the backing store size.

Correctly count index keys after encountering a property with
a computed name during object literal creation.

R=verwaest@chromium.org

BUG=v8:5625

Review-Url: https://codereview.chromium.org/2651523002
Cr-Commit-Position: refs/heads/master@{#42598}
parent 14f470ca
......@@ -520,6 +520,7 @@ void ObjectLiteral::InitDepthAndFlags() {
for (int i = 0; i < properties()->length(); i++) {
ObjectLiteral::Property* property = properties()->at(i);
if (!IsBoilerplateProperty(property)) {
DCHECK(has_seen_proto());
is_simple = false;
continue;
}
......@@ -582,9 +583,26 @@ void ObjectLiteral::InitDepthAndFlags() {
void ObjectLiteral::BuildConstantProperties(Isolate* isolate) {
if (!constant_properties_.is_null()) return;
int index_keys = 0;
for (int i = 0; i < properties()->length(); i++) {
ObjectLiteral::Property* property = properties()->at(i);
if (!IsBoilerplateProperty(property) || property->is_computed_name()) {
continue;
}
Handle<Object> key = property->key()->AsLiteral()->value();
uint32_t element_index = 0;
if (key->ToArrayIndex(&element_index) ||
(key->IsString() && String::cast(*key)->AsArrayIndex(&element_index))) {
index_keys++;
}
}
Handle<BoilerplateDescription> constant_properties =
isolate->factory()->NewBoilerplateDescription(
boilerplate_properties_, properties()->length(), has_seen_proto());
boilerplate_properties_, properties()->length() - index_keys,
has_seen_proto());
int position = 0;
for (int i = 0; i < properties()->length(); i++) {
......
......@@ -187,7 +187,7 @@ Handle<FixedArray> Factory::NewUninitializedFixedArray(int size) {
Handle<BoilerplateDescription> Factory::NewBoilerplateDescription(
int boilerplate, int all_properties, bool has_seen_proto) {
DCHECK_GE(all_properties, boilerplate);
DCHECK_GE(all_properties, 0);
int backing_store_size = all_properties - (has_seen_proto ? 1 : 0);
DCHECK_GE(backing_store_size, 0);
......
......@@ -19,15 +19,6 @@ static Handle<Map> ComputeObjectLiteralMap(
Handle<BoilerplateDescription> boilerplate_description,
bool* is_result_from_cache) {
int number_of_properties = boilerplate_description->backing_store_size();
for (int index = 0; index < boilerplate_description->size(); index++) {
Object* key = boilerplate_description->name(index);
uint32_t element_index = 0;
if (key->ToArrayIndex(&element_index)) {
// An index key does not require space in the property backing store.
number_of_properties--;
}
}
Isolate* isolate = context->GetIsolate();
return isolate->factory()->ObjectLiteralMapFromCache(
context, number_of_properties, is_result_from_cache);
......
......@@ -753,13 +753,14 @@ TEST(ObjectLiteralPropertyBackingStoreSize) {
// Avoid over-/under-allocation for computed property names.
os << "(function() {\n"
" 'use strict';\n"
" function f(x) {\n"
" var o = {\n"
" 1: 42,\n" // Do not allocate for index key.
" '2': 42,\n" // Do not allocate for index key.
" [x]: 42,\n" // Allocate for property with computed name.
" 3: 42\n" // Allocate for index key because we have seen a
// computed property.
" 3: 42,\n" // Do not allocate for index key.
" '4': 42\n" // Do not allocate for index key.
" };\n"
" return o;\n"
" }\n"
......@@ -768,7 +769,26 @@ TEST(ObjectLiteralPropertyBackingStoreSize) {
"\n"
" return f(x);\n"
"} )();";
CheckExpectedProperties(2, os);
CheckExpectedProperties(1, os);
// Conversion to index key.
os << "(function() {\n"
" function f(x) {\n"
" var o = {\n"
" 1: 42,\n" // Do not allocate for index key.
" '2': 42,\n" // Do not allocate for index key.
" [x]: 42,\n" // Allocate for property with computed name.
" 3: 42,\n" // Do not allocate for index key.
" get 12() {}\n" // Do not allocate for index key.
" };\n"
" return o;\n"
" }\n"
"\n"
" var x = 'hello'\n"
"\n"
" return f(x);\n"
"} )();";
CheckExpectedProperties(1, os);
os << "(function() {\n"
" function f() {\n"
......
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