commit 717ab5a
delthas
·
2022-04-15 12:55:53 +0000 UTC
parent c86a2fa
Batch events by time period rather than by fixed size Previously, we would render every 64 events (at most). Now that we have many more events, with history & read, batch events by period rather than static size. Waiting up to 200ms before refreshing is a good compromise between responsiveness and performance.
1 files changed,
+27,
-19
M
app.go
M
app.go
+27,
-19
1@@ -18,7 +18,7 @@ import (
2 "github.com/gdamore/tcell/v2"
3 )
4
5-const eventChanSize = 64
6+const eventChanSize = 1024
7
8 func isCommand(input []rune) bool {
9 // Command can't start with two slashes because that's an escape for
10@@ -194,31 +194,26 @@ func (app *App) SetLastClose(t time.Time) {
11 func (app *App) eventLoop() {
12 defer app.win.Close()
13
14- evs := make([]event, 0, eventChanSize)
15 for !app.win.ShouldExit() {
16 ev := <-app.events
17- evs = evs[:0]
18- evs = append(evs, ev)
19- Batch:
20- for i := 0; i < eventChanSize; i++ {
21+ if !app.handleEvent(ev) {
22+ return
23+ }
24+ deadline := time.NewTimer(200 * time.Millisecond)
25+ outer:
26+ for {
27 select {
28+ case <-deadline.C:
29+ break outer
30 case ev := <-app.events:
31- evs = append(evs, ev)
32- default:
33- break Batch
34- }
35- }
36-
37- for _, ev := range evs {
38- if ev.src == "*" {
39- if ev.content == nil {
40+ if !app.handleEvent(ev) {
41 return
42 }
43- if !app.handleUIEvent(ev.content) {
44- return
45+ default:
46+ if !deadline.Stop() {
47+ <-deadline.C
48 }
49- } else {
50- app.handleIRCEvent(ev.src, ev.content)
51+ break outer
52 }
53 }
54
55@@ -247,6 +242,19 @@ func (app *App) eventLoop() {
56 }
57 }()
58 }
59+func (app *App) handleEvent(ev event) bool {
60+ if ev.src == "*" {
61+ if ev.content == nil {
62+ return false
63+ }
64+ if !app.handleUIEvent(ev.content) {
65+ return false
66+ }
67+ } else {
68+ app.handleIRCEvent(ev.src, ev.content)
69+ }
70+ return true
71+}
72
73 // ircLoop maintains a connection to the IRC server by connecting and then
74 // forwarding IRC events to app.events repeatedly.