Skip to content

Commit

Permalink
reduce allocations at subscription.GetID() because why not.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Sep 29, 2024
1 parent 7e0f1bd commit 24343db
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
7 changes: 7 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package nostr

import (
"strconv"
"strings"
"sync"
"unsafe"

Expand Down Expand Up @@ -92,3 +94,8 @@ func arePointerValuesEqual[V comparable](a *V, b *V) bool {
}
return false
}

func subIdToSerial(subId string) int64 {
serialId, _ := strconv.ParseInt(subId[0:strings.Index(subId, ":")], 10, 64)
return serialId
}
31 changes: 17 additions & 14 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

type Status int

var subscriptionIDCounter atomic.Int32
var subscriptionIDCounter atomic.Int64

type Relay struct {
closeMutex sync.Mutex
Expand All @@ -27,7 +27,7 @@ type Relay struct {
RequestHeader http.Header // e.g. for origin header

Connection *Connection
Subscriptions *xsync.MapOf[string, *Subscription]
Subscriptions *xsync.MapOf[int64, *Subscription]

ConnectionError error
connectionContext context.Context // will be canceled when the connection closes
Expand Down Expand Up @@ -57,7 +57,7 @@ func NewRelay(ctx context.Context, url string, opts ...RelayOption) *Relay {
URL: NormalizeURL(url),
connectionContext: ctx,
connectionContextCancel: cancel,
Subscriptions: xsync.NewMapOf[string, *Subscription](),
Subscriptions: xsync.NewMapOf[int64, *Subscription](),
okCallbacks: xsync.NewMapOf[string, func(bool, string)](),
writeQueue: make(chan writeRequest),
subscriptionChannelCloseQueue: make(chan *Subscription),
Expand Down Expand Up @@ -171,10 +171,9 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
r.Connection = nil

// close all subscriptions
r.Subscriptions.Range(func(_ string, sub *Subscription) bool {
go sub.Unsub()
return true
})
for _, sub := range r.Subscriptions.Range {
sub.Unsub()
}
}()

// queue all write operations here so we don't do mutex spaghetti
Expand Down Expand Up @@ -241,7 +240,8 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
if env.SubscriptionID == nil {
continue
}
if subscription, ok := r.Subscriptions.Load(*env.SubscriptionID); !ok {

if subscription, ok := r.Subscriptions.Load(subIdToSerial(*env.SubscriptionID)); !ok {
// InfoLogger.Printf("{%s} no subscription with id '%s'\n", r.URL, *env.SubscriptionID)
continue
} else {
Expand All @@ -263,15 +263,15 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
subscription.dispatchEvent(&env.Event)
}
case *EOSEEnvelope:
if subscription, ok := r.Subscriptions.Load(string(*env)); ok {
if subscription, ok := r.Subscriptions.Load(subIdToSerial(string(*env))); ok {
subscription.dispatchEose()
}
case *ClosedEnvelope:
if subscription, ok := r.Subscriptions.Load(string(env.SubscriptionID)); ok {
if subscription, ok := r.Subscriptions.Load(subIdToSerial(env.SubscriptionID)); ok {
subscription.dispatchClosed(env.Reason)
}
case *CountEnvelope:
if subscription, ok := r.Subscriptions.Load(string(env.SubscriptionID)); ok && env.Count != nil && subscription.countResult != nil {
if subscription, ok := r.Subscriptions.Load(subIdToSerial(env.SubscriptionID)); ok && env.Count != nil && subscription.countResult != nil {
subscription.countResult <- *env.Count
}
case *OKEnvelope:
Expand Down Expand Up @@ -400,7 +400,7 @@ func (r *Relay) PrepareSubscription(ctx context.Context, filters Filters, opts .
Relay: r,
Context: ctx,
cancel: cancel,
counter: int(current),
counter: current,
Events: make(chan *Event),
EndOfStoredEvents: make(chan struct{}, 1),
ClosedReason: make(chan string, 1),
Expand All @@ -415,8 +415,7 @@ func (r *Relay) PrepareSubscription(ctx context.Context, filters Filters, opts .
}
}

id := sub.GetID()
r.Subscriptions.Store(id, sub)
r.Subscriptions.Store(int64(sub.counter), sub)

// start handling events, eose, unsub etc:
go sub.start()
Expand Down Expand Up @@ -514,3 +513,7 @@ func (r *Relay) Close() error {

return nil
}

var subIdPool = sync.Pool{
New: func() any { return make([]byte, 0, 15) },
}
11 changes: 8 additions & 3 deletions subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type Subscription struct {
label string
counter int
counter int64

Relay *Relay
Filters Filters
Expand Down Expand Up @@ -65,7 +65,12 @@ var _ SubscriptionOption = (WithLabel)("")
// GetID return the Nostr subscription ID as given to the Relay
// it is a concatenation of the label and a serial number.
func (sub *Subscription) GetID() string {
return sub.label + ":" + strconv.Itoa(sub.counter)
buf := subIdPool.Get().([]byte)
buf = strconv.AppendInt(buf, sub.counter, 10)
buf = append(buf, ':')
buf = append(buf, sub.label...)
defer subIdPool.Put(buf)
return string(buf)
}

func (sub *Subscription) start() {
Expand Down Expand Up @@ -133,7 +138,7 @@ func (sub *Subscription) Unsub() {
}

// remove subscription from our map
sub.Relay.Subscriptions.Delete(sub.GetID())
sub.Relay.Subscriptions.Delete(sub.counter)
}

// Close just sends a CLOSE message. You probably want Unsub() instead.
Expand Down

0 comments on commit 24343db

Please sign in to comment.