Skip to content

Instantly share code, notes, and snippets.

@holiman
Last active June 10, 2020 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holiman/c556dabf3029924a454757c138841b3f to your computer and use it in GitHub Desktop.
Save holiman/c556dabf3029924a454757c138841b3f to your computer and use it in GitHub Desktop.
package main
import "testing"
type Address [20]byte
type AccountRef Address
func (ar AccountRef) Address() Address { return (Address)(ar) }
type ContractRef interface {
Address() Address
}
type Contract struct {
CallerAddress Address
CodeAddr *Address
}
func BadOperation(caller ContractRef, addr Address) {
if ok := true; ok {
return
} else {
contract := &Contract{CallerAddress: caller.Address()}
contract.CodeAddr = &addr
}
return
}
func GoodOperation(caller ContractRef, addr Address) {
if ok := true; ok {
return
} else {
//contract := &Contract{CallerAddress: caller.Address()}
//contract.CodeAddr = &addr
}
return
}
func BadAlloc() {
for i := uint64(0); i < 100000; i++ {
BadOperation(AccountRef(Address{}), Address{})
}
}
func GoodAlloc() {
for i := uint64(0); i < 100000; i++ {
GoodOperation(AccountRef(Address{}), Address{})
}
}
func BenchmarkAllocs(b *testing.B) {
b.Run("bad", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
BadAlloc()
}
})
b.Run("good", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
GoodAlloc()
}
})
}
goos: linux
goarch: amd64
pkg: github.com/ethereum/go-ethereum/cmd/testvm
BenchmarkAllocs
BenchmarkAllocs/bad
BenchmarkAllocs/bad-6 100 10296649 ns/op 6400030 B/op 200000 allocs/op
BenchmarkAllocs/good
BenchmarkAllocs/good-6 34618 29632 ns/op 0 B/op 0 allocs/op
PASS
@holiman
Copy link
Author

holiman commented Jun 10, 2020

[user@work testvm]$ go version
go version go1.14.4 linux/amd64

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