LibJS: Replace some use of ByteString with String

1.19x speedup on MicroBench/for-in-indexed-properties.js
This commit is contained in:
Andreas Kling 2025-05-02 14:16:21 +02:00 committed by Andreas Kling
parent 2ef2e75cdc
commit 0ef6444824
Notes: github-actions[bot] 2025-05-03 06:08:59 +00:00
3 changed files with 29 additions and 29 deletions

View file

@ -104,7 +104,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_exponential)
// 7. Let s be the empty String. // 7. Let s be the empty String.
auto sign = ""sv; auto sign = ""sv;
ByteString number_string; String number_string;
int exponent = 0; int exponent = 0;
// 8. If x < 0, then // 8. If x < 0, then
@ -119,7 +119,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_exponential)
// 9. If x = 0, then // 9. If x = 0, then
if (number == 0) { if (number == 0) {
// a. Let m be the String value consisting of f + 1 occurrences of the code unit 0x0030 (DIGIT ZERO). // a. Let m be the String value consisting of f + 1 occurrences of the code unit 0x0030 (DIGIT ZERO).
number_string = ByteString::repeated('0', fraction_digits + 1); number_string = MUST(String::repeated('0', fraction_digits + 1));
// b. Let e be 0. // b. Let e be 0.
exponent = 0; exponent = 0;
@ -147,23 +147,23 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_exponential)
number = round(number / pow(10, exponent - fraction_digits)); number = round(number / pow(10, exponent - fraction_digits));
// c. Let m be the String value consisting of the digits of the decimal representation of n (in order, with no leading zeroes). // c. Let m be the String value consisting of the digits of the decimal representation of n (in order, with no leading zeroes).
number_string = number_to_byte_string(number, NumberToStringMode::WithoutExponent); number_string = number_to_string(number, NumberToStringMode::WithoutExponent);
} }
// 11. If f ≠ 0, then // 11. If f ≠ 0, then
if (fraction_digits != 0) { if (fraction_digits != 0) {
// a. Let a be the first code unit of m. // a. Let a be the first code unit of m.
auto first = number_string.substring_view(0, 1); auto first = number_string.bytes_as_string_view().substring_view(0, 1);
// b. Let b be the other f code units of m. // b. Let b be the other f code units of m.
auto second = number_string.substring_view(1); auto second = number_string.bytes_as_string_view().substring_view(1);
// c. Set m to the string-concatenation of a, ".", and b. // c. Set m to the string-concatenation of a, ".", and b.
number_string = ByteString::formatted("{}.{}", first, second); number_string = MUST(String::formatted("{}.{}", first, second));
} }
char exponent_sign = 0; char exponent_sign = 0;
ByteString exponent_string; String exponent_string;
// 12. If e = 0, then // 12. If e = 0, then
if (exponent == 0) { if (exponent == 0) {
@ -171,7 +171,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_exponential)
exponent_sign = '+'; exponent_sign = '+';
// b. Let d be "0". // b. Let d be "0".
exponent_string = "0"sv; exponent_string = "0"_string;
} }
// 13. Else, // 13. Else,
else { else {
@ -192,12 +192,12 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_exponential)
} }
// c. Let d be the String value consisting of the digits of the decimal representation of e (in order, with no leading zeroes). // c. Let d be the String value consisting of the digits of the decimal representation of e (in order, with no leading zeroes).
exponent_string = ByteString::number(exponent); exponent_string = String::number(exponent);
} }
// 14. Set m to the string-concatenation of m, "e", c, and d. // 14. Set m to the string-concatenation of m, "e", c, and d.
// 15. Return the string-concatenation of s and m. // 15. Return the string-concatenation of s and m.
return PrimitiveString::create(vm, ByteString::formatted("{}{}e{}{}", sign, number_string, exponent_sign, exponent_string)); return PrimitiveString::create(vm, MUST(String::formatted("{}{}e{}{}", sign, number_string, exponent_sign, exponent_string)));
} }
// 21.1.3.3 Number.prototype.toFixed ( fractionDigits ), https://tc39.es/ecma262/#sec-number.prototype.tofixed // 21.1.3.3 Number.prototype.toFixed ( fractionDigits ), https://tc39.es/ecma262/#sec-number.prototype.tofixed
@ -307,7 +307,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_precision)
// 7. Let s be the empty String. // 7. Let s be the empty String.
auto sign = ""sv; auto sign = ""sv;
ByteString number_string; String number_string;
int exponent = 0; int exponent = 0;
// 8. If x < 0, then // 8. If x < 0, then
@ -322,7 +322,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_precision)
// 9. If x = 0, then // 9. If x = 0, then
if (number == 0) { if (number == 0) {
// a. Let m be the String value consisting of p occurrences of the code unit 0x0030 (DIGIT ZERO). // a. Let m be the String value consisting of p occurrences of the code unit 0x0030 (DIGIT ZERO).
number_string = ByteString::repeated('0', precision); number_string = MUST(String::repeated('0', precision));
// b. Let e be 0. // b. Let e be 0.
exponent = 0; exponent = 0;
@ -335,7 +335,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_precision)
number = round(number / pow(10, exponent - precision + 1)); number = round(number / pow(10, exponent - precision + 1));
// b. Let m be the String value consisting of the digits of the decimal representation of n (in order, with no leading zeroes). // b. Let m be the String value consisting of the digits of the decimal representation of n (in order, with no leading zeroes).
number_string = number_to_byte_string(number, NumberToStringMode::WithoutExponent); number_string = number_to_string(number, NumberToStringMode::WithoutExponent);
// c. If e < -6 or e ≥ p, then // c. If e < -6 or e ≥ p, then
if ((exponent < -6) || (exponent >= precision)) { if ((exponent < -6) || (exponent >= precision)) {
@ -345,13 +345,13 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_precision)
// ii. If p ≠ 1, then // ii. If p ≠ 1, then
if (precision != 1) { if (precision != 1) {
// 1. Let a be the first code unit of m. // 1. Let a be the first code unit of m.
auto first = number_string.substring_view(0, 1); auto first = number_string.bytes_as_string_view().substring_view(0, 1);
// 2. Let b be the other p - 1 code units of m. // 2. Let b be the other p - 1 code units of m.
auto second = number_string.substring_view(1); auto second = number_string.bytes_as_string_view().substring_view(1);
// 3. Set m to the string-concatenation of a, ".", and b. // 3. Set m to the string-concatenation of a, ".", and b.
number_string = ByteString::formatted("{}.{}", first, second); number_string = MUST(String::formatted("{}.{}", first, second));
} }
char exponent_sign = 0; char exponent_sign = 0;
@ -374,36 +374,36 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_precision)
} }
// v. Let d be the String value consisting of the digits of the decimal representation of e (in order, with no leading zeroes). // v. Let d be the String value consisting of the digits of the decimal representation of e (in order, with no leading zeroes).
auto exponent_string = ByteString::number(exponent); auto exponent_string = String::number(exponent);
// vi. Return the string-concatenation of s, m, the code unit 0x0065 (LATIN SMALL LETTER E), c, and d. // vi. Return the string-concatenation of s, m, the code unit 0x0065 (LATIN SMALL LETTER E), c, and d.
return PrimitiveString::create(vm, ByteString::formatted("{}{}e{}{}", sign, number_string, exponent_sign, exponent_string)); return PrimitiveString::create(vm, MUST(String::formatted("{}{}e{}{}", sign, number_string, exponent_sign, exponent_string)));
} }
} }
// 11. If e = p - 1, return the string-concatenation of s and m. // 11. If e = p - 1, return the string-concatenation of s and m.
if (exponent == precision - 1) if (exponent == precision - 1)
return PrimitiveString::create(vm, ByteString::formatted("{}{}", sign, number_string)); return PrimitiveString::create(vm, MUST(String::formatted("{}{}", sign, number_string)));
// 12. If e ≥ 0, then // 12. If e ≥ 0, then
if (exponent >= 0) { if (exponent >= 0) {
// a. Set m to the string-concatenation of the first e + 1 code units of m, the code unit 0x002E (FULL STOP), and the remaining p - (e + 1) code units of m. // a. Set m to the string-concatenation of the first e + 1 code units of m, the code unit 0x002E (FULL STOP), and the remaining p - (e + 1) code units of m.
number_string = ByteString::formatted( number_string = MUST(String::formatted(
"{}.{}", "{}.{}",
number_string.substring_view(0, exponent + 1), number_string.bytes_as_string_view().substring_view(0, exponent + 1),
number_string.substring_view(exponent + 1)); number_string.bytes_as_string_view().substring_view(exponent + 1)));
} }
// 13. Else, // 13. Else,
else { else {
// a. Set m to the string-concatenation of the code unit 0x0030 (DIGIT ZERO), the code unit 0x002E (FULL STOP), -(e + 1) occurrences of the code unit 0x0030 (DIGIT ZERO), and the String m. // a. Set m to the string-concatenation of the code unit 0x0030 (DIGIT ZERO), the code unit 0x002E (FULL STOP), -(e + 1) occurrences of the code unit 0x0030 (DIGIT ZERO), and the String m.
number_string = ByteString::formatted( number_string = MUST(String::formatted(
"0.{}{}", "0.{}{}",
ByteString::repeated('0', -1 * (exponent + 1)), MUST(String::repeated('0', -1 * (exponent + 1))),
number_string); number_string));
} }
// 14. Return the string-concatenation of s and m. // 14. Return the string-concatenation of s and m.
return PrimitiveString::create(vm, ByteString::formatted("{}{}", sign, number_string)); return PrimitiveString::create(vm, MUST(String::formatted("{}{}", sign, number_string)));
} }
// 21.1.3.6 Number.prototype.toString ( [ radix ] ), https://tc39.es/ecma262/#sec-number.prototype.tostring // 21.1.3.6 Number.prototype.toString ( [ radix ] ), https://tc39.es/ecma262/#sec-number.prototype.tostring
@ -486,7 +486,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
characters.take_last(); characters.take_last();
} }
return PrimitiveString::create(vm, ByteString(characters.data(), characters.size())); return PrimitiveString::create(vm, String::from_utf8_without_validation(ReadonlyBytes { characters.data(), characters.size() }));
} }
// 21.1.3.7 Number.prototype.valueOf ( ), https://tc39.es/ecma262/#sec-number.prototype.valueof // 21.1.3.7 Number.prototype.valueOf ( ), https://tc39.es/ecma262/#sec-number.prototype.valueof

View file

@ -1095,7 +1095,7 @@ ThrowCompletionOr<GC::RootVector<Value>> Object::internal_own_property_keys() co
// 2. For each own property key P of O such that P is an array index, in ascending numeric index order, do // 2. For each own property key P of O such that P is an array index, in ascending numeric index order, do
for (auto& entry : m_indexed_properties) { for (auto& entry : m_indexed_properties) {
// a. Add P as the last element of keys. // a. Add P as the last element of keys.
keys.append(PrimitiveString::create(vm, ByteString::number(entry.index()))); keys.append(PrimitiveString::create(vm, String::number(entry.index())));
} }
// 3. For each own property key P of O such that Type(P) is String and P is not an array index, in ascending chronological order of property creation, do // 3. For each own property key P of O such that Type(P) is String and P is not an array index, in ascending chronological order of property creation, do

View file

@ -57,7 +57,7 @@ static Value property_key_to_value(VM& vm, PropertyKey const& property_key)
return PrimitiveString::create(vm, property_key.as_string()); return PrimitiveString::create(vm, property_key.as_string());
VERIFY(property_key.is_number()); VERIFY(property_key.is_number());
return PrimitiveString::create(vm, ByteString::number(property_key.as_number())); return PrimitiveString::create(vm, String::number(property_key.as_number()));
} }
// 10.5.1 [[GetPrototypeOf]] ( ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof // 10.5.1 [[GetPrototypeOf]] ( ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof