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
Provenance
- Commit
487c8fe- Tree
d21e3b4- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
0d171ba
4 files changed+64 added−18 removed
modified:src/constants.go+5 added-2 removed
14 unmodified lines15 coordinatorDelayMax time.Duration = 100 * time.Millisecond16 coordinatorDelayStep time.Duration = 10 * time.Millisecond17 18 // Reader19- readerBufferSize = 64 * 102419+ readerBufferSize = 64 * 102420+ readerPollIntervalMin = 10 * time.Millisecond21+ readerPollIntervalStep = 5 * time.Millisecond22+ readerPollIntervalMax = 50 * time.Millisecond23 24 // Terminal25 initialDelay = 20 * time.Millisecond26 initialDelayTac = 100 * time.Millisecond43 unmodified lines70 EvtSearchNew71 EvtSearchProgress72 EvtSearchFin73 EvtHeader71- EvtClose74+ EvtReady75 )76 77 const (78 exitOk = 04 unmodified linesmodified:src/core.go+4 added-4 removed
113 unmodified lines114 115 // 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 }123 124 // Matcher24 unmodified lines149 150 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 line159 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 linesmodified:src/reader_test.go+18 added-10 removed
1 package fzf2 3 import (4 "testing"5+ "time"6 7 "github.com/junegunn/fzf/src/util"8 )9 1 unmodified line11 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)}17 18+ reader.startEventPoller()19+20 // Check EventBox21 if eb.Peek(EvtReadNew) {22 t.Error("EvtReadNew should not be set yet")23 }24 25 // 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 }30 31 // Check EventBox again28- if !eb.Peek(EvtReadNew) {29- t.Error("EvtReadNew should be set yet")30- }32+ eb.WaitFor(EvtReadFin)33 34 // 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 })38 39 // EventBox is cleared40 if eb.Peek(EvtReadNew) {41 t.Error("EvtReadNew should not be set yet")42 }43 44+ // Make sure that event poller is finished45+ time.Sleep(readerPollIntervalMax)46+47+ // Restart event poller48+ reader.startEventPoller()49+50 // 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 }56 57 // 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 lines3 import (4 "bufio"5 "io"6 "os"7+ "sync/atomic"8+ "time"9 10 "github.com/junegunn/fzf/src/util"11 )12 1 unmodified line14 type Reader struct {15 pusher func([]byte) bool16 eventBox *util.EventBox17 delimNil bool18+ event int3219 }20 21+// NewReader returns new Reader object22+func NewReader(pusher func([]byte) bool, eventBox *util.EventBox, delimNil bool) *Reader {23+ return &Reader{pusher, eventBox, delimNil, int32(EvtReady)}24+}25+26+func (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+}46+47+func (r *Reader) fin(success bool) {48+ atomic.StoreInt32(&r.event, int32(EvtReadFin))49+ r.eventBox.Set(EvtReadFin, success)50+}51+52 // ReadSource reads data from the default command or from standard input53 func (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 lines61 success = r.readFromCommand(cmd)62 } else {63 success = r.readFromStdin()64 }30- r.eventBox.Set(EvtReadFin, success)65+ r.fin(success)66 }67 68 func (r *Reader) feed(src io.Reader) {69 delim := byte('\n')15 unmodified lines85 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 break22 unmodified lines