LibJS+LibWeb: Return Vector<PropertyKey> from internal_own_property_keys

By doing that we avoid lots of `PropertyKey` -> `Value` -> `PropertyKey`
transforms, which are quite expensive because of underlying
`FlyString` -> `PrimitiveString` -> `FlyString` conversions.

10% improvement on MicroBench/object-keys.js
This commit is contained in:
Aliaksandr Kalenik 2025-05-14 15:39:48 +03:00 committed by Alexander Kalenik
parent 47569c1714
commit 5ee810f772
Notes: github-actions[bot] 2025-05-15 18:13:23 +00:00
24 changed files with 134 additions and 155 deletions

View file

@ -128,12 +128,10 @@ ThrowCompletionOr<bool> StringObject::internal_define_own_property(PropertyKey c
}
// 10.4.3.3 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-string-exotic-objects-ownpropertykeys
ThrowCompletionOr<GC::RootVector<Value>> StringObject::internal_own_property_keys() const
ThrowCompletionOr<Vector<PropertyKey>> StringObject::internal_own_property_keys() const
{
auto& vm = this->vm();
// 1. Let keys be a new empty List.
auto keys = GC::RootVector<Value> { heap() };
Vector<PropertyKey> keys;
// 2. Let str be O.[[StringData]].
// 3. Assert: str is a String.
@ -143,30 +141,30 @@ ThrowCompletionOr<GC::RootVector<Value>> StringObject::internal_own_property_key
// 5. For each integer i starting with 0 such that i < len, in ascending order, do
for (size_t i = 0; i < length; ++i) {
// a. Add ! ToString(𝔽(i)) as the last element of keys.
keys.append(PrimitiveString::create(vm, String::number(i)));
keys.append(String::number(i));
}
// 6. For each own property key P of O such that P is an array index and ! ToIntegerOrInfinity(P) ≥ len, in ascending numeric index order, do
for (auto& entry : indexed_properties()) {
for (auto const& entry : indexed_properties()) {
if (entry.index() >= length) {
// a. Add P as the last element of keys.
keys.append(PrimitiveString::create(vm, String::number(entry.index())));
keys.append(String::number(entry.index()));
}
}
// 7. For each own property key P of O such that P is a String and P is not an array index, in ascending chronological order of property creation, do
for (auto& it : shape().property_table()) {
for (auto const& it : shape().property_table()) {
if (it.key.is_string()) {
// a. Add P as the last element of keys.
keys.append(it.key.to_value(vm));
keys.append(it.key);
}
}
// 8. For each own property key P of O such that P is a Symbol, in ascending chronological order of property creation, do
for (auto& it : shape().property_table()) {
for (auto const& it : shape().property_table()) {
if (it.key.is_symbol()) {
// a. Add P as the last element of keys.
keys.append(it.key.to_value(vm));
keys.append(it.key);
}
}