Compare commits

...

1 commit

Author SHA1 Message Date
Ayman Bagabas
50d023dc52
perf: combine event and render loops into single loop
This simplifies the program's main loop by merging the event handling
and rendering processes. This change reduces complexity and potential
synchronization issues, leading to improved performance and
maintainability.
2026-01-13 14:58:43 -05:00
14 changed files with 18 additions and 37 deletions

29
tea.go
View file

@ -517,9 +517,6 @@ type Program struct {
// once is used to stop the renderer. // once is used to stop the renderer.
once sync.Once once sync.Once
// rendererDone is used to stop the renderer.
rendererDone chan struct{}
// Initial window size. Mainly used for testing. // Initial window size. Mainly used for testing.
width, height int width, height int
@ -577,7 +574,6 @@ func NewProgram(model Model, opts ...ProgramOption) *Program {
initialModel: model, initialModel: model,
msgs: make(chan Msg), msgs: make(chan Msg),
errs: make(chan error, 1), errs: make(chan error, 1),
rendererDone: make(chan struct{}),
} }
// Apply all options to the program. // Apply all options to the program.
@ -729,6 +725,9 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
case err := <-p.errs: case err := <-p.errs:
return model, err return model, err
case <-p.ticker.C:
go p.render(model) // render view
case msg := <-p.msgs: case msg := <-p.msgs:
msg = p.translateInputEvent(msg) msg = p.translateInputEvent(msg)
@ -849,8 +848,6 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
return model, nil return model, nil
case cmds <- cmd: // process command (if any) case cmds <- cmd: // process command (if any)
} }
p.render(model) // render view
} }
} }
} }
@ -860,6 +857,8 @@ func (p *Program) render(model Model) {
if p.renderer != nil { if p.renderer != nil {
p.renderer.render(model.View()) // send view to renderer p.renderer.render(model.View()) // send view to renderer
} }
_ = p.flush()
_ = p.renderer.flush(false)
} }
func (p *Program) execSequenceMsg(msg sequenceMsg) { func (p *Program) execSequenceMsg(msg sequenceMsg) {
@ -1360,30 +1359,12 @@ func (p *Program) startRenderer() {
// Start the renderer. // Start the renderer.
p.renderer.start() p.renderer.start()
go func() {
for {
select {
case <-p.rendererDone:
p.ticker.Stop()
return
case <-p.ticker.C:
_ = p.flush()
_ = p.renderer.flush(false)
}
}
}()
} }
// stopRenderer stops the renderer. // stopRenderer stops the renderer.
// If kill is true, the renderer will be stopped immediately without flushing // If kill is true, the renderer will be stopped immediately without flushing
// the last frame. // the last frame.
func (p *Program) stopRenderer(kill bool) { func (p *Program) stopRenderer(kill bool) {
// Stop the renderer before acquiring the mutex to avoid a deadlock.
p.once.Do(func() {
p.rendererDone <- struct{}{}
})
if !kill { if !kill {
// flush locks the mutex // flush locks the mutex
_ = p.renderer.flush(true) _ = p.renderer.flush(true)

View file

@ -1 +1 @@
[?25l[?2004h[=1;1u success[=0;1u [?25h[?2004l[?2026$p]10;?]11;?]12;? [?2026$p[?25l[?2004h[=1;1u[?u success]10;?]11;?]12;? success[=0;1u [?25h[?2004l

View file

@ -1 +1 @@
[?25l [?2004h[=1;1usuccess[=0;1u [?25h[?2004l[?2026$p [?2026$p[?25l[?2004h[=1;1u[?u success success[=0;1u [?25h[?2004l

View file

@ -1 +1 @@
[?25l[?2004h[=1;1u success[=0;1u [?25h[?2004l[?2026$p]52;c;?]52;c;c3VjY2Vzcw== [?2026$p[?25l[?2004h[=1;1u[?u success]52;c;?]52;c;c3VjY2Vzcw== success[=0;1u [?25h[?2004l

View file

@ -1 +1 @@
[?25l[?2004h[=1;1u success[=0;1u [?25h[?2004l[?2026$p [?2026$p[?25l[?2004h[=1;1u[?u success success[=0;1u [?25h[?2004l

View file

@ -1 +1 @@
[=0;1u[?1049h[?25l[?2004h[=1;1usuccess[=0;1u[?1049l[?25h[?2004l[?2026$p [?2026$p[?25l[?2004h[=1;1u[?u success[=0;1u[?1049h[?25l[=1;1u[?usuccess[=0;1u [?1049l[?25h[?2004l

View file

@ -1 +1 @@
[?25l[?2004h[=1;1u]11;#ffffff success[=0;1u [?25h[?2004l]111[?2026$p [?2026$p[?25l[?2004h[=1;1u[?u success]11;#ffffff success[=0;1u [?25h[?2004l]111

View file

@ -1 +1 @@
[?25l[?2004h[=1;1u success[=0;1u [?25h[?2004l[?2026$p [?2026$p[?25l[?2004h[=1;1u[?u success success[=0;1u [?25h[?2004l

View file

@ -1 +1 @@
[?25l[?2004h[=1;1u success[=0;1u [?25h[?2004l[?2026$p [?2026$p[?25l[?2004h[=1;1u[?u success success[=0;1u [?25h[?2004l

View file

@ -1 +1 @@
[?2004h[=1;1u[1 q success [?25h[=0;1u[?2004l[?2026$p [?2026$p[?25l[?2004h[=1;1u[?u success[1 q success [?25h[=0;1u[?2004l

View file

@ -1 +1 @@
[?25l[?2004h[=3;1u success[=0;1u [?25h[?2004l[?2026$p [?2026$p[?25l[?2004h[=1;1u[?u success[=3;1u[?u success[=0;1u [?25h[?2004l

View file

@ -1 +1 @@
[?25l[?2004h[?1003h[?1006h[=1;1u success[=0;1u [?25h[?2004l[?1002l[?1003l[?1006l[?2026$p [?2026$p[?25l[?2004h[=1;1u[?u success[?1003h[?1006h success[=0;1u [?25h[?2004l[?1002l[?1003l[?1006l

View file

@ -1 +1 @@
[?25l[?2004h[?1002h[?1006h[=1;1u success[=0;1u [?25h[?2004l[?1002l[?1003l[?1006l[?2026$p [?2026$p[?25l[?2004h[=1;1u[?u success[?1002h[?1006h success[=0;1u [?25h[?2004l[?1002l[?1003l[?1006l

View file

@ -1 +1 @@
[?25l[?2004h[=1;1u success[=0;1u [?25h[?2004l[?2026$p [?2026$p[?25l[?2004h[=1;1u[?u success success[=0;1u [?25h[?2004l