3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

Merge pull request #615 from slingamn/sessions_optimization

optimize Sessions() at the expense of AddSession()
This commit is contained in:
Daniel Oaks 2019-08-25 09:57:51 +10:00 committed by GitHub
commit 014a5fe63c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}