on main help sign in
perf: avoid regex compilation in runtime
760e5c4Raphael Amorim
internal/llm/provider/anthropic.go+6−2
@@ -21,6 +21,11 @@ import (
21 "github.com/charmbracelet/crush/internal/message"
21 "github.com/charmbracelet/crush/internal/message"
22)
22)
23
23
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+
24type anthropicClient struct {
29type anthropicClient struct {
25 providerOptions providerClientOptions
30 providerOptions providerClientOptions
26 useBedrock bool
31 useBedrock bool
@@ -512,8 +517,7 @@ func (a *anthropicClient) handleContextLimitError(apiErr *anthropic.Error) (int,
512 // Parse error message like: "input length and max_tokens exceed context limit: 154978 + 50000 > 200000"
517 // Parse error message like: "input length and max_tokens exceed context limit: 154978 + 50000 > 200000"
513 errorMsg := apiErr.Error()
518 errorMsg := apiErr.Error()
514
519
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)
517
521
518 if len(matches) != 4 {
522 if len(matches) != 4 {
519 return 0, false
523 return 0, false
internal/llm/tools/view.go+2−1
@@ -306,7 +306,8 @@ func readTextFile(filePath string, offset, limit int) (string, int, error) {
306 }
306 }
307 }
307 }
308
308
309+ // Pre-allocate slice with expected capacity
309− var lines []string
310+ lines := make([]string, 0, limit)
310 lineCount = offset
311 lineCount = offset
311
312
312 for scanner.Scan() && len(lines) < limit {
313 for scanner.Scan() && len(lines) < limit {
internal/tui/exp/list/filterable.go+8−3
@@ -15,6 +15,13 @@ import (
15 "github.com/sahilm/fuzzy"
15 "github.com/sahilm/fuzzy"
16)
16)
17
17
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+
18type FilterableItem interface {
25type FilterableItem interface {
19 Item
26 Item
20 FilterValue() string
27 FilterValue() string
@@ -165,8 +172,6 @@ func (f *filterableList[T]) View() string {
165
172
166// removes bindings that are used for search
173// removes bindings that are used for search
167func (f *filterableList[T]) updateKeyMaps() {
174func (f *filterableList[T]) updateKeyMaps() {
168− alphanumeric := regexp.MustCompile("^[a-zA-Z0-9]*$")
169−
170 removeLettersAndNumbers := func(bindings []string) []string {
175 removeLettersAndNumbers := func(bindings []string) []string {
171 var keep []string
176 var keep []string
172 for _, b := range bindings {
177 for _, b := range bindings {
@@ -177,7 +182,7 @@ func (f *filterableList[T]) updateKeyMaps() {
177 if b == " " {
182 if b == " " {
178 continue
183 continue
179 }
184 }
180− m := alphanumeric.MatchString(b)
185+ m := alphanumericRegex.MatchString(b)
181 if !m {
186 if !m {
182 keep = append(keep, b)
187 keep = append(keep, b)
183 }
188 }
internal/tui/exp/list/filterable_group.go+7−3
@@ -15,6 +15,12 @@ import (
15 "github.com/sahilm/fuzzy"
15 "github.com/sahilm/fuzzy"
16)
16)
17
17
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+
18type FilterableGroupList[T FilterableItem] interface {
24type FilterableGroupList[T FilterableItem] interface {
19 GroupedList[T]
25 GroupedList[T]
20 Cursor() *tea.Cursor
26 Cursor() *tea.Cursor
@@ -114,8 +120,6 @@ func (f *filterableGroupList[T]) View() string {
114
120
115// removes bindings that are used for search
121// removes bindings that are used for search
116func (f *filterableGroupList[T]) updateKeyMaps() {
122func (f *filterableGroupList[T]) updateKeyMaps() {
117− alphanumeric := regexp.MustCompile("^[a-zA-Z0-9]*$")
118−
119 removeLettersAndNumbers := func(bindings []string) []string {
123 removeLettersAndNumbers := func(bindings []string) []string {
120 var keep []string
124 var keep []string
121 for _, b := range bindings {
125 for _, b := range bindings {
@@ -126,7 +130,7 @@ func (f *filterableGroupList[T]) updateKeyMaps() {
126 if b == " " {
130 if b == " " {
127 continue
131 continue
128 }
132 }
129− m := alphanumeric.MatchString(b)
133+ m := alphanumericRegexGroup.MatchString(b)
130 if !m {
134 if !m {
131 keep = append(keep, b)
135 keep = append(keep, b)
132 }
136 }