Source file src/runtime/malloc_stubs_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime_test
     6  
     7  import (
     8  	"internal/abi"
     9  	"runtime"
    10  	"testing"
    11  )
    12  
    13  const size_ = 0
    14  
    15  func benchmarkStubTiny(b *testing.B) {
    16  	const size = size_
    17  	type s struct {
    18  		v [size]byte
    19  	}
    20  	b.Run("kind=new", func(b *testing.B) {
    21  		for b.Loop() {
    22  			runtime.Escape(new(s))
    23  		}
    24  	})
    25  	typ := abi.TypeOf(s{})
    26  	b.Run("kind=mallocgc", func(b *testing.B) {
    27  		for b.Loop() {
    28  			runtime.Escape(runtime.MallocGC(size, typ, false))
    29  		}
    30  	})
    31  }
    32  
    33  const noscan_ = false
    34  
    35  func benchmarkStub(b *testing.B) {
    36  	const size = size_
    37  	b.Run("kind=new", func(b *testing.B) {
    38  		for b.Loop() {
    39  			if noscan_ {
    40  				runtime.Escape(new(struct{ v [size / 8]uint64 }))
    41  			}
    42  			if !noscan_ {
    43  				runtime.Escape(new(struct{ v [size / 8]*uint64 }))
    44  			}
    45  		}
    46  	})
    47  	var typ *abi.Type
    48  	if noscan_ {
    49  		typ = abi.TypeOf(struct{ v [size / 8]uint64 }{})
    50  	}
    51  	if !noscan_ {
    52  		typ = abi.TypeOf(struct{ v [size / 8]*uint64 }{})
    53  	}
    54  	b.Run("kind=mallocgc", func(b *testing.B) {
    55  		for b.Loop() {
    56  			runtime.Escape(runtime.MallocGC(size, typ, true))
    57  		}
    58  	})
    59  }
    60  
    61  func benchmarkScanSliceStub(b *testing.B) {
    62  	const size = size_
    63  	for b.Loop() {
    64  		runtime.Escape(make([]*uint64, size/8))
    65  	}
    66  }
    67  

View as plain text