]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: add countAlloc benchmark
authorMichael Anthony Knyszek <mknyszek@google.com>
Wed, 18 Mar 2020 18:46:04 +0000 (18:46 +0000)
committerMichael Knyszek <mknyszek@google.com>
Mon, 23 Mar 2020 16:55:37 +0000 (16:55 +0000)
This change adds a small microbenchmark for (*mspan).countAlloc, which
we're about to replace. Admittedly this isn't a critical piece of code,
but the benchmark was useful in understanding the performance change.

Change-Id: Iea93c00f571ee95534a42f2ef2ab026b382242b3
Reviewed-on: https://go-review.googlesource.com/c/go/+/224438
Run-TryBot: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/runtime/export_test.go
src/runtime/gc_test.go

index 6a8d00c60ddd73df56d92a220988e7f998f3c73e..67379796c7382daffd3fd33e5e3352a62c034f52 100644 (file)
@@ -975,3 +975,11 @@ func MapHashCheck(m interface{}, k interface{}) (uintptr, uintptr) {
        y := typehash(kt, noescape(p), uintptr(mh.hash0))
        return x, y
 }
+
+func MSpanCountAlloc(bits []byte) int {
+       s := mspan{
+               nelems:     uintptr(len(bits) * 8),
+               gcmarkBits: (*gcBits)(unsafe.Pointer(&bits[0])),
+       }
+       return s.countAlloc()
+}
index 8ffb4f4a0f4690baa84235c759f1a8c5b246cc61..4c281ce52b830f6d4d1752f3dbce3074ca8b9854 100644 (file)
@@ -6,6 +6,7 @@ package runtime_test
 
 import (
        "fmt"
+       "math/rand"
        "os"
        "reflect"
        "runtime"
@@ -751,6 +752,24 @@ func BenchmarkScanStackNoLocals(b *testing.B) {
        close(teardown)
 }
 
+func BenchmarkMSpanCountAlloc(b *testing.B) {
+       // n is the number of bytes to benchmark against.
+       // n must always be a multiple of 8, since gcBits is
+       // always rounded up 8 bytes.
+       for _, n := range []int{8, 16, 32, 64, 128} {
+               b.Run(fmt.Sprintf("bits=%d", n*8), func(b *testing.B) {
+                       // Initialize a new byte slice with pseduo-random data.
+                       bits := make([]byte, n)
+                       rand.Read(bits)
+
+                       b.ResetTimer()
+                       for i := 0; i < b.N; i++ {
+                               runtime.MSpanCountAlloc(bits)
+                       }
+               })
+       }
+}
+
 func countpwg(n *int, ready *sync.WaitGroup, teardown chan bool) {
        if *n == 0 {
                ready.Done()