From 7c1012c247ece8735b6857ae853f95a67cc0365e Mon Sep 17 00:00:00 2001 From: vyzo Date: Sun, 26 Aug 2018 13:19:05 +0300 Subject: [PATCH] optimize fast path for single topic validator so that we don't do the goroutine dance for just a single validator, which ought to be the most common case. --- pubsub.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pubsub.go b/pubsub.go index e44c467..4ec5a3c 100644 --- a/pubsub.go +++ b/pubsub.go @@ -506,6 +506,10 @@ func (p *PubSub) validateSignature(msg *Message) bool { } func (p *PubSub) validateTopic(vals []*topicVal, msg *Message) bool { + if len(vals) == 1 { + return p.validateSingleTopic(vals[0], msg) + } + ctx, cancel := context.WithCancel(p.ctx) defer cancel() @@ -545,6 +549,24 @@ loop: return true } +// fast path for single topic validation that avoids the extra goroutine +func (p *PubSub) validateSingleTopic(val *topicVal, msg *Message) bool { + select { + case val.validateThrottle <- struct{}{}: + ctx, cancel := context.WithCancel(p.ctx) + defer cancel() + + res := val.validateMsg(ctx, msg) + <-val.validateThrottle + + return res + + default: + log.Debugf("validation throttled for topic %s", val.topic) + return false + } +} + func (p *PubSub) publishMessage(from peer.ID, pmsg *pb.Message) { p.notifySubs(pmsg) p.rt.Publish(from, pmsg)