Skip to content

Commit f96b95b

Browse files
committed
runtime: benchmark for bulk write barriers
This adds a benchmark of typedslicecopy and its bulk write barriers. For #22460. Change-Id: I439ca3b130bb22944468095f8f18b464e5bb43ca Reviewed-on: https://go-review.googlesource.com/74051 Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rick Hudson <[email protected]>
1 parent 7e34313 commit f96b95b

File tree

1 file changed

+82
-39
lines changed

1 file changed

+82
-39
lines changed

src/runtime/gc_test.go

Lines changed: 82 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,32 @@ func TestUserForcedGC(t *testing.T) {
517517
}
518518
}
519519

520+
func writeBarrierBenchmark(b *testing.B, f func()) {
521+
runtime.GC()
522+
var ms runtime.MemStats
523+
runtime.ReadMemStats(&ms)
524+
//b.Logf("heap size: %d MB", ms.HeapAlloc>>20)
525+
526+
// Keep GC running continuously during the benchmark, which in
527+
// turn keeps the write barrier on continuously.
528+
var stop uint32
529+
done := make(chan bool)
530+
go func() {
531+
for atomic.LoadUint32(&stop) == 0 {
532+
runtime.GC()
533+
}
534+
close(done)
535+
}()
536+
defer func() {
537+
atomic.StoreUint32(&stop, 1)
538+
<-done
539+
}()
540+
541+
b.ResetTimer()
542+
f()
543+
b.StopTimer()
544+
}
545+
520546
func BenchmarkWriteBarrier(b *testing.B) {
521547
if runtime.GOMAXPROCS(-1) < 2 {
522548
// We don't want GC to take our time.
@@ -546,53 +572,70 @@ func BenchmarkWriteBarrier(b *testing.B) {
546572
const depth = 22 // 64 MB
547573
root := mkTree(22)
548574

549-
runtime.GC()
550-
var ms runtime.MemStats
551-
runtime.ReadMemStats(&ms)
552-
//b.Logf("heap size: %d MB", ms.HeapAlloc>>20)
575+
writeBarrierBenchmark(b, func() {
576+
var stack [depth]*node
577+
tos := -1
553578

554-
// Keep GC running continuously during the benchmark.
555-
var stop uint32
556-
done := make(chan bool)
557-
go func() {
558-
for atomic.LoadUint32(&stop) == 0 {
559-
runtime.GC()
579+
// There are two write barriers per iteration, so i+=2.
580+
for i := 0; i < b.N; i += 2 {
581+
if tos == -1 {
582+
stack[0] = root
583+
tos = 0
584+
}
585+
586+
// Perform one step of reversing the tree.
587+
n := stack[tos]
588+
if n.l == nil {
589+
tos--
590+
} else {
591+
n.l, n.r = n.r, n.l
592+
stack[tos] = n.l
593+
stack[tos+1] = n.r
594+
tos++
595+
}
596+
597+
if i%(1<<12) == 0 {
598+
// Avoid non-preemptible loops (see issue #10958).
599+
runtime.Gosched()
600+
}
560601
}
561-
close(done)
562-
}()
602+
})
563603

564-
b.ResetTimer()
604+
runtime.KeepAlive(wbRoots)
605+
}
565606

566-
var stack [depth]*node
567-
tos := -1
607+
func BenchmarkBulkWriteBarrier(b *testing.B) {
608+
if runtime.GOMAXPROCS(-1) < 2 {
609+
// We don't want GC to take our time.
610+
b.Skip("need GOMAXPROCS >= 2")
611+
}
568612

569-
// There are two write barriers per iteration, so i+=2.
570-
for i := 0; i < b.N; i += 2 {
571-
if tos == -1 {
572-
stack[0] = root
573-
tos = 0
574-
}
613+
// Construct a large set of objects we can copy around.
614+
const heapSize = 64 << 20
615+
type obj [16]*byte
616+
ptrs := make([]*obj, heapSize/unsafe.Sizeof(obj{}))
617+
for i := range ptrs {
618+
ptrs[i] = new(obj)
619+
}
575620

576-
// Perform one step of reversing the tree.
577-
n := stack[tos]
578-
if n.l == nil {
579-
tos--
580-
} else {
581-
n.l, n.r = n.r, n.l
582-
stack[tos] = n.l
583-
stack[tos+1] = n.r
584-
tos++
585-
}
621+
writeBarrierBenchmark(b, func() {
622+
const blockSize = 1024
623+
var pos int
624+
for i := 0; i < b.N; i += blockSize {
625+
// Rotate block.
626+
block := ptrs[pos : pos+blockSize]
627+
first := block[0]
628+
copy(block, block[1:])
629+
block[blockSize-1] = first
630+
631+
pos += blockSize
632+
if pos+blockSize > len(ptrs) {
633+
pos = 0
634+
}
586635

587-
if i%(1<<12) == 0 {
588-
// Avoid non-preemptible loops (see issue #10958).
589636
runtime.Gosched()
590637
}
591-
}
638+
})
592639

593-
b.StopTimer()
594-
atomic.StoreUint32(&stop, 1)
595-
<-done
596-
597-
runtime.KeepAlive(wbRoots)
640+
runtime.KeepAlive(ptrs)
598641
}

0 commit comments

Comments
 (0)