1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:57:44 +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

@ -273,3 +273,89 @@ test("TypedArray is abstract", () => {
new TypedArray();
}).toThrowWithMessage(TypeError, "Abstract class TypedArray cannot be constructed directly");
});
TYPED_ARRAYS.forEach(T => {
test(`all numeric indices are valid on ${T.name}`, () => {
const newTypedArray = new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
expect(newTypedArray).toHaveLength(10);
function PoisonError() {}
const poisonedObject = {
valueOf() {
throw new PoisonError();
},
extraValue: 4,
};
// valueOf should only be called if the string is a valid numeric index
expect(() => (newTypedArray["0"] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray["-0"] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray["Infinity"] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray["-Infinity"] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray["NaN"] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray["1"] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray["1.1"] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray["0.3"] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray["-1"] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray["-1.1"] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray["-0.3"] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray[0] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray[-0] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray[Infinity] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray[-Infinity] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray[NaN] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray[1] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray[1.1] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray[0.3] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray[-1] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray[-1.1] = poisonedObject)).toThrow(PoisonError);
expect(() => (newTypedArray[-0.3] = poisonedObject)).toThrow(PoisonError);
function expectValueSet(property) {
newTypedArray[property] = poisonedObject;
expect(newTypedArray).toHaveLength(10);
expect(newTypedArray[property].extraValue).toBe(4);
expect(delete newTypedArray[property]).toBeTrue();
}
expectValueSet("a");
expectValueSet(" 1");
expectValueSet("a");
expectValueSet("a");
expectValueSet(" 1");
expectValueSet("+Infinity");
expectValueSet("00");
expectValueSet("01");
expectValueSet("-01");
expectValueSet("-");
expectValueSet(".");
expectValueSet("-.");
expectValueSet("1e");
expectValueSet("1e");
expectValueSet("1e0");
expectValueSet("5.");
expectValueSet(".5");
expectValueSet("-.5");
expectValueSet("1e1");
expectValueSet("1e+1");
expectValueSet("0.0000001"); // ToString = "1e-7"
// Things that can round trip as numbers get consumed
function expectValueNotSet(property) {
expect(() => {
newTypedArray[property] = poisonedObject;
}).toThrow(PoisonError);
expect(newTypedArray[property]).toBeUndefined();
expect(delete newTypedArray[property]).toBeTrue();
}
expectValueNotSet("-2");
expectValueNotSet(1.5);
expectValueNotSet("-0");
expectValueNotSet(-1.5);
expectValueNotSet("-Infinity");
expectValueNotSet("Infinity");
expectValueNotSet("NaN");
expectValueNotSet("1e-10");
});
});