Skip to content

gonb deletes all "//go:noinline" comments before run #150

Closed
@alex91823

Description

package benchmark_test

import "testing"

func Benchmark_Cache_ADD_A(b *testing.B) {
	f := func() {
		db := NewDataBase()

		Fn(db)
	}

	for range b.N {
		f()
	}
}

type Cache interface {
	Add(key, value string)
	Get(key string) (string, bool)
}

type DataBase struct {
	m map[string]string
}

func NewDataBase() *DataBase {
	return &DataBase{m: make(map[string]string)}
}

func (b *DataBase) Add(key, value string) {
	b.m[key] = value
}

func (b *DataBase) Get(key string) (string, bool) {
	value, ok := b.m[key]

	return value, ok
}

//go:noinline
func Fn(c Cache) {
	c.Add("key", "42")
}

func main() {
	dataBase := NewDataBase()

	Fn(dataBase)
}

%test -test.bench=. -test.run=Benchmark. -test.benchmem

Benchmark_Cache_ADD_A-16 87825375 14.22 ns/op 0 B/op 0 allocs/op

The result is wrong because gonb delete all //go:noinline comments in code before run.

Right result is:

Benchmark_Cache_ADD-16 5417229 206.5 ns/op 344 B/op 3 allocs/op

Please don't delete //go:noinline comments...

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions