mirror of
https://github.com/golang/go
synced 2024-11-12 07:40:23 -07:00
runtime: add simple malloc benchmarks
Allocs of size 16 can bypass atomic set of the allocated bit, while allocs of size 8 can not. Allocs with and w/o type info hit different paths inside of malloc. Current results on linux/amd64: BenchmarkMalloc8 50000000 43.6 ns/op BenchmarkMalloc16 50000000 46.7 ns/op BenchmarkMallocTypeInfo8 50000000 61.3 ns/op BenchmarkMallocTypeInfo16 50000000 63.5 ns/op R=golang-dev, remyoudompheng, minux.ma, bradfitz, iant CC=golang-dev https://golang.org/cl/9090045
This commit is contained in:
parent
ee66972dce
commit
915784e11a
52
src/pkg/runtime/malloc_test.go
Normal file
52
src/pkg/runtime/malloc_test.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// Copyright 2013 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package runtime_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
var mallocSink uintptr
|
||||||
|
|
||||||
|
func BenchmarkMalloc8(b *testing.B) {
|
||||||
|
var x uintptr
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
p := new(int64)
|
||||||
|
x ^= uintptr(unsafe.Pointer(p))
|
||||||
|
}
|
||||||
|
mallocSink = x
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkMalloc16(b *testing.B) {
|
||||||
|
var x uintptr
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
p := new([2]int64)
|
||||||
|
x ^= uintptr(unsafe.Pointer(p))
|
||||||
|
}
|
||||||
|
mallocSink = x
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkMallocTypeInfo8(b *testing.B) {
|
||||||
|
var x uintptr
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
p := new(struct {
|
||||||
|
p [8 / unsafe.Sizeof(uintptr(0))]*int
|
||||||
|
})
|
||||||
|
x ^= uintptr(unsafe.Pointer(p))
|
||||||
|
}
|
||||||
|
mallocSink = x
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkMallocTypeInfo16(b *testing.B) {
|
||||||
|
var x uintptr
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
p := new(struct {
|
||||||
|
p [16 / unsafe.Sizeof(uintptr(0))]*int
|
||||||
|
})
|
||||||
|
x ^= uintptr(unsafe.Pointer(p))
|
||||||
|
}
|
||||||
|
mallocSink = x
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user