Skip to content
Myr

Make Reader event notification asynchronous

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Instead of notifying the event coordinator (EventBox) whenever a new line is arrived, start a background goroutine that periodically does the task. Atomic.StoreInt32 is much cheaper than mutex synchronization that happens during EventBox update.

authored by Junegunn Choi · committed by Junegunn Choi · parent 0d171ba · Browse files · 4 files+64 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+64 added−18 removed

  • other:+64 added−18 removed

4 files+64 added−18 removed

modified:src/constants.go+5 added-2 removed
14 unmodified lines
15 coordinatorDelayMax time.Duration = 100 * time.Millisecond16 coordinatorDelayStep time.Duration = 10 * time.Millisecond1718 // Reader19 readerBufferSize = 64 * 102419 readerBufferSize = 64 * 102420 readerPollIntervalMin = 10 * time.Millisecond21 readerPollIntervalStep = 5 * time.Millisecond22 readerPollIntervalMax = 50 * time.Millisecond2324 // Terminal25 initialDelay = 20 * time.Millisecond26 initialDelayTac = 100 * time.Millisecond
43 unmodified lines
70 EvtSearchNew71 EvtSearchProgress72 EvtSearchFin73 EvtHeader71 EvtClose74 EvtReady75)7677const (78 exitOk = 0
4 unmodified lines
modified:src/core.go+4 added-4 removed
113 unmodified lines
114115 // Reader116 streamingFilter := opts.Filter != nil && !sort && !opts.Tac && !opts.Sync117 if !streamingFilter {118 reader := Reader{func(data []byte) bool {118 reader := NewReader(func(data []byte) bool {119 return chunkList.Push(data)120 }, eventBox, opts.ReadZero}120 }, eventBox, opts.ReadZero)121 go reader.ReadSource()122 }123124 // Matcher
24 unmodified lines
149150 found := false151 if streamingFilter {152 slab := util.MakeSlab(slab16Size, slab32Size)153 reader := Reader{153 reader := NewReader(154 func(runes []byte) bool {155 item := Item{}156 if chunkList.trans(&item, runes, 0) {157 if result, _, _ := pattern.MatchItem(&item, false, slab); result != nil {
1 unmodified line
159 found = true160 }161 }162 return false163 }, eventBox, opts.ReadZero}163 }, eventBox, opts.ReadZero)164 reader.ReadSource()165 } else {166 eventBox.Unwatch(EvtReadNew)167 eventBox.WaitFor(EvtReadFin)
110 unmodified lines
modified:src/reader_test.go+18 added-10 removed
1package fzf23import (4 "testing"5 "time"67 "github.com/junegunn/fzf/src/util"8)9
1 unmodified line
11 strs := []string{}12 eb := util.NewEventBox()13 reader := Reader{14 pusher: func(s []byte) bool { strs = append(strs, string(s)); return true },14 eventBox: eb}15 eventBox: eb,16 event: int32(EvtReady)}1718 reader.startEventPoller()1920 // Check EventBox21 if eb.Peek(EvtReadNew) {22 t.Error("EvtReadNew should not be set yet")23 }2425 // Normal command22 reader.readFromCommand(`echo abc && echo def`)26 reader.fin(reader.readFromCommand(`echo abc && echo def`))27 if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" {28 t.Errorf("%s", strs)29 }3031 // Check EventBox again28 if !eb.Peek(EvtReadNew) {29 t.Error("EvtReadNew should be set yet")30 }32 eb.WaitFor(EvtReadFin)3334 // Wait should return immediately35 eb.Wait(func(events *util.Events) {34 if _, found := (*events)[EvtReadNew]; !found {35 t.Errorf("%s", events)36 }36 events.Clear()37 })3839 // EventBox is cleared40 if eb.Peek(EvtReadNew) {41 t.Error("EvtReadNew should not be set yet")42 }4344 // Make sure that event poller is finished45 time.Sleep(readerPollIntervalMax)4647 // Restart event poller48 reader.startEventPoller()4950 // Failing command46 reader.readFromCommand(`no-such-command`)51 reader.fin(reader.readFromCommand(`no-such-command`))52 strs = []string{}53 if len(strs) > 0 {54 t.Errorf("%s", strs)55 }5657 // Check EventBox again58 if eb.Peek(EvtReadNew) {54 t.Error("Command failed. EvtReadNew should be set")59 t.Error("Command failed. EvtReadNew should not be set")60 }61 if !eb.Peek(EvtReadFin) {62 t.Error("EvtReadFin should be set")63 }64}
modified:src/reader.go+37 added-2 removed
2 unmodified lines
3import (4 "bufio"5 "io"6 "os"7 "sync/atomic"8 "time"910 "github.com/junegunn/fzf/src/util"11)12
1 unmodified line
14type Reader struct {15 pusher func([]byte) bool16 eventBox *util.EventBox17 delimNil bool18 event int3219}2021// NewReader returns new Reader object22func NewReader(pusher func([]byte) bool, eventBox *util.EventBox, delimNil bool) *Reader {23 return &Reader{pusher, eventBox, delimNil, int32(EvtReady)}24}2526func (r *Reader) startEventPoller() {27 go func() {28 ptr := &r.event29 pollInterval := readerPollIntervalMin30 for {31 if atomic.CompareAndSwapInt32(ptr, int32(EvtReadNew), int32(EvtReady)) {32 r.eventBox.Set(EvtReadNew, true)33 pollInterval = readerPollIntervalMin34 } else if atomic.LoadInt32(ptr) == int32(EvtReadFin) {35 return36 } else {37 pollInterval += readerPollIntervalStep38 if pollInterval > readerPollIntervalMax {39 pollInterval = readerPollIntervalMax40 }41 }42 time.Sleep(pollInterval)43 }44 }()45}4647func (r *Reader) fin(success bool) {48 atomic.StoreInt32(&r.event, int32(EvtReadFin))49 r.eventBox.Set(EvtReadFin, success)50}5152// ReadSource reads data from the default command or from standard input53func (r *Reader) ReadSource() {54 r.startEventPoller()55 var success bool56 if util.IsTty() {57 cmd := os.Getenv("FZF_DEFAULT_COMMAND")58 if len(cmd) == 0 {
2 unmodified lines
61 success = r.readFromCommand(cmd)62 } else {63 success = r.readFromStdin()64 }30 r.eventBox.Set(EvtReadFin, success)65 r.fin(success)66}6768func (r *Reader) feed(src io.Reader) {69 delim := byte('\n')
15 unmodified lines
85 bytea = bytea[:byteaLen-1]86 }87 }88 if r.pusher(bytea) {54 r.eventBox.Set(EvtReadNew, true)89 atomic.StoreInt32(&r.event, int32(EvtReadNew))90 }91 }92 if err != nil {93 break
22 unmodified lines