Skip to content

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

@jxskiss

Description

@jxskiss

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions