reduce allocations at subscription.GetID() because why not.

This commit is contained in:
fiatjaf
2024-09-29 07:55:06 -03:00
parent 7e0f1bd43c
commit 24343dbbef
3 changed files with 32 additions and 17 deletions

View File

@@ -10,7 +10,7 @@ import (
type Subscription struct {
label string
counter int
counter int64
Relay *Relay
Filters Filters
@@ -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() {
@@ -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.