Skip to content

Commit

Permalink
perf(router): added a fixed event map to avoid reallocating and rebal…
Browse files Browse the repository at this point in the history
…ancing in map
  • Loading branch information
geolffreym committed Apr 22, 2024
1 parent ef062f7 commit b10d833
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ type broker struct {
topics topics // topic subscriptions
}

func newBroker() *broker {
return &broker{make(topics)}
func newBroker(size int) *broker {
return &broker{make(topics, size)}
}

// Register associate subscriber to broker topics.
Expand Down
12 changes: 6 additions & 6 deletions broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestRegister(t *testing.T) {
event := newBroker()
event := newBroker(4)
subscriber := newSubscriber()
event.Register(NewPeerDetected, subscriber)
event.Register(PeerDisconnected, subscriber)
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestRegister(t *testing.T) {
}

func TestUnregister(t *testing.T) {
broker := newBroker()
broker := newBroker(4)
subscriber := newSubscriber()
broker.Register(MessageReceived, subscriber)
broker.Register(NewPeerDetected, subscriber)
Expand All @@ -61,7 +61,7 @@ func TestUnregister(t *testing.T) {
}

func TestUnregisterExpectedLen(t *testing.T) {
broker := newBroker()
broker := newBroker(4)
subscriber := newSubscriber()
broker.Register(MessageReceived, subscriber)
broker.Register(NewPeerDetected, subscriber)
Expand All @@ -75,7 +75,7 @@ func TestUnregisterExpectedLen(t *testing.T) {
}

func TestInvalidUnregister(t *testing.T) {
broker := newBroker()
broker := newBroker(4)
subscriber := newSubscriber()
// Remove self listening from broker events
success := broker.Unregister(MessageReceived, subscriber)
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestTopicRemoveInvalid(t *testing.T) {
func TestPublish(t *testing.T) {
var result Signal
subscriber := newSubscriber()
broker := newBroker()
broker := newBroker(4)

session := mockSession(&mockConn{}, PeerAPb)
header1 := header{newPeer(session), NewPeerDetected}
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestPublish(t *testing.T) {
}

func TestInvalidPublish(t *testing.T) {
broker := newBroker()
broker := newBroker(4)
session := mockSession(&mockConn{}, PeerAPb)
header1 := header{newPeer(session), NewPeerDetected}
signaling := Signal{header1, ""}
Expand Down
5 changes: 4 additions & 1 deletion events.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ type events struct {

func newEvents() *events {
subscriber := newSubscriber()
broker := newBroker()
// !IMPORTANT if new events are added the size should be equal to new events number.
// we need only 4 spaces one for each event, adding this avoids potential map growth.
// https://100go.co/#inefficient-map-initialization-27
broker := newBroker(4)
// register default events
broker.Register(NewPeerDetected, subscriber)
broker.Register(MessageReceived, subscriber)
Expand Down

0 comments on commit b10d833

Please sign in to comment.