Skip to content

Commit

Permalink
refactor(textinput): reduce allocations
Browse files Browse the repository at this point in the history
As the number of items in `suggestions` and `m.suggestions` is known,
we can use `make` with the expected size instead of appending, which
requires more allocations as the slice grows.
  • Loading branch information
naglis committed Aug 30, 2023
1 parent eda8912 commit 54a9360
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions textinput/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,9 @@ func (m *Model) Reset() {

// SetSuggestions sets the suggestions for the input.
func (m *Model) SetSuggestions(suggestions []string) {
m.suggestions = [][]rune{}

for _, s := range suggestions {
m.suggestions = append(m.suggestions, []rune(s))
m.suggestions = make([][]rune, len(suggestions))
for i, s := range suggestions {
m.suggestions[i] = []rune(s)
}

m.updateSuggestions()
Expand Down Expand Up @@ -799,9 +798,9 @@ func (m Model) completionView(offset int) string {

// AvailableSuggestions returns the list of available suggestions.
func (m *Model) AvailableSuggestions() []string {
suggestions := []string{}
for _, s := range m.suggestions {
suggestions = append(suggestions, string(s))
suggestions := make([]string, len(m.suggestions))
for i, s := range m.suggestions {
suggestions[i] = string(s)
}

return suggestions
Expand Down

0 comments on commit 54a9360

Please sign in to comment.