Skip to content

Commit ca9aecd

Browse files
committed
perf: various optimizations
1 parent 934757a commit ca9aecd

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

event.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"strings"
66
)
77

8+
//nolint:gochecknoglobals
9+
var dataReplacer = strings.NewReplacer("\r\n", "\ndata: ", "\r", "\ndata: ", "\n", "\ndata: ")
10+
811
// Event is the actual Server Sent Event that will be dispatched.
912
type Event struct {
1013
// The updates' data, encoded in the sever-sent event format: every line starts with the string "data: "
@@ -33,8 +36,7 @@ func (e *Event) String() string {
3336
_, _ = fmt.Fprintf(&b, "retry: %d\n", e.Retry)
3437
}
3538

36-
r := strings.NewReplacer("\r\n", "\ndata: ", "\r", "\ndata: ", "\n", "\ndata: ")
37-
_, _ = fmt.Fprintf(&b, "id: %s\ndata: %s\n\n", e.ID, r.Replace(e.Data))
39+
_, _ = fmt.Fprintf(&b, "id: %s\ndata: %s\n\n", e.ID, dataReplacer.Replace(e.Data))
3840

3941
return b.String()
4042
}

subscriber.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func (s *Subscriber) MatchTopics(topics []string, private bool) bool {
5959
if s.topicSelectorStore.match(topic, ts) {
6060
subscribed = true
6161

62+
if canAccess {
63+
return true
64+
}
65+
6266
break
6367
}
6468
}
@@ -69,6 +73,10 @@ func (s *Subscriber) MatchTopics(topics []string, private bool) bool {
6973
if s.topicSelectorStore.match(topic, ts) {
7074
canAccess = true
7175

76+
if subscribed {
77+
return true
78+
}
79+
7280
break
7381
}
7482
}

topicselector.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@ func (tss *TopicSelectorStore) match(topic, topicSelector string) bool {
2626
}
2727

2828
var k string
29+
2930
if tss.cache != nil {
30-
k = "m_" + topicSelector + "_" + topic
31+
var b strings.Builder
32+
b.Grow(3 + len(topicSelector) + len(topic))
33+
b.WriteString("m_")
34+
b.WriteString(topicSelector)
35+
b.WriteByte('_')
36+
b.WriteString(topic)
37+
k = b.String()
3138

3239
value, found := tss.cache.Get(k)
3340
if found {

0 commit comments

Comments
 (0)