lib/protocol: Optimize luhn and chunk functions
These functions were very naive and slow. We haven't done much about them because they pretty much don't matter at all for Syncthing performance. They are however called very often in the discovery server and these optimizations have a huge effect on the CPU load on the public discovery servers. The code isn't exactly obvious, but we have good test coverage on all these functions. benchmark old ns/op new ns/op delta BenchmarkLuhnify-8 12458 1045 -91.61% BenchmarkUnluhnify-8 12598 1074 -91.47% BenchmarkChunkify-8 10792 104 -99.04% benchmark old allocs new allocs delta BenchmarkLuhnify-8 18 1 -94.44% BenchmarkUnluhnify-8 18 1 -94.44% BenchmarkChunkify-8 44 2 -95.45% benchmark old bytes new bytes delta BenchmarkLuhnify-8 1278 64 -94.99% BenchmarkUnluhnify-8 1278 64 -94.99% BenchmarkChunkify-8 42552 128 -99.70% GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4346
This commit is contained in:
committed by
Audrius Butkevicius
parent
9e6a1fdcd4
commit
9682bbfbda
@@ -150,3 +150,41 @@ func TestNewDeviceIDMarshalling(t *testing.T) {
|
||||
t.Error("Mismatch in old -> new direction")
|
||||
}
|
||||
}
|
||||
|
||||
var resStr string
|
||||
|
||||
func BenchmarkLuhnify(b *testing.B) {
|
||||
str := "ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB"
|
||||
var err error
|
||||
for i := 0; i < b.N; i++ {
|
||||
resStr, err = luhnify(str)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkUnluhnify(b *testing.B) {
|
||||
str, _ := luhnify("ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB")
|
||||
var err error
|
||||
for i := 0; i < b.N; i++ {
|
||||
resStr, err = unluhnify(str)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkChunkify(b *testing.B) {
|
||||
str := "ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB"
|
||||
for i := 0; i < b.N; i++ {
|
||||
resStr = chunkify(str)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkUnchunkify(b *testing.B) {
|
||||
str := chunkify("ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB")
|
||||
for i := 0; i < b.N; i++ {
|
||||
resStr = unchunkify(str)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user