Skip to content

Commit f4fd79f

Browse files
aykevldeadprogram
authored andcommitted
runtime: manually initialize xorshift state
This ensures: 1. The xorshift state is initialized during interp. 2. The xorshift state gets initialized to a real random number on hardware that supports it at runtime. This fixes a big binary size regression from the previous commit. It's still not perfect: most programs increase binary size by a few bytes. But it's not nearly as bad as before.
1 parent 811b13a commit f4fd79f

File tree

5 files changed

+8
-5
lines changed

5 files changed

+8
-5
lines changed

builder/sizes_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func TestBinarySize(t *testing.T) {
4343
tests := []sizeTest{
4444
// microcontrollers
4545
{"hifive1b", "examples/echo", 4560, 280, 0, 2268},
46-
{"microbit", "examples/serial", 2908, 388, 8, 2272},
47-
{"wioterminal", "examples/pininterrupt", 7293, 1487, 116, 6912},
46+
{"microbit", "examples/serial", 2916, 388, 8, 2272},
47+
{"wioterminal", "examples/pininterrupt", 7315, 1489, 116, 6912},
4848

4949
// TODO: also check wasm. Right now this is difficult, because
5050
// wasm binaries are run through wasm-opt and therefore the

src/runtime/algorithm.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ func fastrand() uint32 {
2323
return xorshift32State
2424
}
2525

26-
func init() {
26+
func initRand() {
2727
r, _ := hardwareRand()
2828
xorshift64State = uint64(r | 1) // protect against 0
2929
xorshift32State = uint32(xorshift64State)
3030
}
3131

32-
var xorshift32State uint32
32+
var xorshift32State uint32 = 1
3333

3434
func xorshift32(x uint32) uint32 {
3535
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs".
@@ -49,7 +49,7 @@ func fastrand64() uint64 {
4949
return xorshift64State
5050
}
5151

52-
var xorshift64State uint64
52+
var xorshift64State uint64 = 1
5353

5454
// 64-bit xorshift multiply rng from http://vigna.di.unimi.it/ftp/papers/xorshift.pdf
5555
func xorshiftMult64(x uint64) uint64 {

src/runtime/runtime_wasmentry.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func wasmEntryReactor() {
3535
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
3636
heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize)
3737
initHeap()
38+
initRand()
3839

3940
if hasScheduler {
4041
// A package initializer might do funky stuff like start a goroutine and

src/runtime/scheduler_cooperative.go

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ func sleep(duration int64) {
247247
// With a scheduler, init and the main function are invoked in a goroutine before starting the scheduler.
248248
func run() {
249249
initHeap()
250+
initRand()
250251
go func() {
251252
initAll()
252253
callMain()

src/runtime/scheduler_none.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const hasParallelism = false
1313
// With the "none" scheduler, init and the main function are invoked directly.
1414
func run() {
1515
initHeap()
16+
initRand()
1617
initAll()
1718
callMain()
1819
mainExited = true

0 commit comments

Comments
 (0)