mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-25 11:16:57 +02:00
binary: unsafe.String() decoders.
This commit is contained in:
@@ -7,18 +7,18 @@ goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/nbd-wtf/go-nostr/binary
|
||||
cpu: AMD Ryzen 3 3200G with Radeon Vega Graphics
|
||||
BenchmarkBinaryEncoding/easyjson.Marshal-4 8283 153034 ns/op 95167 B/op 123 allocs/op
|
||||
BenchmarkBinaryEncoding/gob.Encode-4 3601 299684 ns/op 407859 B/op 1491 allocs/op
|
||||
BenchmarkBinaryEncoding/binary.Marshal-4 4004 346269 ns/op 1476069 B/op 814 allocs/op
|
||||
BenchmarkBinaryEncoding/binary.MarshalBinary-4 3368 354479 ns/op 1471205 B/op 757 allocs/op
|
||||
BenchmarkBinaryDecoding/easyjson.Unmarshal-4 4684 253556 ns/op 257561 B/op 1584 allocs/op
|
||||
BenchmarkBinaryDecoding/gob.Decode-4 1311 922829 ns/op 427914 B/op 7883 allocs/op
|
||||
BenchmarkBinaryDecoding/binary.Unmarshal-4 13438 89201 ns/op 114576 B/op 1592 allocs/op
|
||||
BenchmarkBinaryDecoding/binary.UnmarshalBinary-4 14200 84410 ns/op 104848 B/op 1478 allocs/op
|
||||
BenchmarkBinaryDecoding/easyjson.Unmarshal+sig-4 259 4720044 ns/op 588309 B/op 1920 allocs/op
|
||||
BenchmarkBinaryDecoding/binary.Unmarshal+sig-4 271 4514978 ns/op 445286 B/op 1928 allocs/op
|
||||
BenchmarkBinaryEncoding/easyjson.Marshal-4 12756 109437 ns/op 66058 B/op 227 allocs/op
|
||||
BenchmarkBinaryEncoding/gob.Encode-4 3807 367426 ns/op 171456 B/op 1501 allocs/op
|
||||
BenchmarkBinaryEncoding/binary.Marshal-4 2568 486766 ns/op 2736133 B/op 37 allocs/op
|
||||
BenchmarkBinaryEncoding/binary.MarshalBinary-4 2150 525876 ns/op 2736135 B/op 37 allocs/op
|
||||
BenchmarkBinaryDecoding/easyjson.Unmarshal-4 13719 92516 ns/op 82680 B/op 360 allocs/op
|
||||
BenchmarkBinaryDecoding/gob.Decode-4 938 1469278 ns/op 386459 B/op 8549 allocs/op
|
||||
BenchmarkBinaryDecoding/binary.Unmarshal-4 49454 29724 ns/op 21776 B/op 282 allocs/op
|
||||
BenchmarkBinaryDecoding/binary.UnmarshalBinary-4 230827 6876 ns/op 2832 B/op 60 allocs/op
|
||||
BenchmarkBinaryDecoding/easyjson.Unmarshal+sig-4 177 7038434 ns/op 209834 B/op 939 allocs/op
|
||||
BenchmarkBinaryDecoding/binary.Unmarshal+sig-4 180 6727125 ns/op 148841 B/op 861 allocs/op
|
||||
PASS
|
||||
ok github.com/nbd-wtf/go-nostr/binary 15.109s
|
||||
ok github.com/nbd-wtf/go-nostr/binary 16.937s
|
||||
```
|
||||
|
||||
This is 2~5x faster than [NSON](../nson) decoding, which means 8x faster than default easyjson decoding,
|
||||
|
@@ -3,6 +3,7 @@ package binary
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
)
|
||||
@@ -21,7 +22,7 @@ func UnmarshalBinary(data []byte, evt *Event) (err error) {
|
||||
evt.CreatedAt = nostr.Timestamp(binary.BigEndian.Uint32(data[128:132]))
|
||||
evt.Kind = binary.BigEndian.Uint16(data[132:134])
|
||||
contentLength := int(binary.BigEndian.Uint16(data[134:136]))
|
||||
evt.Content = string(data[136 : 136+contentLength])
|
||||
evt.Content = unsafe.String(&data[136], contentLength)
|
||||
|
||||
curr := 136 + contentLength
|
||||
|
||||
@@ -37,10 +38,9 @@ func UnmarshalBinary(data []byte, evt *Event) (err error) {
|
||||
curr = curr + 1
|
||||
itemSize := int(binary.BigEndian.Uint16(data[curr : curr+2]))
|
||||
itemStart := curr + 2
|
||||
itemEnd := itemStart + itemSize
|
||||
item := string(data[itemStart:itemEnd])
|
||||
item := unsafe.String(&data[itemStart], itemSize)
|
||||
tag[i] = item
|
||||
curr = itemEnd
|
||||
curr = itemStart + itemSize
|
||||
}
|
||||
evt.Tags[t] = tag
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
)
|
||||
@@ -22,7 +23,7 @@ func Unmarshal(data []byte, evt *nostr.Event) (err error) {
|
||||
evt.CreatedAt = nostr.Timestamp(binary.BigEndian.Uint32(data[128:132]))
|
||||
evt.Kind = int(binary.BigEndian.Uint16(data[132:134]))
|
||||
contentLength := int(binary.BigEndian.Uint16(data[134:136]))
|
||||
evt.Content = string(data[136 : 136+contentLength])
|
||||
evt.Content = unsafe.String(&data[136], contentLength)
|
||||
|
||||
curr := 136 + contentLength
|
||||
|
||||
@@ -38,10 +39,9 @@ func Unmarshal(data []byte, evt *nostr.Event) (err error) {
|
||||
curr = curr + 1
|
||||
itemSize := int(binary.BigEndian.Uint16(data[curr : curr+2]))
|
||||
itemStart := curr + 2
|
||||
itemEnd := itemStart + itemSize
|
||||
item := string(data[itemStart:itemEnd])
|
||||
item := unsafe.String(&data[itemStart], itemSize)
|
||||
tag[i] = item
|
||||
curr = itemEnd
|
||||
curr = itemStart + itemSize
|
||||
}
|
||||
evt.Tags[t] = tag
|
||||
}
|
||||
|
Reference in New Issue
Block a user