Bolt: Optimize hasAttachments in routing by bypassing strings.ToLower for most inputs

Co-authored-by: hobbyistlabs-coder <267281733+hobbyistlabs-coder@users.noreply.github.com>
This commit is contained in:
google-labs-jules[bot] 2026-03-17 17:48:50 +00:00
parent 9fb071836b
commit b040463cd3
12 changed files with 70 additions and 53 deletions

View file

@ -4,3 +4,7 @@
## 2024-05-25 - Efficient String Building in Loops ## 2024-05-25 - Efficient String Building in Loops
**Learning:** In Go, string concatenation (`+=`) in a loop leads to $O(N^2)$ complexity due to immutability. Using `strings.Builder` provides $O(N)$ efficiency. Additionally, `fmt.Fprintf` has overhead due to format string parsing; direct `sb.WriteString` calls are significantly faster. **Learning:** In Go, string concatenation (`+=`) in a loop leads to $O(N^2)$ complexity due to immutability. Using `strings.Builder` provides $O(N)$ efficiency. Additionally, `fmt.Fprintf` has overhead due to format string parsing; direct `sb.WriteString` calls are significantly faster.
**Action:** Use `strings.Builder` for building strings in loops and prefer direct `WriteString` calls over `fmt.Fprintf` for maximum performance in hot paths. **Action:** Use `strings.Builder` for building strings in loops and prefer direct `WriteString` calls over `fmt.Fprintf` for maximum performance in hot paths.
## 2024-05-26 - Avoid Unnecessary `strings.ToLower`
**Learning:** Calling `strings.ToLower` on the entire message content allocates a new string and iterates over all runes. This causes measurable GC pressure and latency on hot paths like feature extraction during routing.
**Action:** Use fast paths to bypass `strings.ToLower`. For instance, check if a requisite character (like a dot `.`) exists, or check common casings directly (`DATA:IMAGE` vs `data:image`) before falling back to full case-normalization.

View file

@ -105,21 +105,34 @@ func countRecentToolCalls(history []providers.Message) int {
// false negatives (missing an attachment) just mean the routing falls back to // false negatives (missing an attachment) just mean the routing falls back to
// the primary model anyway. // the primary model anyway.
func hasAttachments(msg string) bool { func hasAttachments(msg string) bool {
lower := strings.ToLower(msg) // Bolt: Fast path to avoid strings.ToLower memory allocation and full string pass
// for the vast majority of messages that contain no media.
// Base64 data URIs embedded directly in the message hasDataURI := strings.Contains(msg, "data:image/") || strings.Contains(msg, "DATA:IMAGE/") ||
if strings.Contains(lower, "data:image/") || strings.Contains(msg, "data:audio/") || strings.Contains(msg, "DATA:AUDIO/") ||
strings.Contains(lower, "data:audio/") || strings.Contains(msg, "data:video/") || strings.Contains(msg, "DATA:VIDEO/")
strings.Contains(lower, "data:video/") { if hasDataURI {
return true return true
} }
// Common image/audio extensions in URLs or file references // Extensions must have a dot
if !strings.Contains(msg, ".") {
return false
}
// Check common extensions without ToLower first to capture standard lowercase domains
mediaExts := []string{ mediaExts := []string{
".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp",
".mp3", ".wav", ".ogg", ".m4a", ".flac", ".mp3", ".wav", ".ogg", ".m4a", ".flac",
".mp4", ".avi", ".mov", ".webm", ".mp4", ".avi", ".mov", ".webm",
} }
for _, ext := range mediaExts {
if strings.Contains(msg, ext) {
return true
}
}
// Fallback to ToLower for weirdly cased extensions
lower := strings.ToLower(msg)
for _, ext := range mediaExts { for _, ext := range mediaExts {
if strings.Contains(lower, ext) { if strings.Contains(lower, ext) {
return true return true

View file

@ -259,7 +259,7 @@ func splitQuoted(s string) []string {
var quoteChar rune var quoteChar rune
for _, r := range s { for _, r := range s {
if (r == '"' || r == '\'') { if r == '"' || r == '\'' {
if inQuotes && quoteChar == r { if inQuotes && quoteChar == r {
inQuotes = false inQuotes = false
} else if !inQuotes { } else if !inQuotes {

View file

@ -2,8 +2,8 @@ package tools
import ( import (
"context" "context"
"testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing"
) )
func TestSplitQuoted(t *testing.T) { func TestSplitQuoted(t *testing.T) {

View file

@ -1,12 +1,12 @@
package web package web
import ( import (
"jane/pkg/tools"
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"jane/pkg/tools"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"

View file

@ -1,9 +1,9 @@
package web package web
import ( import (
"jane/pkg/tools"
"context" "context"
"fmt" "fmt"
"jane/pkg/tools"
) )
type WebSearchTool struct { type WebSearchTool struct {