on main help sign in
perf: avoid regex compilation in runtime
commit760e5c4f1271b8813f6d2fe89e01df18dd0ececa authorRaphael Amorim <rapha850@gmail.com> dateWed 30 Jul 2025, 11:22 (1 year ago) parent229c0c7
4 files changed+23−9
internal/llm/provider/anthropic.go+6−2 internal/llm/tools/view.go+2−1 internal/tui/exp/list/filterable.go+8−3 internal/tui/exp/list/filterable_group.go+7−3
internal/llm/provider/anthropic.go+6−2
@@ -21,6 +21,11 @@ import (
2121 "github.com/charmbracelet/crush/internal/message"
2222)
2323
24+var (
25+ // Pre-compiled regex for parsing context limit errors.
26+ contextLimitRegex = regexp.MustCompile(`input length and ` + "`max_tokens`" + ` exceed context limit: (\d+) \+ (\d+) > (\d+)`)
27+)
28+
2429type anthropicClient struct {
2530 providerOptions providerClientOptions
2631 useBedrock bool
@@ -512,8 +517,7 @@ func (a *anthropicClient) handleContextLimitError(apiErr *anthropic.Error) (int,
512517 // Parse error message like: "input length and max_tokens exceed context limit: 154978 + 50000 > 200000"
513518 errorMsg := apiErr.Error()
514519
515− re := regexp.MustCompile("input length and `max_tokens` exceed context limit: (\\d+) \\+ (\\d+) > (\\d+)")
516− matches := re.FindStringSubmatch(errorMsg)
520+ matches := contextLimitRegex.FindStringSubmatch(errorMsg)
517521
518522 if len(matches) != 4 {
519523 return 0, false
internal/llm/tools/view.go+2−1
@@ -306,7 +306,8 @@ func readTextFile(filePath string, offset, limit int) (string, int, error) {
306306 }
307307 }
308308
309− var lines []string
309+ // Pre-allocate slice with expected capacity
310+ lines := make([]string, 0, limit)
310311 lineCount = offset
311312
312313 for scanner.Scan() && len(lines) < limit {
internal/tui/exp/list/filterable.go+8−3
@@ -15,6 +15,13 @@ import (
1515 "github.com/sahilm/fuzzy"
1616)
1717
18+var (
19+ // Pre-compiled regex for checking if a string contains alphabetic characters.
20+ alphaRegex = regexp.MustCompile(`[a-zA-Z]`)
21+ // Pre-compiled regex for checking if a string is alphanumeric.
22+ alphanumericRegex = regexp.MustCompile(`^[a-zA-Z0-9]*$`)
23+)
24+
1825type FilterableItem interface {
1926 Item
2027 FilterValue() string
@@ -165,8 +172,6 @@ func (f *filterableList[T]) View() string {
165172
166173// removes bindings that are used for search
167174func (f *filterableList[T]) updateKeyMaps() {
168− alphanumeric := regexp.MustCompile("^[a-zA-Z0-9]*$")
169−
170175 removeLettersAndNumbers := func(bindings []string) []string {
171176 var keep []string
172177 for _, b := range bindings {
@@ -177,7 +182,7 @@ func (f *filterableList[T]) updateKeyMaps() {
177182 if b == " " {
178183 continue
179184 }
180− m := alphanumeric.MatchString(b)
185+ m := alphanumericRegex.MatchString(b)
181186 if !m {
182187 keep = append(keep, b)
183188 }
internal/tui/exp/list/filterable_group.go+7−3
@@ -15,6 +15,12 @@ import (
1515 "github.com/sahilm/fuzzy"
1616)
1717
18+var (
19+ // Pre-compiled regex for checking if a string is alphanumeric.
20+ // Note: This is duplicated from filterable.go to avoid circular dependencies.
21+ alphanumericRegexGroup = regexp.MustCompile(`^[a-zA-Z0-9]*$`)
22+)
23+
1824type FilterableGroupList[T FilterableItem] interface {
1925 GroupedList[T]
2026 Cursor() *tea.Cursor
@@ -114,8 +120,6 @@ func (f *filterableGroupList[T]) View() string {
114120
115121// removes bindings that are used for search
116122func (f *filterableGroupList[T]) updateKeyMaps() {
117− alphanumeric := regexp.MustCompile("^[a-zA-Z0-9]*$")
118−
119123 removeLettersAndNumbers := func(bindings []string) []string {
120124 var keep []string
121125 for _, b := range bindings {
@@ -126,7 +130,7 @@ func (f *filterableGroupList[T]) updateKeyMaps() {
126130 if b == " " {
127131 continue
128132 }
129− m := alphanumeric.MatchString(b)
133+ m := alphanumericRegexGroup.MatchString(b)
130134 if !m {
131135 keep = append(keep, b)
132136 }