From dc317f89834e1ceae1a2280dc99a906957b5e003 Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Fri, 23 Jun 2023 23:43:43 +0200 Subject: [PATCH 1/2] feat: optimisation of EqualMatch method - Combined whitespace stripping: The leading and trailing whitespaces are stripped in a single step, reducing the number of method calls and condition checks. - Reduced string conversions: Instead of converting the entire text and pattern to strings, only the necessary substrings are used. This avoids unnecessary conversions and improves performance. - Early return: The function returns early if the normalized characters or string comparison fails, eliminating the need for the match boolean variable and reducing unnecessary computations. --- src/algo/algo.go | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/algo/algo.go b/src/algo/algo.go index 1d021ed0..a958d851 100644 --- a/src/algo/algo.go +++ b/src/algo/algo.go @@ -902,14 +902,11 @@ func EqualMatch(caseSensitive bool, normalize bool, forward bool, text *util.Cha return Result{-1, -1, 0}, nil } - // Strip leading whitespaces - trimmedLen := 0 + // Strip leading and trailing whitespaces + trimmedLen, trimmedEndLen := 0, 0 if !unicode.IsSpace(pattern[0]) { trimmedLen = text.LeadingWhitespaces() } - - // Strip trailing whitespaces - trimmedEndLen := 0 if !unicode.IsSpace(pattern[lenPattern-1]) { trimmedEndLen = text.TrailingWhitespaces() } @@ -917,30 +914,28 @@ func EqualMatch(caseSensitive bool, normalize bool, forward bool, text *util.Cha if text.Length()-trimmedLen-trimmedEndLen != lenPattern { return Result{-1, -1, 0}, nil } - match := true + + textRunes := text.ToRunes() if normalize { - runes := text.ToRunes() for idx, pchar := range pattern { - char := runes[trimmedLen+idx] + char := textRunes[trimmedLen+idx] if !caseSensitive { char = unicode.To(unicode.LowerCase, char) } if normalizeRune(pchar) != normalizeRune(char) { - match = false - break + return Result{-1, -1, 0}, nil } } } else { - runes := text.ToRunes() - runesStr := string(runes[trimmedLen : len(runes)-trimmedEndLen]) + runesStr := string(textRunes[trimmedLen : len(textRunes)-trimmedEndLen]) if !caseSensitive { runesStr = strings.ToLower(runesStr) } - match = runesStr == string(pattern) - } - if match { - return Result{trimmedLen, trimmedLen + lenPattern, (scoreMatch+int(bonusBoundaryWhite))*lenPattern + - (bonusFirstCharMultiplier-1)*int(bonusBoundaryWhite)}, nil + if runesStr != string(pattern) { + return Result{-1, -1, 0}, nil + } } - return Result{-1, -1, 0}, nil + + score := (scoreMatch + int(bonusBoundaryWhite)) * lenPattern + (bonusFirstCharMultiplier-1) * int(bonusBoundaryWhite) + return Result{trimmedLen, trimmedLen + lenPattern, score}, nil } From 444d738f8e4c3969939b2e33d11b69dcce671447 Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Fri, 23 Jun 2023 23:57:57 +0200 Subject: [PATCH 2/2] feat: format --- src/algo/algo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algo/algo.go b/src/algo/algo.go index a958d851..d9e08a03 100644 --- a/src/algo/algo.go +++ b/src/algo/algo.go @@ -936,6 +936,6 @@ func EqualMatch(caseSensitive bool, normalize bool, forward bool, text *util.Cha } } - score := (scoreMatch + int(bonusBoundaryWhite)) * lenPattern + (bonusFirstCharMultiplier-1) * int(bonusBoundaryWhite) + score := (scoreMatch+int(bonusBoundaryWhite))*lenPattern + (bonusFirstCharMultiplier-1)*int(bonusBoundaryWhite) return Result{trimmedLen, trimmedLen + lenPattern, score}, nil }