From 49600bfc482b232a98a676d24441dfcfb3e1c8d7 Mon Sep 17 00:00:00 2001 From: xtaci Date: Mon, 5 Jun 2017 13:24:01 +0800 Subject: [PATCH] cache the session for last packet, without querying map. --- sess.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/sess.go b/sess.go index fc43969..4ca7e39 100644 --- a/sess.go +++ b/sess.go @@ -684,6 +684,10 @@ type ( // monitor incoming data for all connections of server func (l *Listener) monitor() { + // cache last session + var lastAddr string + var lastSession *UDPSession + chPacket := make(chan inPacket, qlen) go l.receiver(chPacket) for { @@ -709,7 +713,20 @@ func (l *Listener) monitor() { if dataValid { addr := from.String() - s, ok := l.sessions[addr] + var s *UDPSession + var ok bool + + // packets received from an address always come in batch. + // cache the session for next packet, without querying map. + if addr == lastAddr { + s, ok = lastSession, true + } else { + if s, ok = l.sessions[addr]; ok { + lastSession = s + lastAddr = addr + } + } + if !ok { // new session if len(l.chAccepts) < cap(l.chAccepts) { // do not let new session overwhelm accept queue var conv uint32