diff --git a/nson/README.md b/nson/README.md index a22c1ea..96354e7 100644 --- a/nson/README.md +++ b/nson/README.md @@ -11,15 +11,15 @@ Some benchmarks: goos: linux goarch: amd64 pkg: github.com/nbd-wtf/go-nostr/nson -cpu: AMD Ryzen 3 3200G with Radeon Vega Graphics -BenchmarkNSONEncoding/json.Marshal-4 6214 230680 ns/op -BenchmarkNSONEncoding/nson.Marshal-4 4520 319058 ns/op -BenchmarkNSONDecoding/json.Unmarshal-4 3741 280641 ns/op -BenchmarkNSONDecoding/nson.Unmarshal-4 46519 23762 ns/op -BenchmarkNSONDecoding/json.Unmarshal_+_sig_verification-4 352 3218583 ns/op -BenchmarkNSONDecoding/nson.Unmarshal_+_sig_verification-4 451 2739238 ns/op +cpu: 13th Gen Intel(R) Core(TM) i7-13620H +BenchmarkNSONEncoding/easyjson.Marshal-16 18795 61397 ns/op +BenchmarkNSONEncoding/nson.Marshal-16 5985 205112 ns/op +BenchmarkNSONDecoding/easyjson.Unmarshal-16 14928 83890 ns/op +BenchmarkNSONDecoding/nson.Unmarshal-16 24982 50527 ns/op +BenchmarkNSONDecoding/easyjson.Unmarshal+sig-16 196 5898287 ns/op +BenchmarkNSONDecoding/nson.Unmarshal+sig-16 205 5802747 ns/op PASS -ok github.com/nbd-wtf/go-nostr/nson 8.291s +ok github.com/nbd-wtf/go-nostr/nson 10.227s ``` It takes a little while more to encode (although it's probably possible to optimize that), but decodes at 10x the diff --git a/nson/nson_test.go b/nson/nson_test.go index 3deb32a..b224f3a 100644 --- a/nson/nson_test.go +++ b/nson/nson_test.go @@ -127,6 +127,12 @@ var nsonTestEvents = []string{ `{"id":"ec9345e2af4225aada296964fa6025a1666dcac8dba154f5591a81f7dee1f84a","pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","sig":"49f4b9edd7eff9e127b70077daff9a66da8c1ad974e5e6f47c094e8cc0c553071ff61c07b69d3db80c25f36248237ba6021038f5eb6b569ce79e3b024e8e358d","created_at":1688572819,"nson":"0401000400","kind":1,"content":"x\ty","tags":[]}`, } +var ( + EncodedEventEasyJson []byte + EncodedEventNSON string + DecodedEvent *nostr.Event +) + func BenchmarkNSONEncoding(b *testing.B) { events := make([]*nostr.Event, len(test_common.NormalEvents)) for i, jevt := range test_common.NormalEvents { @@ -135,20 +141,26 @@ func BenchmarkNSONEncoding(b *testing.B) { events[i] = evt } + b.ResetTimer() + b.Run("easyjson.Marshal", func(b *testing.B) { + var encodedEvent []byte for i := 0; i < b.N; i++ { for _, evt := range events { - easyjson.Marshal(evt) + encodedEvent, _ = easyjson.Marshal(evt) } } + EncodedEventEasyJson = encodedEvent }) b.Run("nson.Marshal", func(b *testing.B) { + var encodedEvent string for i := 0; i < b.N; i++ { for _, evt := range events { - Marshal(evt) + encodedEvent, _ = Marshal(evt) } } + EncodedEventNSON = encodedEvent }) } @@ -161,34 +173,38 @@ func BenchmarkNSONDecoding(b *testing.B) { events[i] = nevt } + b.ResetTimer() + b.Run("easyjson.Unmarshal", func(b *testing.B) { + evt := &nostr.Event{} for i := 0; i < b.N; i++ { for _, nevt := range events { - evt := &nostr.Event{} err := easyjson.Unmarshal([]byte(nevt), evt) if err != nil { b.Fatalf("failed to unmarshal: %s", err) } } } + DecodedEvent = evt }) b.Run("nson.Unmarshal", func(b *testing.B) { + evt := &nostr.Event{} for i := 0; i < b.N; i++ { for _, nevt := range events { - evt := &nostr.Event{} err := Unmarshal(nevt, evt) if err != nil { b.Fatalf("failed to unmarshal: %s", err) } } } + DecodedEvent = evt }) b.Run("easyjson.Unmarshal+sig", func(b *testing.B) { + evt := &nostr.Event{} for i := 0; i < b.N; i++ { for _, nevt := range events { - evt := &nostr.Event{} err := easyjson.Unmarshal([]byte(nevt), evt) if err != nil { b.Fatalf("failed to unmarshal: %s", err) @@ -196,12 +212,13 @@ func BenchmarkNSONDecoding(b *testing.B) { evt.CheckSignature() } } + DecodedEvent = evt }) b.Run("nson.Unmarshal+sig", func(b *testing.B) { + evt := &nostr.Event{} for i := 0; i < b.N; i++ { for _, nevt := range events { - evt := &nostr.Event{} err := Unmarshal(nevt, evt) if err != nil { b.Fatalf("failed to unmarshal: %s", err) @@ -209,5 +226,6 @@ func BenchmarkNSONDecoding(b *testing.B) { evt.CheckSignature() } } + DecodedEvent = evt }) }