bubbletea
bubbletea copied to clipboard
Function params involve heavy amount of copying
For performance sake, consider passing
func (p Program) restoreTerminal() error { showCursor(p.output)
Program as a pointer instead of the value itself.
Noted: that's a very acute point.
+1 - I get these errors frequently via golangci-lint
:
wizard/model.go:41:7: hugeParam: m is heavy (96 bytes); consider passing it by pointer (gocritic)
func (m model) Init() tea.Cmd {
^
wizard/model.go:46:7: hugeParam: m is heavy (96 bytes); consider passing it by pointer (gocritic)
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
^
wizard/model.go:150:7: hugeParam: m is heavy (96 bytes); consider passing it by pointer (gocritic)
func (m model) View() string {
Passing the model struct around is costly and would be great to change this interface to expect a pointer: func (m *model
). Would probably have to be in some kind of major release since I think this would be a breaking change.
Happy to contribute this back as well 👍🏼
In your own models, there's nothing stopping you from adding methods with pointer receivers so long as you initialize the model as a pointer:
type Model struct { /* ... */ }
func (m *Model) Init() tea.Cmd {
return nil
}
// Et cetera
m := &Model{}
Technically speaking this breaks the TEA immutability paradigm a bit, but it's generally fine so long as you're aware that the model is being mutated. For heavy models it's perfectly reasonable. We're do it across the board in Soft Serve.
Makes sense! Makes sense!! 👍🏼
This particular example (Program.RestoreTerminal
) now operates on a pointer receiver. Closing.