<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[211128] trunk</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/211128">211128</a></dd>
<dt>Author</dt> <dd>utatane.tea@gmail.com</dd>
<dt>Date</dt> <dd>2017-01-24 18:40:52 -0800 (Tue, 24 Jan 2017)</dd>
</dl>
<h3>Log Message</h3>
<pre>[JSC] Optimize Number#toString with Int52
https://bugs.webkit.org/show_bug.cgi?id=167303
Reviewed by Sam Weinig.
JSTests:
* stress/to-string-with-int52.js: Added.
(shouldBe):
Source/JavaScriptCore:
In kraken crypto-sha256-iterative, we frequently call Number.prototype.toString with
Int52. In that case, toString handles it in the generic double path. But we should
have a fast path for this since it can be represented in int64_t.
The stanford-crypto-sha256-iterative shows 1.6% performance improvement (on Linux machine hanayamata).
Collected 100 samples per benchmark/VM, with 100 VM invocations per benchmark. Emitted a call to gc() between
sample measurements. Used 1 benchmark iteration per VM invocation for warm-up. Used the jsc-specific preciseTime()
function to get microsecond-level timing. Reporting benchmark execution times with 95% confidence intervals in
milliseconds.
baseline patched
stanford-crypto-sha256-iterative 32.853+-0.075 ^ 32.325+-0.055 ^ definitely 1.0163x faster
* runtime/JSCJSValue.h:
* runtime/NumberPrototype.cpp:
(JSC::int52ToStringWithRadix):
(JSC::toStringWithRadix):</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkJSTestsChangeLog">trunk/JSTests/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSCJSValueh">trunk/Source/JavaScriptCore/runtime/JSCJSValue.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeNumberPrototypecpp">trunk/Source/JavaScriptCore/runtime/NumberPrototype.cpp</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunkJSTestsstresstostringwithint52js">trunk/JSTests/stress/to-string-with-int52.js</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkJSTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/ChangeLog (211127 => 211128)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/ChangeLog        2017-01-25 02:18:01 UTC (rev 211127)
+++ trunk/JSTests/ChangeLog        2017-01-25 02:40:52 UTC (rev 211128)
</span><span class="lines">@@ -1,3 +1,13 @@
</span><ins>+2017-01-24 Yusuke Suzuki <utatane.tea@gmail.com>
+
+ [JSC] Optimize Number#toString with Int52
+ https://bugs.webkit.org/show_bug.cgi?id=167303
+
+ Reviewed by Sam Weinig.
+
+ * stress/to-string-with-int52.js: Added.
+ (shouldBe):
+
</ins><span class="cx"> 2017-01-24 Filip Pizlo <fpizlo@apple.com>
</span><span class="cx">
</span><span class="cx"> Atomics.store should return the int-converted value, not the value that it stored
</span></span></pre></div>
<a id="trunkJSTestsstresstostringwithint52js"></a>
<div class="addfile"><h4>Added: trunk/JSTests/stress/to-string-with-int52.js (0 => 211128)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/stress/to-string-with-int52.js         (rev 0)
+++ trunk/JSTests/stress/to-string-with-int52.js        2017-01-25 02:40:52 UTC (rev 211128)
</span><span class="lines">@@ -0,0 +1,15 @@
</span><ins>+function shouldBe(actual, expected)
+{
+ if (actual !== expected)
+ throw new Error('bad value: ' + actual);
+}
+
+shouldBe((0xfffffffffff).toString(16), `fffffffffff`);
+shouldBe((-0xfffffffffff).toString(16), `-fffffffffff`);
+shouldBe((0xfffffffffff000).toString(16), `fffffffffff000`);
+shouldBe((-0xfffffffffff000).toString(16), `-fffffffffff000`);
+
+shouldBe((0x8000000000000).toString(16), `8000000000000`);
+shouldBe((-0x8000000000000).toString(16), `-8000000000000`);
+shouldBe((0x8000000000000 - 1).toString(16), `7ffffffffffff`);
+shouldBe((-0x8000000000000 + 1).toString(16), `-7ffffffffffff`);
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (211127 => 211128)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2017-01-25 02:18:01 UTC (rev 211127)
+++ trunk/Source/JavaScriptCore/ChangeLog        2017-01-25 02:40:52 UTC (rev 211128)
</span><span class="lines">@@ -1,3 +1,30 @@
</span><ins>+2017-01-24 Yusuke Suzuki <utatane.tea@gmail.com>
+
+ [JSC] Optimize Number#toString with Int52
+ https://bugs.webkit.org/show_bug.cgi?id=167303
+
+ Reviewed by Sam Weinig.
+
+ In kraken crypto-sha256-iterative, we frequently call Number.prototype.toString with
+ Int52. In that case, toString handles it in the generic double path. But we should
+ have a fast path for this since it can be represented in int64_t.
+
+ The stanford-crypto-sha256-iterative shows 1.6% performance improvement (on Linux machine hanayamata).
+
+ Collected 100 samples per benchmark/VM, with 100 VM invocations per benchmark. Emitted a call to gc() between
+ sample measurements. Used 1 benchmark iteration per VM invocation for warm-up. Used the jsc-specific preciseTime()
+ function to get microsecond-level timing. Reporting benchmark execution times with 95% confidence intervals in
+ milliseconds.
+
+ baseline patched
+
+ stanford-crypto-sha256-iterative 32.853+-0.075 ^ 32.325+-0.055 ^ definitely 1.0163x faster
+
+ * runtime/JSCJSValue.h:
+ * runtime/NumberPrototype.cpp:
+ (JSC::int52ToStringWithRadix):
+ (JSC::toStringWithRadix):
+
</ins><span class="cx"> 2017-01-24 Michael Saboff <msaboff@apple.com>
</span><span class="cx">
</span><span class="cx"> InferredTypeTable entry manipulation is not TOCTOU race safe
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSCJSValueh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSCJSValue.h (211127 => 211128)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSCJSValue.h        2017-01-25 02:18:01 UTC (rev 211127)
+++ trunk/Source/JavaScriptCore/runtime/JSCJSValue.h        2017-01-25 02:40:52 UTC (rev 211128)
</span><span class="lines">@@ -316,9 +316,9 @@
</span><span class="cx">
</span><span class="cx"> // Constants used for Int52. Int52 isn't part of JSValue right now, but JSValues may be
</span><span class="cx"> // converted to Int52s and back again.
</span><del>- static const unsigned numberOfInt52Bits = 52;
- static const int64_t notInt52 = static_cast<int64_t>(1) << numberOfInt52Bits;
- static const unsigned int52ShiftAmount = 12;
</del><ins>+ static constexpr const unsigned numberOfInt52Bits = 52;
+ static constexpr const int64_t notInt52 = static_cast<int64_t>(1) << numberOfInt52Bits;
+ static constexpr const unsigned int52ShiftAmount = 12;
</ins><span class="cx">
</span><span class="cx"> static ptrdiff_t offsetOfPayload() { return OBJECT_OFFSETOF(JSValue, u.asBits.payload); }
</span><span class="cx"> static ptrdiff_t offsetOfTag() { return OBJECT_OFFSETOF(JSValue, u.asBits.tag); }
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeNumberPrototypecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/NumberPrototype.cpp (211127 => 211128)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/NumberPrototype.cpp        2017-01-25 02:18:01 UTC (rev 211127)
+++ trunk/Source/JavaScriptCore/runtime/NumberPrototype.cpp        2017-01-25 02:40:52 UTC (rev 211128)
</span><span class="lines">@@ -144,9 +144,30 @@
</span><span class="cx"> // Mapping from integers 0..35 to digit identifying this value, for radix 2..36.
</span><span class="cx"> static const char radixDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
</span><span class="cx">
</span><del>-static char* toStringWithRadix(RadixBuffer& buffer, double number, unsigned radix)
</del><ins>+static char* int52ToStringWithRadix(char* startOfResultString, int64_t int52Value, unsigned radix)
</ins><span class="cx"> {
</span><del>- ASSERT(std::isfinite(number));
</del><ins>+ bool negative = false;
+ uint64_t positiveNumber = int52Value;
+ if (int52Value < 0) {
+ negative = true;
+ positiveNumber = -int52Value;
+ }
+
+ do {
+ uint64_t index = positiveNumber % radix;
+ ASSERT(index < sizeof(radixDigits));
+ *--startOfResultString = radixDigits[index];
+ positiveNumber /= radix;
+ } while (positiveNumber);
+ if (negative)
+ *--startOfResultString = '-';
+
+ return startOfResultString;
+}
+
+static char* toStringWithRadix(RadixBuffer& buffer, double originalNumber, unsigned radix)
+{
+ ASSERT(std::isfinite(originalNumber));
</ins><span class="cx"> ASSERT(radix >= 2 && radix <= 36);
</span><span class="cx">
</span><span class="cx"> // Position the decimal point at the center of the string, set
</span><span class="lines">@@ -155,32 +176,38 @@
</span><span class="cx"> char* startOfResultString = decimalPoint;
</span><span class="cx">
</span><span class="cx"> // Extract the sign.
</span><del>- bool isNegative = number < 0;
- if (std::signbit(number))
- number = -number;
</del><ins>+ bool isNegative = originalNumber < 0;
+ double number = originalNumber;
+ if (std::signbit(originalNumber))
+ number = -originalNumber;
</ins><span class="cx"> double integerPart = floor(number);
</span><span class="cx">
</span><del>- // We use this to test for odd values in odd radix bases.
- // Where the base is even, (e.g. 10), to determine whether a value is even we need only
- // consider the least significant digit. For example, 124 in base 10 is even, because '4'
- // is even. if the radix is odd, then the radix raised to an integer power is also odd.
- // E.g. in base 5, 124 represents (1 * 125 + 2 * 25 + 4 * 5). Since each digit in the value
- // is multiplied by an odd number, the result is even if the sum of all digits is even.
- //
- // For the integer portion of the result, we only need test whether the integer value is
- // even or odd. For each digit of the fraction added, we should invert our idea of whether
- // the number is odd if the new digit is odd.
- //
- // Also initialize digit to this value; for even radix values we only need track whether
- // the last individual digit was odd.
- bool integerPartIsOdd = integerPart <= static_cast<double>(0x1FFFFFFFFFFFFFull) && static_cast<int64_t>(integerPart) & 1;
- ASSERT(integerPartIsOdd == static_cast<bool>(fmod(integerPart, 2)));
- bool isOddInOddRadix = integerPartIsOdd;
- uint32_t digit = integerPartIsOdd;
-
</del><span class="cx"> // Check if the value has a fractional part to convert.
</span><span class="cx"> double fractionPart = number - integerPart;
</span><del>- if (fractionPart) {
</del><ins>+ if (!fractionPart) {
+ *decimalPoint = '\0';
+ // We do not need to care the negative zero (-0) since it is also converted to "0" in all the radix.
+ if (integerPart < (static_cast<int64_t>(1) << (JSValue::numberOfInt52Bits - 1)))
+ return int52ToStringWithRadix(startOfResultString, static_cast<int64_t>(originalNumber), radix);
+ } else {
+ // We use this to test for odd values in odd radix bases.
+ // Where the base is even, (e.g. 10), to determine whether a value is even we need only
+ // consider the least significant digit. For example, 124 in base 10 is even, because '4'
+ // is even. if the radix is odd, then the radix raised to an integer power is also odd.
+ // E.g. in base 5, 124 represents (1 * 125 + 2 * 25 + 4 * 5). Since each digit in the value
+ // is multiplied by an odd number, the result is even if the sum of all digits is even.
+ //
+ // For the integer portion of the result, we only need test whether the integer value is
+ // even or odd. For each digit of the fraction added, we should invert our idea of whether
+ // the number is odd if the new digit is odd.
+ //
+ // Also initialize digit to this value; for even radix values we only need track whether
+ // the last individual digit was odd.
+ bool integerPartIsOdd = integerPart <= static_cast<double>(0x1FFFFFFFFFFFFFull) && static_cast<int64_t>(integerPart) & 1;
+ ASSERT(integerPartIsOdd == static_cast<bool>(fmod(integerPart, 2)));
+ bool isOddInOddRadix = integerPartIsOdd;
+ uint32_t digit = integerPartIsOdd;
+
</ins><span class="cx"> // Write the decimal point now.
</span><span class="cx"> *decimalPoint = '.';
</span><span class="cx">
</span><span class="lines">@@ -310,8 +337,7 @@
</span><span class="cx">
</span><span class="cx"> *endOfResultString = '\0';
</span><span class="cx"> ASSERT(endOfResultString < buffer + sizeof(buffer));
</span><del>- } else
- *decimalPoint = '\0';
</del><ins>+ }
</ins><span class="cx">
</span><span class="cx"> BigInteger units(integerPart);
</span><span class="cx">
</span><span class="lines">@@ -321,7 +347,7 @@
</span><span class="cx">
</span><span class="cx"> // Read a single digit and write it to the front of the string.
</span><span class="cx"> // Divide by radix to remove one digit from the value.
</span><del>- digit = units.divide(radix);
</del><ins>+ uint32_t digit = units.divide(radix);
</ins><span class="cx"> *--startOfResultString = radixDigits[digit];
</span><span class="cx"> } while (!!units);
</span><span class="cx">
</span></span></pre>
</div>
</div>
</body>
</html>