Skip to content

Commit 7717ac1

Browse files
committed
runtime: make Malloc benchmarks actually benchmark malloc
The compiler is too clever so the allocations are currently avoided. Rewrite to make them actually allocate. Change-Id: I9542e1365120b2ace318360883b0b01ed5670da7 Reviewed-on: https://go-review.googlesource.com/c/go/+/449476 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Cherry Mui <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 05cc8b5 commit 7717ac1

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

src/runtime/malloc_test.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -318,59 +318,47 @@ func TestArenaCollision(t *testing.T) {
318318
}
319319
}
320320

321-
var mallocSink uintptr
322-
323321
func BenchmarkMalloc8(b *testing.B) {
324-
var x uintptr
325322
for i := 0; i < b.N; i++ {
326323
p := new(int64)
327-
x ^= uintptr(unsafe.Pointer(p))
324+
Escape(p)
328325
}
329-
mallocSink = x
330326
}
331327

332328
func BenchmarkMalloc16(b *testing.B) {
333-
var x uintptr
334329
for i := 0; i < b.N; i++ {
335330
p := new([2]int64)
336-
x ^= uintptr(unsafe.Pointer(p))
331+
Escape(p)
337332
}
338-
mallocSink = x
339333
}
340334

341335
func BenchmarkMallocTypeInfo8(b *testing.B) {
342-
var x uintptr
343336
for i := 0; i < b.N; i++ {
344337
p := new(struct {
345338
p [8 / unsafe.Sizeof(uintptr(0))]*int
346339
})
347-
x ^= uintptr(unsafe.Pointer(p))
340+
Escape(p)
348341
}
349-
mallocSink = x
350342
}
351343

352344
func BenchmarkMallocTypeInfo16(b *testing.B) {
353-
var x uintptr
354345
for i := 0; i < b.N; i++ {
355346
p := new(struct {
356347
p [16 / unsafe.Sizeof(uintptr(0))]*int
357348
})
358-
x ^= uintptr(unsafe.Pointer(p))
349+
Escape(p)
359350
}
360-
mallocSink = x
361351
}
362352

363353
type LargeStruct struct {
364354
x [16][]byte
365355
}
366356

367357
func BenchmarkMallocLargeStruct(b *testing.B) {
368-
var x uintptr
369358
for i := 0; i < b.N; i++ {
370359
p := make([]LargeStruct, 2)
371-
x ^= uintptr(unsafe.Pointer(&p[0]))
360+
Escape(p)
372361
}
373-
mallocSink = x
374362
}
375363

376364
var n = flag.Int("n", 1000, "number of goroutines")

0 commit comments

Comments
 (0)