⚡ 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
|
## 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.
|
||||||
|
|
|
||||||
|
|
@ -844,15 +844,15 @@ func TestTargetReasoningChannelID_AllChannels(t *testing.T) {
|
||||||
t.Fatalf("Failed to create channel manager: %v", err)
|
t.Fatalf("Failed to create channel manager: %v", err)
|
||||||
}
|
}
|
||||||
for name, id := range map[string]string{
|
for name, id := range map[string]string{
|
||||||
"whatsapp": "rid-whatsapp",
|
"whatsapp": "rid-whatsapp",
|
||||||
"telegram": "rid-telegram",
|
"telegram": "rid-telegram",
|
||||||
"discord": "rid-discord",
|
"discord": "rid-discord",
|
||||||
"maixcam": "rid-maixcam",
|
"maixcam": "rid-maixcam",
|
||||||
"qq": "rid-qq",
|
"qq": "rid-qq",
|
||||||
"dingtalk": "rid-dingtalk",
|
"dingtalk": "rid-dingtalk",
|
||||||
"slack": "rid-slack",
|
"slack": "rid-slack",
|
||||||
"line": "rid-line",
|
"line": "rid-line",
|
||||||
"onebot": "rid-onebot",
|
"onebot": "rid-onebot",
|
||||||
} {
|
} {
|
||||||
chManager.RegisterChannel(name, &fakeChannel{id: id})
|
chManager.RegisterChannel(name, &fakeChannel{id: id})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,18 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
type ChannelsConfig struct {
|
type ChannelsConfig struct {
|
||||||
WhatsApp WhatsAppConfig `json:"whatsapp"`
|
WhatsApp WhatsAppConfig `json:"whatsapp"`
|
||||||
Telegram TelegramConfig `json:"telegram"`
|
Telegram TelegramConfig `json:"telegram"`
|
||||||
Discord DiscordConfig `json:"discord"`
|
Discord DiscordConfig `json:"discord"`
|
||||||
MaixCam MaixCamConfig `json:"maixcam"`
|
MaixCam MaixCamConfig `json:"maixcam"`
|
||||||
QQ QQConfig `json:"qq"`
|
QQ QQConfig `json:"qq"`
|
||||||
DingTalk DingTalkConfig `json:"dingtalk"`
|
DingTalk DingTalkConfig `json:"dingtalk"`
|
||||||
Slack SlackConfig `json:"slack"`
|
Slack SlackConfig `json:"slack"`
|
||||||
Matrix MatrixConfig `json:"matrix"`
|
Matrix MatrixConfig `json:"matrix"`
|
||||||
LINE LINEConfig `json:"line"`
|
LINE LINEConfig `json:"line"`
|
||||||
OneBot OneBotConfig `json:"onebot"`
|
OneBot OneBotConfig `json:"onebot"`
|
||||||
Pico PicoConfig `json:"pico"`
|
Pico PicoConfig `json:"pico"`
|
||||||
IRC IRCConfig `json:"irc"`
|
IRC IRCConfig `json:"irc"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GroupTriggerConfig controls when the bot responds in group chats.
|
// GroupTriggerConfig controls when the bot responds in group chats.
|
||||||
|
|
|
||||||
|
|
@ -66,12 +66,12 @@ func (rt *ResourceTracker) logResources() {
|
||||||
sysMB := float64(m.Sys) / 1024 / 1024
|
sysMB := float64(m.Sys) / 1024 / 1024
|
||||||
|
|
||||||
logger.InfoCF("SystemHealth", "Resource tracking telemetry", map[string]any{
|
logger.InfoCF("SystemHealth", "Resource tracking telemetry", map[string]any{
|
||||||
"goroutines": goroutines,
|
"goroutines": goroutines,
|
||||||
"memory_alloc_mb": allocMB,
|
"memory_alloc_mb": allocMB,
|
||||||
"memory_total_mb": totalAllocMB,
|
"memory_total_mb": totalAllocMB,
|
||||||
"memory_sys_mb": sysMB,
|
"memory_sys_mb": sysMB,
|
||||||
"num_gc": m.NumGC,
|
"num_gc": m.NumGC,
|
||||||
"gc_pause_ns": m.PauseNs[(m.NumGC+255)%256], // Latest GC pause time
|
"gc_pause_ns": m.PauseNs[(m.NumGC+255)%256], // Latest GC pause time
|
||||||
"gc_pause_total_ns": m.PauseTotalNs,
|
"gc_pause_total_ns": m.PauseTotalNs,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,9 +34,9 @@ var (
|
||||||
currentTimeFormat = "15:04:05"
|
currentTimeFormat = "15:04:05"
|
||||||
logger zerolog.Logger
|
logger zerolog.Logger
|
||||||
fileLogger zerolog.Logger
|
fileLogger zerolog.Logger
|
||||||
logFile *os.File
|
logFile *os.File
|
||||||
once sync.Once
|
once sync.Once
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
||||||
|
|
@ -13,14 +13,14 @@ var migrateableDirs = []string{
|
||||||
}
|
}
|
||||||
|
|
||||||
var supportedChannels = map[string]bool{
|
var supportedChannels = map[string]bool{
|
||||||
"whatsapp": true,
|
"whatsapp": true,
|
||||||
"telegram": true,
|
"telegram": true,
|
||||||
"discord": true,
|
"discord": true,
|
||||||
"maixcam": true,
|
"maixcam": true,
|
||||||
"qq": true,
|
"qq": true,
|
||||||
"dingtalk": true,
|
"dingtalk": true,
|
||||||
"slack": true,
|
"slack": true,
|
||||||
"matrix": true,
|
"matrix": true,
|
||||||
"line": true,
|
"line": true,
|
||||||
"onebot": true,
|
"onebot": true,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -107,8 +107,8 @@ func (t *AlpacaTool) getPrice(symbol string) *tools.ToolResult {
|
||||||
|
|
||||||
func (t *AlpacaTool) getSMA(symbol string) *tools.ToolResult {
|
func (t *AlpacaTool) getSMA(symbol string) *tools.ToolResult {
|
||||||
req := marketdata.GetBarsRequest{
|
req := marketdata.GetBarsRequest{
|
||||||
TimeFrame: marketdata.OneDay,
|
TimeFrame: marketdata.OneDay,
|
||||||
TotalLimit: 10, // 10-day simple moving average
|
TotalLimit: 10, // 10-day simple moving average
|
||||||
}
|
}
|
||||||
bars, err := t.marketData.GetBars(symbol, req)
|
bars, err := t.marketData.GetBars(symbol, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue