From 0172d8bc6e67b26a61d6e3ba1f54f0f37f294365 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 6 Aug 2026 02:17:02 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Use=20strings.Cut=20to=20av?= =?UTF-8?q?oid=20string=20allocations=20in=20CompareVersions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: himattm <6266621+himattm@users.noreply.github.com> --- .jules/bolt.md | 4 ++++ internal/plugin/manager.go | 24 ++++++++++-------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index c214509..3743d82 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` to parse strings separated by a known single-byte delimiter causes unnecessary slice allocations, memory pressure, and slows down performance. +**Action:** When iterating over segments of a delimited string, use a `for` loop with `strings.Cut` to avoid intermediate slice allocations and improve parsing speed. diff --git a/internal/plugin/manager.go b/internal/plugin/manager.go index 7aad027..f80a50d 100644 --- a/internal/plugin/manager.go +++ b/internal/plugin/manager.go @@ -832,24 +832,20 @@ 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, ".") + for a != "" || b != "" { + var partA, partB string - maxLen := len(partsA) - if len(partsB) > maxLen { - maxLen = len(partsB) - } - - for i := 0; i < maxLen; i++ { - var numA, numB int - if i < len(partsA) { - // Bolt optimization: using a custom loop is ~14x faster than fmt.Sscanf - numA = parseLenientAtoi(partsA[i]) + if a != "" { + partA, a, _ = strings.Cut(a, ".") } - if i < len(partsB) { - numB = parseLenientAtoi(partsB[i]) + if b != "" { + partB, b, _ = strings.Cut(b, ".") } + // Bolt optimization: using a custom loop is ~14x faster than fmt.Sscanf + numA := parseLenientAtoi(partA) + numB := parseLenientAtoi(partB) + if numA < numB { return -1 }