bubbletea icon indicating copy to clipboard operation
bubbletea copied to clipboard

Function params involve heavy amount of copying

Open zLeki opened this issue 2 years ago • 4 comments

For performance sake, consider passing func (p Program) restoreTerminal() error { showCursor(p.output) Program as a pointer instead of the value itself.

zLeki avatar Mar 31 '22 19:03 zLeki

Noted: that's a very acute point.

meowgorithm avatar Mar 31 '22 20:03 meowgorithm

+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 👍🏼

jpmcb avatar Apr 26 '22 21:04 jpmcb

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.

meowgorithm avatar Apr 26 '22 22:04 meowgorithm

Makes sense! Makes sense!! 👍🏼

jpmcb avatar Apr 26 '22 22:04 jpmcb

This particular example (Program.RestoreTerminal) now operates on a pointer receiver. Closing.

muesli avatar Oct 23 '22 01:10 muesli