make simdjson great again.

now it is generally a little faster than the easyjson approach.

goos: linux
goarch: amd64
pkg: github.com/nbd-wtf/go-nostr
cpu: AMD Ryzen 3 3200G with Radeon Vega Graphics
BenchmarkParseMessage/stdlib-4         	      90	  15616341 ns/op
BenchmarkParseMessage/easyjson-4       	     110	  11306466 ns/op
BenchmarkParseMessage/simdjson-4       	     162	   7779856 ns/op
PASS
ok  	github.com/nbd-wtf/go-nostr	5.547s
This commit is contained in:
fiatjaf
2025-03-05 23:42:16 -03:00
parent de358e641c
commit 4fb6fcd9a2
6 changed files with 258 additions and 257 deletions

View File

@@ -1,6 +1,7 @@
package nostr
import (
stdlibjson "encoding/json"
"fmt"
"math/rand/v2"
"testing"
@@ -12,7 +13,16 @@ import (
func BenchmarkParseMessage(b *testing.B) {
messages := generateTestMessages(2000)
b.Run("golang", func(b *testing.B) {
b.Run("stdlib", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, msg := range messages {
var v any
stdlibjson.Unmarshal(msg, &v)
}
}
})
b.Run("easyjson", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, msg := range messages {
_ = ParseMessage(msg)
@@ -21,10 +31,10 @@ func BenchmarkParseMessage(b *testing.B) {
})
b.Run("simdjson", func(b *testing.B) {
pj := &simdjson.ParsedJson{}
smp := SIMDMessageParser{ParsedJSON: &simdjson.ParsedJson{}, AuxIter: &simdjson.Iter{}}
for i := 0; i < b.N; i++ {
for _, msg := range messages {
_, _ = ParseMessageSIMD(msg, pj)
_, _ = smp.ParseMessage(msg)
}
}
})
@@ -76,9 +86,9 @@ func generateRandomEvent() Event {
tags := make(Tags, tagCount)
for i := 0; i < tagCount; i++ {
tagType := string([]byte{byte('a' + rand.IntN(26))})
tagValues := make([]string, rand.IntN(5)+1)
tagValues := make([]string, rand.IntN(3)+1)
for j := range tagValues {
tagValues[j] = fmt.Sprintf("value_%d_%d", i, j)
tagValues[j] = fmt.Sprintf("%d", j)
}
tags[i] = append([]string{tagType}, tagValues...)
}