runtime: use RtlGenRandom instead of CryptGenRandom

This change replaces the use of CryptGenRandom with RtlGenRandom in
Windows to generate cryptographically random numbers during process
startup. RtlGenRandom uses the same RNG as CryptGenRandom, but it has many
fewer DLL dependencies and so does not affect process startup time as
much.

This makes running simple Go program on my computers faster.

Windows XP:
benchmark                      old ns/op     new ns/op     delta
BenchmarkRunningGoProgram-2     47408573      10784148      -77.25%

Windows 7 (VM):
benchmark                    old ns/op     new ns/op     delta
BenchmarkRunningGoProgram     16260390      12792150      -21.33%

Windows 7:
benchmark                      old ns/op     new ns/op     delta
BenchmarkRunningGoProgram-2     13600778      10050574      -26.10%

Fixes #15589

Change-Id: I2816239a2056e3d4a6dcd86a6fa2bb619c6008fe
Reviewed-on: https://go-review.googlesource.com/29700
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Alex Brainman 2016-09-26 16:44:35 +10:00
parent 98938189a1
commit db82cf4e50
2 changed files with 57 additions and 16 deletions

View file

@ -972,3 +972,41 @@ func BenchmarkOsYield(b *testing.B) {
runtime.OsYield()
}
}
func BenchmarkRunningGoProgram(b *testing.B) {
tmpdir, err := ioutil.TempDir("", "BenchmarkRunningGoProgram")
if err != nil {
b.Fatal(err)
}
defer os.RemoveAll(tmpdir)
src := filepath.Join(tmpdir, "main.go")
err = ioutil.WriteFile(src, []byte(benchmarkRunnigGoProgram), 0666)
if err != nil {
b.Fatal(err)
}
exe := filepath.Join(tmpdir, "main.exe")
cmd := exec.Command("go", "build", "-o", exe, src)
cmd.Dir = tmpdir
out, err := cmd.CombinedOutput()
if err != nil {
b.Fatalf("building main.exe failed: %v\n%s", err, out)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
cmd := exec.Command(exe)
out, err := cmd.CombinedOutput()
if err != nil {
b.Fatalf("runing main.exe failed: %v\n%s", err, out)
}
}
}
const benchmarkRunnigGoProgram = `
package main
func main() {
}
`