Commit 4513e077 authored by yangguo's avatar yangguo Committed by Commit bot

Reland of Allow Math.random to be called when creating a custom startup...

Reland of Allow Math.random to be called when creating a custom startup snapshot. (patchset #1 id:1 of https://codereview.chromium.org/1798863003/ )

Reason for revert:
This seems not to change performance.

Original issue's description:
> Revert of Allow Math.random to be called when creating a custom startup snapshot. (patchset #2 id:20001 of https://codereview.chromium.org/1780173002/ )
>
> Reason for revert:
> Regresses performance on base64 benchmark.
>
> Original issue's description:
> > Allow Math.random to be called when creating a custom startup snapshot.
> >
> > R=jkummerow@chromium.org
> > BUG=v8:4810
> > LOG=N
> >
> > Committed: https://crrev.com/6a7ec6a3bf779cdd41c66a768fd7a37195ed7b7f
> > Cr-Commit-Position: refs/heads/master@{#34705}
>
> TBR=jkummerow@chromium.org
> # Not skipping CQ checks because original CL landed more than 1 days ago.
> BUG=v8:4810, chromium:594484
> LOG=N
>
> Committed: https://crrev.com/b7be51cd33bc81d768dbf5632ba0c68843448e37
> Cr-Commit-Position: refs/heads/master@{#34739}

TBR=jkummerow@chromium.org
# Not skipping CQ checks because original CL landed more than 1 days ago.
BUG=v8:4810, chromium:594484
LOG=N

Review URL: https://codereview.chromium.org/1806713003

Cr-Commit-Position: refs/heads/master@{#34820}
parent 18109232
......@@ -10,7 +10,6 @@
// -------------------------------------------------------------------
// Imports
define kRandomBatchSize = 64;
// The first two slots are reserved to persist PRNG state.
define kRandomNumberStart = 2;
......@@ -19,7 +18,7 @@ var GlobalMath = global.Math;
var GlobalObject = global.Object;
var InternalArray = utils.InternalArray;
var NaN = %GetRootNaN();
var nextRandomIndex = kRandomBatchSize;
var nextRandomIndex = 0;
var randomNumbers = UNDEFINED;
var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
......@@ -67,19 +66,24 @@ function MathPowJS(x, y) {
// ECMA 262 - 15.8.2.14
function MathRandom() {
if (nextRandomIndex >= kRandomBatchSize) {
// While creating a startup snapshot, %GenerateRandomNumbers returns a
// normal array containing a single random number, and has to be called for
// every new random number.
// Otherwise, it returns a pre-populated typed array of random numbers. The
// first two elements are reserved for the PRNG state.
if (nextRandomIndex <= kRandomNumberStart) {
randomNumbers = %GenerateRandomNumbers(randomNumbers);
nextRandomIndex = kRandomNumberStart;
nextRandomIndex = randomNumbers.length;
}
return randomNumbers[nextRandomIndex++];
return randomNumbers[--nextRandomIndex];
}
function MathRandomRaw() {
if (nextRandomIndex >= kRandomBatchSize) {
if (nextRandomIndex <= kRandomNumberStart) {
randomNumbers = %GenerateRandomNumbers(randomNumbers);
nextRandomIndex = kRandomNumberStart;
nextRandomIndex = randomNumbers.length;
}
return %_DoubleLo(randomNumbers[nextRandomIndex++]) & 0x3FFFFFFF;
return %_DoubleLo(randomNumbers[--nextRandomIndex]) & 0x3FFFFFFF;
}
// ECMA 262 - 15.8.2.15
......
......@@ -228,8 +228,18 @@ RUNTIME_FUNCTION(Runtime_MathSqrt) {
RUNTIME_FUNCTION(Runtime_GenerateRandomNumbers) {
HandleScope scope(isolate);
DCHECK(args.length() == 1);
// Random numbers in the snapshot are not really that random.
CHECK(!isolate->serializer_enabled());
if (isolate->serializer_enabled()) {
// Random numbers in the snapshot are not really that random. And we cannot
// return a typed array as it cannot be serialized. To make calling
// Math.random possible when creating a custom startup snapshot, we simply
// return a normal array with a single random number.
Handle<HeapNumber> random_number = isolate->factory()->NewHeapNumber(
isolate->random_number_generator()->NextDouble());
Handle<FixedArray> array_backing = isolate->factory()->NewFixedArray(1);
array_backing->set(0, *random_number);
return *isolate->factory()->NewJSArrayWithElements(array_backing);
}
static const int kState0Offset = 0;
static const int kState1Offset = 1;
static const int kRandomBatchSize = 64;
......
......@@ -462,7 +462,8 @@ static void PartiallySerializeCustomContext(
" e = function(s) { return eval (s); }"
"})();"
"var o = this;"
"var r = Math.sin(0) + Math.cos(0);"
"var r = Math.random();"
"var c = Math.sin(0) + Math.cos(0);"
"var f = (function(a, b) { return a + b; }).bind(1, 2, 3);"
"var s = parseInt('12345');");
......@@ -558,7 +559,18 @@ UNINITIALIZED_TEST(PartialSerializerCustomContext) {
->ToNumber(v8_isolate->GetCurrentContext())
.ToLocalChecked()
->Value();
CHECK_EQ(1, r);
CHECK(0.0 <= r && r < 1.0);
// Math.random still works.
double random = CompileRun("Math.random()")
->ToNumber(v8_isolate->GetCurrentContext())
.ToLocalChecked()
->Value();
CHECK(0.0 <= random && random < 1.0);
double c = CompileRun("c")
->ToNumber(v8_isolate->GetCurrentContext())
.ToLocalChecked()
->Value();
CHECK_EQ(1, c);
int f = CompileRun("f()")
->ToNumber(v8_isolate->GetCurrentContext())
.ToLocalChecked()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment