mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 12:24:38 +03:00
Improve oshash code (#1829)
Reduce allocations. Don't create intermediary arrays which we then consume right after. Manually fuse the arrays and decode straight into the sum instead. Furthermore, don't invoke a Reader, but carve out the locations via a loop, directly. These two changes taken together speeds up oshash computations by a factor of 10 according to the benchmark tests. The main reason for this change is a much lowered memory allocation rate which in turn improves GC pressure. While here, add a benchmark for oshash computations and use it for testing the performance.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -44,3 +45,31 @@ func TestOshashCollisions(t *testing.T) {
|
||||
t.Errorf("TestOshashCollisions: oshash(n, k, ... %v) =! oshash(n, k, ... %v)", buf1, buf2)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkOsHash(b *testing.B) {
|
||||
src := rand.NewSource(9999)
|
||||
r := rand.New(src)
|
||||
|
||||
size := int64(1234567890)
|
||||
|
||||
head := make([]byte, 1024*64)
|
||||
_, err := r.Read(head)
|
||||
if err != nil {
|
||||
b.Errorf("unable to generate head array: %v", err)
|
||||
}
|
||||
|
||||
tail := make([]byte, 1024*64)
|
||||
_, err = r.Read(tail)
|
||||
if err != nil {
|
||||
b.Errorf("unable to generate tail array: %v", err)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
for n := 0; n < b.N; n++ {
|
||||
_, err := oshash(size, head, tail)
|
||||
if err != nil {
|
||||
b.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user