Files
next.orly.dev/pkg/encoders/event/canonical.go
mleku 509eb8f901 Add benchmark tests for event encoders and optimize performance
- Introduced benchmark tests for JSON and binary marshaling/unmarshaling, canonical encoding, and ID generation to assess performance.
- Implemented optimizations to reduce memory allocations and CPU processing time across various encoding methods.
- Enhanced `Marshal`, `ToCanonical`, and `MarshalBinary` methods with pre-allocation strategies to minimize reallocations.
- Added handling for nil tags to avoid unnecessary allocations during binary encoding.
- Documented performance improvements in the new PERFORMANCE_REPORT.md file, highlighting significant reductions in execution time and memory usage.
2025-11-02 17:47:40 +00:00

56 lines
1.5 KiB
Go

package event
import (
"next.orly.dev/pkg/crypto/sha256"
"next.orly.dev/pkg/encoders/hex"
"next.orly.dev/pkg/encoders/ints"
"next.orly.dev/pkg/encoders/text"
)
// ToCanonical converts the event to the canonical encoding used to derive the
// event ID.
func (ev *E) ToCanonical(dst []byte) (b []byte) {
b = dst
// Pre-allocate buffer if nil to reduce reallocations
if b == nil {
// Estimate size: [0," + hex(pubkey) + "," + timestamp + "," + kind + "," + tags + "," + content + ]
estimatedSize := 5 + 2*len(ev.Pubkey) + 20 + 10 + 100
if ev.Tags != nil {
for _, tag := range *ev.Tags {
for _, elem := range tag.T {
estimatedSize += len(elem)*2 + 10 // escaped element + overhead
}
}
}
estimatedSize += len(ev.Content)*2 + 10 // escaped content + overhead
b = make([]byte, 0, estimatedSize)
}
b = append(b, "[0,\""...)
b = hex.EncAppend(b, ev.Pubkey)
b = append(b, "\","...)
b = ints.New(ev.CreatedAt).Marshal(b)
b = append(b, ',')
b = ints.New(ev.Kind).Marshal(b)
b = append(b, ',')
if ev.Tags != nil {
b = ev.Tags.Marshal(b)
} else {
b = append(b, '[')
b = append(b, ']')
}
b = append(b, ',')
b = text.AppendQuote(b, ev.Content, text.NostrEscape)
b = append(b, ']')
return
}
// GetIDBytes returns the raw SHA256 hash of the canonical form of an event.E.
func (ev *E) GetIDBytes() []byte { return Hash(ev.ToCanonical(nil)) }
// Hash is a little helper generate a hash and return a slice instead of an
// array.
func Hash(in []byte) (out []byte) {
h := sha256.Sum256(in)
return h[:]
}