Skip to content

Commit cdc77bd

Browse files
committed
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.
1 parent 819e2e8 commit cdc77bd

14 files changed

+18
-37
lines changed

β€Žtea.goβ€Ž

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,6 @@ type Program struct {
517517
// once is used to stop the renderer.
518518
once sync.Once
519519

520-
// rendererDone is used to stop the renderer.
521-
rendererDone chan struct{}
522-
523520
// Initial window size. Mainly used for testing.
524521
width, height int
525522

@@ -577,7 +574,6 @@ func NewProgram(model Model, opts ...ProgramOption) *Program {
577574
initialModel: model,
578575
msgs: make(chan Msg),
579576
errs: make(chan error, 1),
580-
rendererDone: make(chan struct{}),
581577
}
582578

583579
// Apply all options to the program.
@@ -729,6 +725,9 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
729725
case err := <-p.errs:
730726
return model, err
731727

728+
case <-p.ticker.C:
729+
go p.render(model) // render view
730+
732731
case msg := <-p.msgs:
733732
msg = p.translateInputEvent(msg)
734733

@@ -849,8 +848,6 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
849848
return model, nil
850849
case cmds <- cmd: // process command (if any)
851850
}
852-
853-
p.render(model) // render view
854851
}
855852
}
856853
}
@@ -860,6 +857,8 @@ func (p *Program) render(model Model) {
860857
if p.renderer != nil {
861858
p.renderer.render(model.View()) // send view to renderer
862859
}
860+
_ = p.flush()
861+
_ = p.renderer.flush(false)
863862
}
864863

865864
func (p *Program) execSequenceMsg(msg sequenceMsg) {
@@ -1360,30 +1359,12 @@ func (p *Program) startRenderer() {
13601359

13611360
// Start the renderer.
13621361
p.renderer.start()
1363-
go func() {
1364-
for {
1365-
select {
1366-
case <-p.rendererDone:
1367-
p.ticker.Stop()
1368-
return
1369-
1370-
case <-p.ticker.C:
1371-
_ = p.flush()
1372-
_ = p.renderer.flush(false)
1373-
}
1374-
}
1375-
}()
13761362
}
13771363

13781364
// stopRenderer stops the renderer.
13791365
// If kill is true, the renderer will be stopped immediately without flushing
13801366
// the last frame.
13811367
func (p *Program) stopRenderer(kill bool) {
1382-
// Stop the renderer before acquiring the mutex to avoid a deadlock.
1383-
p.once.Do(func() {
1384-
p.rendererDone <- struct{}{}
1385-
})
1386-
13871368
if !kill {
13881369
// flush locks the mutex
13891370
_ = p.renderer.flush(true)

β€Žtestdata/TestClearMsg/bg_fg_cur_color.goldenβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[?25l[?2004h[=1;1usuccess[=0;1u[?25h[?2004l[?2026$p]10;?]11;?]12;?
1+
[?2026$p[?25l[?2004h[=1;1u[?usuccess]10;?]11;?]12;?success[=0;1u[?25h[?2004l

β€Žtestdata/TestClearMsg/clear_screen.goldenβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[?25l[?2004h[=1;1usuccess[=0;1u[?25h[?2004l[?2026$p
1+
[?2026$p[?25l[?2004h[=1;1u[?usuccesssuccess[=0;1u[?25h[?2004l

β€Žtestdata/TestClearMsg/read_set_clipboard.goldenβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[?25l[?2004h[=1;1usuccess[=0;1u[?25h[?2004l[?2026$p]52;c;?]52;c;c3VjY2Vzcw==
1+
[?2026$p[?25l[?2004h[=1;1u[?usuccess]52;c;?]52;c;c3VjY2Vzcw==success[=0;1u[?25h[?2004l

β€Žtestdata/TestViewModel/altscreen.goldenβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[?25l[?2004h[=1;1usuccess[=0;1u[?25h[?2004l[?2026$p
1+
[?2026$p[?25l[?2004h[=1;1u[?usuccesssuccess[=0;1u[?25h[?2004l

β€Žtestdata/TestViewModel/altscreen_autoexit.goldenβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[=0;1u[?1049h[?25l[?2004h[=1;1usuccess[=0;1u[?1049l[?25h[?2004l[?2026$p
1+
[?2026$p[?25l[?2004h[=1;1u[?usuccess[=0;1u[?1049h[?25l[=1;1u[?usuccess[=0;1u[?1049l[?25h[?2004l

β€Žtestdata/TestViewModel/bg_set_color.goldenβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[?25l[?2004h[=1;1u]11;#ffffffsuccess[=0;1u[?25h[?2004l]111[?2026$p
1+
[?2026$p[?25l[?2004h[=1;1u[?usuccess]11;#ffffffsuccess[=0;1u[?25h[?2004l]111

β€Žtestdata/TestViewModel/bp_stop_start.goldenβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[?25l[?2004h[=1;1usuccess[=0;1u[?25h[?2004l[?2026$p
1+
[?2026$p[?25l[?2004h[=1;1u[?usuccesssuccess[=0;1u[?25h[?2004l

β€Žtestdata/TestViewModel/cursor_hide.goldenβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[?25l[?2004h[=1;1usuccess[=0;1u[?25h[?2004l[?2026$p
1+
[?2026$p[?25l[?2004h[=1;1u[?usuccesssuccess[=0;1u[?25h[?2004l

β€Žtestdata/TestViewModel/cursor_hideshow.goldenβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[?2004h[=1;1u[1 qsuccess[?25h[=0;1u[?2004l[?2026$p
1+
[?2026$p[?25l[?2004h[=1;1u[?usuccess[1 qsuccess[?25h[=0;1u[?2004l

0 commit comments

Comments
Β (0)