1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:37:36 +00:00

LibJS: Get rid of unnecessary work from canonical_numeric_index_string

The spec version of canonical_numeric_index_string is absurdly complex,
and ends up converting from a string to a number, and then back again
which is both slow and also requires a few allocations and a string
compare.

Instead this patch moves away from using Values to represent canonical
a canonical index. In most cases all we need to know is whether a
PropertyKey is an integer between 0 and 2^^32-2, which we already
compute when we construct a PropertyKey so the existing is_number()
check is sufficient.

The more expensive case is handling strings containing numbers that
don't roundtrip through string conversion. In most cases these turn
into regular string properties, but for TypedArray access these
property names are not treated as normal named properties.
TypedArrays treat these numeric properties as magic indexes that are
ignored on read and are not stored (but are evaluated) on assignment.

For that reason there's now a mode flag on canonical_numeric_index_string
so that only TypedArrays take the cost of the ToString round trip test.
In order to improve the performance of this path this patch includes
some early returns to avoid conversion in cases where we can quickly
know whether a property can round trip.
This commit is contained in:
Anonymous 2022-02-14 01:17:07 -08:00 committed by Linus Groh
parent 7c4d42279d
commit 745b998774
10 changed files with 207 additions and 96 deletions

View file

@ -2599,7 +2599,7 @@ JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> @class_name@::legacy_pla
if (interface.supports_indexed_properties()) {
// ...and P is an array index, then:
get_own_property_generator.append(R"~~~(
if (IDL::is_an_array_index(global_object, property_name)) {
if (property_name.is_number()) {
// 1. Let index be the result of calling ToUint32(P).
u32 index = property_name.as_number();
@ -2874,7 +2874,7 @@ JS::ThrowCompletionOr<bool> @class_name@::internal_set(JS::PropertyKey const& pr
if (interface.indexed_property_setter.has_value()) {
// ...and P is an array index, then:
scoped_generator.append(R"~~~(
if (IDL::is_an_array_index(global_object, property_name)) {
if (property_name.is_number()) {
// 1. Invoke the indexed property setter on O with P and V.
TRY(invoke_indexed_property_setter(global_object, impl(), property_name, value));
@ -2919,14 +2919,14 @@ JS::ThrowCompletionOr<bool> @class_name@::internal_set(JS::PropertyKey const& pr
JS::ThrowCompletionOr<bool> @class_name@::internal_define_own_property(JS::PropertyKey const& property_name, JS::PropertyDescriptor const& property_descriptor)
{
[[maybe_unused]] auto& vm = this->vm();
auto& global_object = this->global_object();
[[maybe_unused]] auto& global_object = this->global_object();
)~~~");
// 1. If O supports indexed properties...
if (interface.supports_indexed_properties()) {
// ...and P is an array index, then:
scoped_generator.append(R"~~~(
if (IDL::is_an_array_index(global_object, property_name)) {
if (property_name.is_number()) {
// 1. If the result of calling IsDataDescriptor(Desc) is false, then return false.
if (!property_descriptor.is_data_descriptor())
return false;
@ -3038,14 +3038,14 @@ JS::ThrowCompletionOr<bool> @class_name@::internal_define_own_property(JS::Prope
scoped_generator.append(R"~~~(
JS::ThrowCompletionOr<bool> @class_name@::internal_delete(JS::PropertyKey const& property_name)
{
auto& global_object = this->global_object();
[[maybe_unused]] auto& global_object = this->global_object();
)~~~");
// 1. If O supports indexed properties...
if (interface.supports_indexed_properties()) {
// ...and P is an array index, then:
scoped_generator.append(R"~~~(
if (IDL::is_an_array_index(global_object, property_name)) {
if (property_name.is_number()) {
// 1. Let index be the result of calling ToUint32(P).
u32 index = property_name.as_number();
@ -3362,7 +3362,7 @@ JS::ThrowCompletionOr<JS::Object*> @constructor_class@::construct(FunctionObject
generator.append(R"~~~(
[[maybe_unused]] auto& vm = this->vm();
auto& global_object = this->global_object();
[[maybe_unused]] auto& global_object = this->global_object();
auto& window = static_cast<WindowObject&>(global_object);
)~~~");