Skip to content
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

Is it reasonable to use runtime.fasthash on platforms other than linux_amd64? #10

Open
jxskiss opened this issue Sep 29, 2019 · 2 comments

Comments

@jxskiss
Copy link

jxskiss commented Sep 29, 2019

Since drwmutex is mainly to improve the performance of read-most workload, use a fast hash function also can distribute the lock evenly, which may help to reduce lock contention.
Benchmark shows cost of runtime.fasthashn is very cheap.

func BenchmarkCpuid(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = cpu()
	}
}

func BenchmarkFastrand(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = fastrandn(CPU_COUNT)
	}
}

func cpu() uint64

//go:noescape
//go:linkname fastrandn runtime.fastrandn
func fastrandn(x uint32) uint32
goos: darwin
goarch: amd64

BenchmarkCpuid-12               16798030                66.3 ns/op
BenchmarkFastrand-12            504372906                2.39 ns/op

goos: linux
goarch: amd64

BenchmarkCpuid-4         1471983               813 ns/op
BenchmarkFastrand-4     324570566                3.58 ns/op

So is it reasonable to use this rand method to choose read lock on platforms other than linux_amd64?

@jxskiss
Copy link
Author

jxskiss commented Sep 29, 2019

@jonhoo

@jonhoo
Copy link
Owner

jonhoo commented Sep 30, 2019

Hmm.. While this would help somewhat, it won't have anywhere near the same scalability as using a core-local lock (which CPUID) gives you. It could be that it's a reasonable fall-back for other platforms for the time being, but at the same time I think I'd prefer to say that this is linux_amd64-only so that people don't depend on the scaling behavior of the CPUID implementations and then get disappointed when they don't get the same behavior on other platforms.

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

No branches or pull requests

2 participants