Skip to content

Conversation

cuiweixie
Copy link
Contributor

benchmark code:

package main

import (
	"testing"

	"github.com/ethereum/go-ethereum/common"
)

func storageKeyCopy(accountHash, slotHash common.Hash) [64]byte {
	var key [64]byte
	copy(key[:32], accountHash[:])
	copy(key[32:], slotHash[:])
	return key
}

func storageKeyPointer(accountHash, slotHash common.Hash) [64]byte {
	var key [64]byte
	*(*[32]byte)(key[:32]) = accountHash
	*(*[32]byte)(key[32:]) = slotHash
	return key
}

func BenchmarkStorageKeyCopy(b *testing.B) {
	var a, s common.Hash
	for i := 0; i < b.N; i++ {
		_ = storageKeyCopy(a, s)
	}
}

func BenchmarkStorageKeyPointer(b *testing.B) {
	var a, s common.Hash
	for i := 0; i < b.N; i++ {
		_ = storageKeyPointer(a, s)
	}
}
···
the result is:
···
goos: darwin
goarch: arm64
pkg: github.com/cuiweixie/mylabs/tests
cpu: Apple M1 Pro
BenchmarkStorageKeyCopy
BenchmarkStorageKeyCopy-10       	415266823	         2.987 ns/op
BenchmarkStorageKeyPointer
BenchmarkStorageKeyPointer-10    	1000000000	         0.7977 ns/op
PASS

Process finished with the exit code 0

···

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants