From a6e4a26cbbcac4f813004862b23d4ec725d89810 Mon Sep 17 00:00:00 2001 From: Alex Jaspersen Date: Fri, 8 May 2020 01:55:47 +0000 Subject: [PATCH] Performance optimization for determining friends in WHO . Construct a hash set of the user's channels and check that rather than querying channel membership, to reduce the number of locks that need to be acquired. --- irc/client.go | 15 --------------- irc/handlers.go | 22 +++++++++++++++++++++- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/irc/client.go b/irc/client.go index eb44cc9b..b636678a 100644 --- a/irc/client.go +++ b/irc/client.go @@ -1022,21 +1022,6 @@ func (client *Client) ModeString() (str string) { return "+" + client.modes.String() } -// IsFriend() returns true if the given otherClient shares a channel with this client, or if they are the same user. -func (client *Client) IsFriend(otherClient *Client) bool { - if client == otherClient { - return true - } - - for _, channel := range client.Channels() { - if channel.hasClient(otherClient) { - return true - } - } - - return false -} - // Friends refers to clients that share a channel with this client. func (client *Client) Friends(capabs ...caps.Capability) (result map[*Session]bool) { result = make(map[*Session]bool) diff --git a/irc/handlers.go b/irc/handlers.go index fd53dd41..c337f88f 100644 --- a/irc/handlers.go +++ b/irc/handlers.go @@ -2636,8 +2636,28 @@ func whoHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Respo } } } else { + // Construct set of channels the client is in. + userChannels := make(map[*Channel]bool) + for _, channel := range client.Channels() { + userChannels[channel] = true + } + + // Another client is a friend if they share at least one channel, or they are the same client. + isFriend := func(otherClient *Client) bool { + if client == otherClient { + return true + } + + for _, channel := range otherClient.Channels() { + if userChannels[channel] { + return true + } + } + return false + } + for mclient := range server.clients.FindAll(mask) { - if isOper || !mclient.HasMode(modes.Invisible) || mclient.IsFriend(client) { + if isOper || !mclient.HasMode(modes.Invisible) || isFriend(mclient) { client.rplWhoReply(nil, mclient, rb) } }