Skip to content

trie: use common.Hash as the key in secKeyCache map #31786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2025

Conversation

mask-pp
Copy link
Contributor

@mask-pp mask-pp commented May 8, 2025

Simplify secKeyCache to avoid unnecessary transfers among []byte, string and common.Hash.

@mask-pp mask-pp requested a review from rjl493456442 as a code owner May 8, 2025 12:39
@MariusVanDerWijden
Copy link
Member

Could you show that there is a benefit to this with a benchmark?

@mask-pp
Copy link
Contributor Author

mask-pp commented May 8, 2025

Could you show that there is a benefit to this with a benchmark?

Ok, I'll add a benchmark test case to show the difference.

@mask-pp
Copy link
Contributor Author

mask-pp commented May 8, 2025

Could you show that there is a benefit to this with a benchmark?

This is the benchmark result:

BenchmarkSecKeyCache/string_key
BenchmarkSecKeyCache/string_key-12         	51602098	        22.25 ns/op
BenchmarkSecKeyCache/hash_key
BenchmarkSecKeyCache/hash_key-12           	96151920	        12.39 ns/op

This is the test case:

func BenchmarkSecKeyCache(b *testing.B) {
	values := make(map[int]common.Hash) // collect test data
	for i := range b.N {
		data := common.FromHex(fmt.Sprintf("%x", i))
		hk := common.BytesToHash(data)
		values[i] = hk
	}

	valuesStr := make(map[string][]byte)
	b.Run("string_key", func(b *testing.B) {
		for i := range b.N {
			key := values[i]
			valuesStr[string(key[:])] = nil // Simulate the operation before modification.
		}
	})

	valuesHash := make(map[common.Hash][]byte)
	b.Run("hash_key", func(b *testing.B) {
		for i := range b.N {
			key := values[i]
			valuesHash[key] = nil // Simulate the operation after modification.
		}
	})
}

@mask-pp
Copy link
Contributor Author

mask-pp commented May 8, 2025

Could you show that there is a benefit to this with a benchmark?

This change can also eliminate the map conversion:
image

@mask-pp mask-pp force-pushed the change_secKeyCache_type branch from 01b4440 to e99dd39 Compare May 13, 2025 13:27
@mask-pp mask-pp requested a review from rjl493456442 May 13, 2025 13:28
Copy link
Member

@rjl493456442 rjl493456442 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

@rjl493456442 rjl493456442 added this to the 1.15.12 milestone May 19, 2025
@rjl493456442 rjl493456442 merged commit 0867c24 into ethereum:master May 19, 2025
3 of 4 checks passed
Dargon789 pushed a commit to Dargon789/go-ethereum that referenced this pull request May 27, 2025
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.

3 participants