From 65834c0729abe9ed69544819c32a94416e2a44a9 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Fri, 2 Aug 2019 00:20:31 -0400 Subject: [PATCH] optimize Sessions() at the expense of AddSession() --- irc/getters.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/irc/getters.go b/irc/getters.go index c6a0d208..4c26cfe9 100644 --- a/irc/getters.go +++ b/irc/getters.go @@ -43,8 +43,7 @@ func (server *Server) Languages() (lm *languages.Manager) { func (client *Client) Sessions() (sessions []*Session) { client.stateMutex.RLock() - sessions = make([]*Session, len(client.sessions)) - copy(sessions, client.sessions) + sessions = client.sessions client.stateMutex.RUnlock() return } @@ -102,7 +101,10 @@ func (client *Client) AddSession(session *Session) (success bool) { } // success, attach the new session to the client session.client = client - client.sessions = append(client.sessions, session) + newSessions := make([]*Session, len(client.sessions)+1) + copy(newSessions, client.sessions) + newSessions[len(newSessions)-1] = session + client.sessions = newSessions return true }