⚡ 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:
parent
9fb071836b
commit
b040463cd3
12 changed files with 70 additions and 53 deletions
|
|
@ -4,3 +4,7 @@
|
|||
## 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.
|
||||
**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.
|
||||
|
|
|
|||
|
|
@ -105,21 +105,34 @@ func countRecentToolCalls(history []providers.Message) int {
|
|||
// false negatives (missing an attachment) just mean the routing falls back to
|
||||
// the primary model anyway.
|
||||
func hasAttachments(msg string) bool {
|
||||
lower := strings.ToLower(msg)
|
||||
|
||||
// Base64 data URIs embedded directly in the message
|
||||
if strings.Contains(lower, "data:image/") ||
|
||||
strings.Contains(lower, "data:audio/") ||
|
||||
strings.Contains(lower, "data:video/") {
|
||||
// Bolt: Fast path to avoid strings.ToLower memory allocation and full string pass
|
||||
// for the vast majority of messages that contain no media.
|
||||
hasDataURI := strings.Contains(msg, "data:image/") || strings.Contains(msg, "DATA:IMAGE/") ||
|
||||
strings.Contains(msg, "data:audio/") || strings.Contains(msg, "DATA:AUDIO/") ||
|
||||
strings.Contains(msg, "data:video/") || strings.Contains(msg, "DATA:VIDEO/")
|
||||
if hasDataURI {
|
||||
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{
|
||||
".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp",
|
||||
".mp3", ".wav", ".ogg", ".m4a", ".flac",
|
||||
".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 {
|
||||
if strings.Contains(lower, ext) {
|
||||
return true
|
||||
|
|
|
|||
|
|
@ -259,7 +259,7 @@ func splitQuoted(s string) []string {
|
|||
var quoteChar rune
|
||||
|
||||
for _, r := range s {
|
||||
if (r == '"' || r == '\'') {
|
||||
if r == '"' || r == '\'' {
|
||||
if inQuotes && quoteChar == r {
|
||||
inQuotes = false
|
||||
} else if !inQuotes {
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ package tools
|
|||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSplitQuoted(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"jane/pkg/tools"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"jane/pkg/tools"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"jane/pkg/tools"
|
||||
"context"
|
||||
"fmt"
|
||||
"jane/pkg/tools"
|
||||
)
|
||||
|
||||
type WebSearchTool struct {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue