diff --git a/.jules/bolt.md b/.jules/bolt.md index c214509..849638d 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -21,3 +21,7 @@ ## 2025-05-10 - fmt.Sscanf is lenient, strconv.Atoi is strict **Learning:** In Go, `fmt.Sscanf("%d")` parses digits until it encounters a non-digit character (e.g., `"2beta"` parses as `2`), while `strconv.Atoi` fails and returns `0` for the entire string. If you need the lenient parsing behavior of `fmt.Sscanf` for performance optimization, implement a custom byte-traversal loop to extract leading digits rather than relying on `strconv.Atoi` or regex, as it is over 10x faster and maintains exact functional parity. **Action:** When replacing `fmt.Sscanf` for performance, always evaluate whether the leniency of the parser is being implicitly relied upon by the surrounding code. + +## 2024-05-22 - Avoid strings.Split in hot loops +**Learning:** Using `strings.Split` in tight parsing loops (like version string comparisons) causes unnecessary slice allocations and degrades performance. +**Action:** To avoid this overhead and improve speed, use a `for` loop with `strings.Cut` to incrementally consume and parse the delimited string without allocating intermediate slices. diff --git a/internal/plugin/manager.go b/internal/plugin/manager.go index 7aad027..571a0b5 100644 --- a/internal/plugin/manager.go +++ b/internal/plugin/manager.go @@ -832,22 +832,22 @@ func parseLenientAtoi(s string) int { // CompareVersions compares two semver strings // Returns -1 if a < b, 0 if a == b, 1 if a > b func CompareVersions(a, b string) int { - partsA := strings.Split(a, ".") - partsB := strings.Split(b, ".") - - maxLen := len(partsA) - if len(partsB) > maxLen { - maxLen = len(partsB) - } + for a != "" || b != "" { + var partA, partB string + if a != "" { + partA, a, _ = strings.Cut(a, ".") + } + if b != "" { + partB, b, _ = strings.Cut(b, ".") + } - for i := 0; i < maxLen; i++ { var numA, numB int - if i < len(partsA) { + if partA != "" { // Bolt optimization: using a custom loop is ~14x faster than fmt.Sscanf - numA = parseLenientAtoi(partsA[i]) + numA = parseLenientAtoi(partA) } - if i < len(partsB) { - numB = parseLenientAtoi(partsB[i]) + if partB != "" { + numB = parseLenientAtoi(partB) } if numA < numB { @@ -857,6 +857,5 @@ func CompareVersions(a, b string) int { return 1 } } - return 0 }