Bug 1453922: Add fast path for non-negative int32 values to ToIndex. r=jandem
authorAndré Bargull <andre.bargull@gmail.com>
Thu, 19 Apr 2018 10:46:49 +0200
changeset 414456 363cc3b4dd41a2c5469fb03d72451e942687c73c
parent 414455 0ab0d909476f55bd58181485889f0a31d032b308
child 414457 ff7588bba148f3f6f0bed0a88e5c94dcb102f3db
push id102333
push usernerli@mozilla.com
push dateThu, 19 Apr 2018 10:59:52 +0000
treeherdermozilla-inbound@363cc3b4dd41 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjandem
bugs1453922
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1453922: Add fast path for non-negative int32 values to ToIndex. r=jandem
js/src/jsnum.cpp
js/src/jsnum.h
--- a/js/src/jsnum.cpp
+++ b/js/src/jsnum.cpp
@@ -1738,18 +1738,20 @@ js::ToUint16Slow(JSContext* cx, const Ha
         return false;
     }
     *out = ToUint16(d);
     return true;
 }
 
 // ES2017 draft 7.1.17 ToIndex
 bool
-js::ToIndex(JSContext* cx, JS::HandleValue v, const unsigned errorNumber, uint64_t* index)
+js::ToIndexSlow(JSContext* cx, JS::HandleValue v, const unsigned errorNumber, uint64_t* index)
 {
+    MOZ_ASSERT_IF(v.isInt32(), v.toInt32() < 0);
+
     // Step 1.
     if (v.isUndefined()) {
         *index = 0;
         return true;
     }
 
     // Step 2.a.
     double integerIndex;
--- a/js/src/jsnum.h
+++ b/js/src/jsnum.h
@@ -266,18 +266,31 @@ ToInteger(JSContext* cx, HandleValue v, 
 
 /* ES2017 draft 7.1.17 ToIndex
  *
  * Return true and set |*index| to the integer value if |v| is a valid
  * integer index value. Otherwise report a RangeError and return false.
  *
  * The returned index will always be in the range 0 <= *index <= 2^53-1.
  */
-MOZ_MUST_USE bool
-ToIndex(JSContext* cx, JS::HandleValue v, const unsigned errorNumber, uint64_t* index);
+extern MOZ_MUST_USE bool
+ToIndexSlow(JSContext* cx, JS::HandleValue v, const unsigned errorNumber, uint64_t* index);
+
+static MOZ_MUST_USE inline bool
+ToIndex(JSContext* cx, JS::HandleValue v, const unsigned errorNumber, uint64_t* index)
+{
+    if (v.isInt32()) {
+        int32_t i = v.toInt32();
+        if (i >= 0) {
+            *index = uint64_t(i);
+            return true;
+        }
+    }
+    return ToIndexSlow(cx, v, errorNumber, index);
+}
 
 static MOZ_MUST_USE inline bool
 ToIndex(JSContext* cx, JS::HandleValue v, uint64_t* index)
 {
     return ToIndex(cx, v, JSMSG_BAD_INDEX, index);
 }
 
 MOZ_MUST_USE inline bool