mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:17:45 +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:
parent
7c4d42279d
commit
745b998774
10 changed files with 207 additions and 96 deletions
|
@ -1031,7 +1031,7 @@ Object* create_mapped_arguments_object(GlobalObject& global_object, FunctionObje
|
|||
}
|
||||
|
||||
// 7.1.21 CanonicalNumericIndexString ( argument ), https://tc39.es/ecma262/#sec-canonicalnumericindexstring
|
||||
Value canonical_numeric_index_string(GlobalObject& global_object, PropertyKey const& property_key)
|
||||
CanonicalIndex canonical_numeric_index_string(PropertyKey const& property_key, CanonicalIndexMode mode)
|
||||
{
|
||||
// NOTE: If the property name is a number type (An implementation-defined optimized
|
||||
// property key type), it can be treated as a string property that has already been
|
||||
|
@ -1040,24 +1040,56 @@ Value canonical_numeric_index_string(GlobalObject& global_object, PropertyKey co
|
|||
VERIFY(property_key.is_string() || property_key.is_number());
|
||||
|
||||
if (property_key.is_number())
|
||||
return Value(property_key.as_number());
|
||||
return CanonicalIndex(CanonicalIndex::Type::Index, property_key.as_number());
|
||||
|
||||
if (mode != CanonicalIndexMode::DetectNumericRoundtrip)
|
||||
return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
|
||||
|
||||
// 1. Assert: Type(argument) is String.
|
||||
auto argument = Value(js_string(global_object.vm(), property_key.as_string()));
|
||||
auto& argument = property_key.as_string();
|
||||
|
||||
// 2. If argument is "-0", return -0𝔽.
|
||||
if (argument.as_string().string() == "-0")
|
||||
return Value(-0.0);
|
||||
// Handle trivial cases without a full round trip test
|
||||
// We do not need to check for argument == "0" at this point because we
|
||||
// already covered it with the is_number() == true path.
|
||||
if (argument.is_empty())
|
||||
return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
|
||||
u32 current_index = 0;
|
||||
if (argument.characters()[current_index] == '-') {
|
||||
current_index++;
|
||||
if (current_index == argument.length())
|
||||
return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
|
||||
}
|
||||
if (argument.characters()[current_index] == '0') {
|
||||
current_index++;
|
||||
if (current_index == argument.length())
|
||||
return CanonicalIndex(CanonicalIndex::Type::Numeric, 0);
|
||||
if (argument.characters()[current_index] != '.')
|
||||
return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
|
||||
current_index++;
|
||||
if (current_index == argument.length())
|
||||
return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
|
||||
}
|
||||
|
||||
// Short circuit a few common cases
|
||||
if (argument == "Infinity" || argument == "-Infinity" || argument == "NaN")
|
||||
return CanonicalIndex(CanonicalIndex::Type::Numeric, 0);
|
||||
|
||||
// Short circuit any string that doesn't start with digits
|
||||
if (char first_non_zero = argument.characters()[current_index]; first_non_zero < '0' || first_non_zero > '9')
|
||||
return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
|
||||
|
||||
// 3. Let n be ! ToNumber(argument).
|
||||
auto n = MUST(argument.to_number(global_object));
|
||||
char* endptr;
|
||||
auto n = Value(strtod(argument.characters(), &endptr));
|
||||
if (endptr != argument.characters() + argument.length())
|
||||
return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
|
||||
|
||||
// 4. If SameValue(! ToString(n), argument) is false, return undefined.
|
||||
if (!same_value(MUST(n.to_primitive_string(global_object)), argument))
|
||||
return js_undefined();
|
||||
if (n.to_string_without_side_effects() != argument)
|
||||
return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
|
||||
|
||||
// 5. Return n.
|
||||
return n;
|
||||
return CanonicalIndex(CanonicalIndex::Type::Numeric, 0);
|
||||
}
|
||||
|
||||
// 22.1.3.17.1 GetSubstitution ( matched, str, position, captures, namedCaptures, replacement ), https://tc39.es/ecma262/#sec-getsubstitution
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue