From 62f623403fd6bc793f299055f60aa6d44f138a09 Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Sat, 30 Sep 2023 06:25:02 +1000 Subject: [PATCH] Use static initialisation for regular expressions to optimize (#551) --- bridge/mattermost/mattermost.go | 5 +++-- mm-go-irckit/server_commands.go | 15 +++++++++------ mm-go-irckit/user.go | 9 ++++++--- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index 361c3555..a0496644 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -840,6 +840,8 @@ func (m *Mattermost) addParentMsg(parentID string, msg string, newLen int, uncou return strings.TrimRight(msg, "\n") + replyMessage, nil } +var validIRCNickRegExp = regexp.MustCompile("^[a-zA-Z0-9_]*$") + //nolint:funlen,gocognit,gocyclo,cyclop,forcetypeassert func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) { var data model.Post @@ -890,8 +892,7 @@ func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) { if overrideUsername != "" { logger.Debugf("found override username %s", overrideUsername) // only allow valid irc nicks - re := regexp.MustCompile("^[a-zA-Z0-9_]*$") - if re.MatchString(overrideUsername) { + if validIRCNickRegExp.MatchString(overrideUsername) { ghost.Nick = overrideUsername ghost.Me = false } diff --git a/mm-go-irckit/server_commands.go b/mm-go-irckit/server_commands.go index 2f0c69be..c3b30794 100644 --- a/mm-go-irckit/server_commands.go +++ b/mm-go-irckit/server_commands.go @@ -460,9 +460,10 @@ func CmdPrivMsg(s Server, u *User, msg *irc.Message) error { return s.EncodeMessage(u, irc.ERR_NOSUCHNICK, msg.Params, "No such nick/channel") } +var parseReactionToMsgRegExp = regexp.MustCompile(`^\@\@([0-9a-f]{3}|[0-9a-z]{26})\s+([\-\+]):(\S+):\s*$`) + func parseReactionToMsg(u *User, msg *irc.Message, channelID string) bool { - re := regexp.MustCompile(`^\@\@([0-9a-f]{3}|[0-9a-z]{26})\s+([\-\+]):(\S+):\s*$`) - matches := re.FindStringSubmatch(msg.Trailing) + matches := parseReactionToMsgRegExp.FindStringSubmatch(msg.Trailing) if len(matches) != 4 { return false } @@ -503,9 +504,10 @@ func parseReactionToMsg(u *User, msg *irc.Message, channelID string) bool { return true } +var parseModifyMsgRegExp = regexp.MustCompile(`^s(\/(?:[0-9a-f]{3}|[0-9a-z]{26}|!!)?\/)(.*)`) + func parseModifyMsg(u *User, msg *irc.Message, channelID string) bool { - re := regexp.MustCompile(`^s(\/(?:[0-9a-f]{3}|[0-9a-z]{26}|!!)?\/)(.*)`) - matches := re.FindStringSubmatch(msg.Trailing) + matches := parseModifyMsgRegExp.FindStringSubmatch(msg.Trailing) text := msg.Trailing // only two so s/xxx/ which means a delete @@ -576,9 +578,10 @@ func parseModifyMsg(u *User, msg *irc.Message, channelID string) bool { return true } +var parseThreadIDRegExp = regexp.MustCompile(`(?s)^\@\@(?:(!!|[0-9a-f]{3}|[0-9a-z]{26})\s)(.*)`) + func parseThreadID(u *User, msg *irc.Message, channelID string) (string, string) { - re := regexp.MustCompile(`(?s)^\@\@(?:(!!|[0-9a-f]{3}|[0-9a-z]{26})\s)(.*)`) - matches := re.FindStringSubmatch(msg.Trailing) + matches := parseThreadIDRegExp.FindStringSubmatch(msg.Trailing) if len(matches) == 0 { return "", "" } diff --git a/mm-go-irckit/user.go b/mm-go-irckit/user.go index 239123b7..cf9eb2fc 100644 --- a/mm-go-irckit/user.go +++ b/mm-go-irckit/user.go @@ -153,6 +153,11 @@ func (u *User) Encode(msgs ...*irc.Message) (err error) { return nil } +var ( + replyRegExp = regexp.MustCompile(`\@\@(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)\s`) + modifyRegExp = regexp.MustCompile(`^s/(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)?/`) +) + // Decode will receive and return a decoded message, or an error. // nolint:funlen,gocognit,gocyclo func (u *User) Decode() { @@ -181,9 +186,7 @@ func (u *User) Decode() { // start timer now t.Reset(time.Duration(bufferTimeout) * time.Millisecond) } else { - replyRe := regexp.MustCompile(`\@\@(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)\s`) - modifyRe := regexp.MustCompile(`^s/(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)?/`) - if strings.HasPrefix(msg.Trailing, "\x01ACTION") || replyRe.MatchString(msg.Trailing) || modifyRe.MatchString(msg.Trailing) { + if strings.HasPrefix(msg.Trailing, "\x01ACTION") || replyRegExp.MatchString(msg.Trailing) || modifyRegExp.MatchString(msg.Trailing) { // flush buffer logger.Debug("flushing buffer because of /me, replies to threads, and message modifications") u.BufferedMsg.Trailing = strings.TrimSpace(u.BufferedMsg.Trailing)