From 7b6f95072a2158d3f1b6b7c763713ecb88a7ccbe Mon Sep 17 00:00:00 2001 From: Yusuke Suzuki Date: Fri, 14 Jul 2023 19:38:55 -0700 Subject: [PATCH] [JSC] Early return from isCanonicalNumericIndexString when it is definitely not a number https://bugs.webkit.org/show_bug.cgi?id=259230 rdar://112292849 Reviewed by Alexey Shvayka. This patch makes isCanonicalNumericIndexString early return for obvious non number cases so that we avoid super costly number-to-string & string-to-number path. * Source/JavaScriptCore/runtime/PropertyName.h: (JSC::isCanonicalNumericIndexString): Canonical link: https://commits.webkit.org/266080@main --- Source/JavaScriptCore/runtime/PropertyName.h | 23 ++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Source/JavaScriptCore/runtime/PropertyName.h b/Source/JavaScriptCore/runtime/PropertyName.h index 90ba29cddb71..a8a2041e1454 100644 --- a/Source/JavaScriptCore/runtime/PropertyName.h +++ b/Source/JavaScriptCore/runtime/PropertyName.h @@ -123,8 +123,27 @@ ALWAYS_INLINE bool isCanonicalNumericIndexString(UniquedStringImpl* propertyName return false; if (propertyName->isSymbol()) return false; - if (equal(propertyName, "-0"_s)) - return true; + + StringView view(propertyName); + unsigned length = view.length(); + if (!length) + return false; + UChar first = view[0]; + if (length == 1) + return isASCIIDigit(first); + UChar second = view[1]; + if (first == '-') { + // -Infinity case should go to the slow path. -NaN cannot exist since it becomes NaN. + if (!isASCIIDigit(second) && (length != strlen("-Infinity") || second != 'I')) + return false; + if (length == 2) // Including -0, and it should be accepted. + return true; + } else if (!isASCIIDigit(first)) { + // Infinity and NaN should go to the slow path. + if (!(length == strlen("Infinity") || first == 'I') && !(length == strlen("NaN") || first == 'N')) + return false; + } + double index = jsToNumber(propertyName); NumberToStringBuffer buffer; const char* indexString = WTF::numberToString(index, buffer);