From 58d98f82888345a49f2e7660a0a5b35a5da891f5 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Wed, 17 Feb 2016 14:28:59 -0500 Subject: [PATCH] Use pool for pubsub `Publish`'s waitgroups benchmark old ns/op new ns/op delta BenchmarkPubSub-8 1036494796 1032443513 -0.39% benchmark old allocs new allocs delta BenchmarkPubSub-8 2467 1441 -41.59% benchmark old bytes new bytes delta BenchmarkPubSub-8 212216 187792 -11.51% Signed-off-by: Brian Goff --- pkg/pubsub/publisher.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/pubsub/publisher.go b/pkg/pubsub/publisher.go index 22be5b757b..9d2ae42fa7 100644 --- a/pkg/pubsub/publisher.go +++ b/pkg/pubsub/publisher.go @@ -5,6 +5,8 @@ import ( "time" ) +var wgPool = sync.Pool{New: func() interface{} { return new(sync.WaitGroup) }} + // NewPublisher creates a new pub/sub publisher to broadcast messages. // The duration is used as the send timeout as to not block the publisher publishing // messages to other clients if one client is slow or unresponsive. @@ -69,12 +71,13 @@ func (p *Publisher) Publish(v interface{}) { return } - wg := new(sync.WaitGroup) + wg := wgPool.Get().(*sync.WaitGroup) for sub, topic := range p.subscribers { wg.Add(1) go p.sendTopic(sub, topic, v, wg) } wg.Wait() + wgPool.Put(wg) p.m.RUnlock() }