reader.go
This file
Path
src/reader.go
278 lines · 6.1 KiB · Go
Last changed by
Refactor the code to remove global variablesJunegunn Choibefore adoption4bedd33
History of this fileTake it
The walk stopped after 200 commits: lines older than d75bb5c point at it.
Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 1–3why, one step back1package fzf23import (Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37
line 4why, one step back4 "bytes"Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 5–13why, one step back5 "context"6 "io"7 "os"8 "os/exec"9 "path/filepath"10 "sync"11 "sync/atomic"12 "time"13Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556
line 14why, one step back14 "github.com/charlievieth/fastwalk"Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 15–20why, one step back15 "github.com/junegunn/fzf/src/util"16)1718// Reader reads from command or standard input19type Reader struct {20 pusher func([]byte) boolAdd --with-shell for shelling out with different command and flags (#3746)Junegunn Choibefore adoptiona4391ae
line 21why, one step back21 executor *util.ExecutorDeploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 22–33why, one step back22 eventBox *util.EventBox23 delimNil bool24 event int3225 finChan chan bool26 mutex sync.Mutex27 exec *exec.Cmd28 command *string29 killed bool30 wait bool31}3233// NewReader returns new Reader objectAdd --with-shell for shelling out with different command and flags (#3746)Junegunn Choibefore adoptiona4391ae
lines 34–35why, one step back34func NewReader(pusher func([]byte) bool, eventBox *util.EventBox, executor *util.Executor, delimNil bool, wait bool) *Reader {35 return &Reader{pusher, executor, eventBox, delimNil, int32(EvtReady), make(chan bool, 1), sync.Mutex{}, nil, nil, false, wait}Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 36–82why, one step back36}3738func (r *Reader) startEventPoller() {39 go func() {40 ptr := &r.event41 pollInterval := readerPollIntervalMin42 for {43 if atomic.CompareAndSwapInt32(ptr, int32(EvtReadNew), int32(EvtReady)) {44 r.eventBox.Set(EvtReadNew, (*string)(nil))45 pollInterval = readerPollIntervalMin46 } else if atomic.LoadInt32(ptr) == int32(EvtReadFin) {47 if r.wait {48 r.finChan <- true49 }50 return51 } else {52 pollInterval += readerPollIntervalStep53 if pollInterval > readerPollIntervalMax {54 pollInterval = readerPollIntervalMax55 }56 }57 time.Sleep(pollInterval)58 }59 }()60}6162func (r *Reader) fin(success bool) {63 atomic.StoreInt32(&r.event, int32(EvtReadFin))64 if r.wait {65 <-r.finChan66 }6768 r.mutex.Lock()69 ret := r.command70 if success || r.killed {71 ret = nil72 }73 r.mutex.Unlock()7475 r.eventBox.Set(EvtReadFin, ret)76}7778func (r *Reader) terminate() {79 r.mutex.Lock()80 r.killed = true81 if r.exec != nil && r.exec.Process != nil {82 util.KillCommand(r.exec)Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556
line 83why, one step back83 } else {Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 84–85why, one step back84 os.Stdin.Close()85 }Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556
line 86why, one step back86 r.mutex.Unlock()Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 87–88why, one step back87}88Refactor the code to remove global variablesJunegunn Choibefore adoption4bedd33
line 89why, one step back89func (r *Reader) restart(command commandSpec, environ []string) {Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 90–91why, one step back90 r.event = int32(EvtReady)91 r.startEventPoller()Refactor the code to remove global variablesJunegunn Choibefore adoption4bedd33
line 92why, one step back92 success := r.readFromCommand(command.command, environ)Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
line 93why, one step back93 r.fin(success)Refactor the code to remove global variablesJunegunn Choibefore adoption4bedd33
line 94why, one step back94 removeFiles(command.tempFiles)Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 95–96why, one step back95}96Refactor the code so that fzf can be used as a library (#3769)Junegunn Choibefore adoptione8405f4
lines 97–109why, one step back97func (r *Reader) readChannel(inputChan chan string) bool {98 for {99 item, more := <-inputChan100 if !more {101 break102 }103 if r.pusher([]byte(item)) {104 atomic.StoreInt32(&r.event, int32(EvtReadNew))105 }106 }107 return true108}109Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
line 110why, one step back110// ReadSource reads data from the default command or from standard inputRefactor the code so that fzf can be used as a library (#3769)Junegunn Choibefore adoptione8405f4
line 111why, one step back111func (r *Reader) ReadSource(inputChan chan string, root string, opts walkerOpts, ignores []string) {Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 112–113why, one step back112 r.startEventPoller()113 var success boolRefactor the code so that fzf can be used as a library (#3769)Junegunn Choibefore adoptione8405f4
lines 114–116why, one step back114 if inputChan != nil {115 success = r.readChannel(inputChan)116 } else if util.IsTty() {Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 117–118why, one step back117 cmd := os.Getenv("FZF_DEFAULT_COMMAND")118 if len(cmd) == 0 {Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
line 119why, one step back119 success = r.readFiles(root, opts, ignores)Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
line 120why, one step back120 } else {Export FZF_* variables to 'reload' process as wellJunegunn Choibefore adoption5e6788c
lines 121–122why, one step back121 // We can't export FZF_* environment variables to the default command122 success = r.readFromCommand(cmd, nil)Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 123–130why, one step back123 }124 } else {125 success = r.readFromStdin()126 }127 r.fin(success)128}129130func (r *Reader) feed(src io.Reader) {Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37
lines 131–144why, one step back131 /*132 readerSlabSize, ae := strconv.Atoi(os.Getenv("SLAB_KB"))133 if ae != nil {134 readerSlabSize = 128 * 1024135 } else {136 readerSlabSize *= 1024137 }138 readerBufferSize, be := strconv.Atoi(os.Getenv("BUF_KB"))139 if be != nil {140 readerBufferSize = 64 * 1024141 } else {142 readerBufferSize *= 1024143 }144 */Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511
line 145why, one step back145Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
line 146why, one step back146 delim := byte('\n')Do not trim CR on Windows when --read0 is setJunegunn Choibefore adoption68a35e4
line 147why, one step back147 trimCR := util.IsWindows()Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 148–149why, one step back148 if r.delimNil {149 delim = '\000'Do not trim CR on Windows when --read0 is setJunegunn Choibefore adoption68a35e4
line 150why, one step back150 trimCR = falseDeploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
line 151why, one step back151 }Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511
line 152why, one step back152Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37
lines 153–155why, one step back153 slab := make([]byte, readerSlabSize)154 leftover := []byte{}155 var err errorDeploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
line 156why, one step back156 for {Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37
lines 157–164why, one step back157 n := 0158 scope := slab[:util.Min(len(slab), readerBufferSize)]159 for i := 0; i < 100; i++ {160 n, err = src.Read(scope)161 if n > 0 || err != nil {162 break163 }164 }Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511
line 165why, one step back165Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37
line 166why, one step back166 // We're not making any progress after 100 tries. Stop.Fix 'reload' not terminating closed standard input streamJunegunn Choibefore adoptionfddbfe7
line 167why, one step back167 if n == 0 {Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37
lines 168–169why, one step back168 break169 }Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511
line 170why, one step back170Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37
lines 171–178why, one step back171 buf := slab[:n]172 slab = slab[n:]173174 for len(buf) > 0 {175 if i := bytes.IndexByte(buf, delim); i >= 0 {176 // Found the delimiter177 slice := buf[:i+1]178 buf = buf[i+1:]Do not trim CR on Windows when --read0 is setJunegunn Choibefore adoption68a35e4
line 179why, one step back179 if trimCR && len(slice) >= 2 && slice[len(slice)-2] == byte('\r') {Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37
lines 180–182why, one step back180 slice = slice[:len(slice)-2]181 } else {182 slice = slice[:len(slice)-1]Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511
line 183why, one step back183 }Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37
lines 184–188why, one step back184 if len(leftover) > 0 {185 slice = append(leftover, slice...)186 leftover = []byte{}187 }188 if (err == nil || len(slice) > 0) && r.pusher(slice) {Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511
lines 189–190why, one step back189 atomic.StoreInt32(&r.event, int32(EvtReadNew))190 }Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37
lines 191–192why, one step back191 } else {192 // Could not find the delimiter in the bufferImprove search performance by limiting the search scopeJunegunn Choibefore adoptione86b81b
lines 193–198why, one step back193 // NOTE: We can further optimize this by keeping track of the cursor194 // position in the slab so that a straddling item that doesn't go195 // beyond the boundary of a slab doesn't need to be copied to196 // another buffer. However, the performance gain is negligible in197 // practice (< 0.1%) and is not198 // worth the added complexity.Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37
line 199why, one step back199 leftover = append(leftover, buf...)Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511
line 200why, one step back200 breakDeploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 201–202why, one step back201 }202 }Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37
lines 203–214why, one step back203204 if err == io.EOF {205 leftover = append(leftover, buf...)206 break207 }208209 if len(slab) == 0 {210 slab = make([]byte, readerSlabSize)211 }212 }213 if len(leftover) > 0 && r.pusher(leftover) {214 atomic.StoreInt32(&r.event, int32(EvtReadNew))Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 215–222why, one step back215 }216}217218func (r *Reader) readFromStdin() bool {219 r.feed(os.Stdin)220 return true221}222Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
line 223why, one step back223func (r *Reader) readFiles(root string, opts walkerOpts, ignores []string) bool {Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
line 224why, one step back224 r.killed = falseAdd walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
line 225why, one step back225 conf := fastwalk.Config{Follow: opts.follow}Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556
lines 226–229why, one step back226 fn := func(path string, de os.DirEntry, err error) error {227 if err != nil {228 return nil229 }Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 230–231why, one step back230 path = filepath.Clean(path)231 if path != "." {Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556
line 232why, one step back232 isDir := de.IsDir()Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
lines 233–242why, one step back233 if isDir {234 base := filepath.Base(path)235 if !opts.hidden && base[0] == '.' {236 return filepath.SkipDir237 }238 for _, ignore := range ignores {239 if ignore == base {240 return filepath.SkipDir241 }242 }Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
line 243why, one step back243 }Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
line 244why, one step back244 if ((opts.file && !isDir) || (opts.dir && isDir)) && r.pusher([]byte(path)) {Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 245–254why, one step back245 atomic.StoreInt32(&r.event, int32(EvtReadNew))246 }247 }248 r.mutex.Lock()249 defer r.mutex.Unlock()250 if r.killed {251 return context.Canceled252 }253 return nil254 }Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
line 255why, one step back255 return fastwalk.Walk(&conf, root, fn) == nilDeploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 256–257why, one step back256}257Export FZF_* variables to 'reload' process as wellJunegunn Choibefore adoption5e6788c
line 258why, one step back258func (r *Reader) readFromCommand(command string, environ []string) bool {Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 259–261why, one step back259 r.mutex.Lock()260 r.killed = false261 r.command = &commandAdd --with-shell for shelling out with different command and flags (#3746)Junegunn Choibefore adoptiona4391ae
line 262why, one step back262 r.exec = r.executor.ExecCommand(command, true)Export FZF_* variables to 'reload' process as wellJunegunn Choibefore adoption5e6788c
lines 263–265why, one step back263 if environ != nil {264 r.exec.Env = environ265 }Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c
lines 266–278why, one step back266 out, err := r.exec.StdoutPipe()267 if err != nil {268 r.mutex.Unlock()269 return false270 }271 err = r.exec.Start()272 r.mutex.Unlock()273 if err != nil {274 return false275 }276 r.feed(out)277 return r.exec.Wait() == nil278}