Skip to content
Myr

Improve ingestion performance (by around 20%)

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

authored by Junegunn Choi · committed by Junegunn Choi · parent 128e4a2 · Browse files · 6 files+85 added−23 removed
This commit

State

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

Diff stats

6 files+85 added−23 removed

  • other:+85 added−23 removed

6 files+85 added−23 removed

modified:src/constants.go+1 added
12 unmodified lines
13 coordinatorDelayStep time.Duration = 10 * time.Millisecond1415 // Reader16 readerBufferSize = 64 * 102417 readerSlabSize = 128 * 102418 readerPollIntervalMin = 10 * time.Millisecond19 readerPollIntervalStep = 5 * time.Millisecond20 readerPollIntervalMax = 50 * time.Millisecond21
54 unmodified lines
modified:src/core.go+9 added-4 removed
3 unmodified lines
4import (5 "fmt"6 "os"7 "time"8 "unsafe"910 "github.com/junegunn/fzf/src/util"11)12
5 unmodified lines
18Matcher -> EvtSearchFin -> Terminal (update list)19Matcher -> EvtHeader -> Terminal (update header)20*/2122func ustring(data []byte) string {23 return unsafe.String(unsafe.SliceData(data), len(data))24}2526// Run starts fzf27func Run(opts *Options, version string, revision string) {28 sort := opts.Sort > 029 sortCriteria = opts.Criteria
19 unmodified lines
49 if opts.Ansi {50 if opts.Theme.Colored {51 ansiProcessor = func(data []byte) (util.Chars, *[]ansiOffset) {52 prevLineAnsiState = lineAnsiState48 trimmed, offsets, newState := extractColor(string(data), lineAnsiState, nil)53 trimmed, offsets, newState := extractColor(ustring(data), lineAnsiState, nil)54 lineAnsiState = newState55 return util.ToChars([]byte(trimmed)), offsets56 }57 } else {58 // When color is disabled but ansi option is given,59 // we simply strip out ANSI codes from the input60 ansiProcessor = func(data []byte) (util.Chars, *[]ansiOffset) {56 trimmed, _, _ := extractColor(string(data), nil, nil)61 trimmed, _, _ := extractColor(ustring(data), nil, nil)62 return util.ToChars([]byte(trimmed)), nil63 }64 }65 }
4 unmodified lines
70 header := make([]string, 0, opts.HeaderLines)71 if len(opts.WithNth) == 0 {72 chunkList = NewChunkList(func(item *Item, data []byte) bool {73 if len(header) < opts.HeaderLines {69 header = append(header, string(data))74 header = append(header, ustring(data))75 eventBox.Set(EvtHeader, header)76 return false77 }78 item.text, item.colors = ansiProcessor(data)
2 unmodified lines
81 return true82 })83 } else {84 chunkList = NewChunkList(func(item *Item, data []byte) bool {80 tokens := Tokenize(string(data), opts.Delimiter)85 tokens := Tokenize(ustring(data), opts.Delimiter)86 if opts.Ansi && opts.Theme.Colored && len(tokens) > 1 {87 var ansiState *ansiState88 if prevLineAnsiState != nil {89 ansiStateDup := *prevLineAnsiState
302 unmodified lines
modified:src/reader.go+72 added-17 removed
5 unmodified lines
6 "io"7 "os"8 "os/exec"9 "path/filepath"10 "strconv"11 "sync"12 "sync/atomic"13 "time"14
96 unmodified lines
111 r.fin(success)112}113114func (r *Reader) feed(src io.Reader) {115 readerSlabSize, ae := strconv.Atoi(os.Getenv("SLAB_KB"))116 if ae != nil {117 readerSlabSize = 128 * 1024118 } else {119 readerSlabSize *= 1024120 }121 readerBufferSize, be := strconv.Atoi(os.Getenv("BUF_KB"))122 if be != nil {123 readerBufferSize = 64 * 1024124 } else {125 readerBufferSize *= 1024126 }127128 slab := make([]byte, readerSlabSize)129 pointer := 0130 delim := byte('\n')131 if r.delimNil {132 delim = '\000'133 }134 reader := bufio.NewReaderSize(src, readerBufferSize)135136 // We do not put a slice longer than 10% of the slab to reduce fragmentation137 maxBytes := readerBufferSize / 10138139 for {120 // ReadBytes returns err != nil if and only if the returned data does not121 // end in delim.122 bytea, err := reader.ReadBytes(delim)123 byteaLen := len(bytea)124 if byteaLen > 0 {125 if err == nil {126 // get rid of carriage return if under Windows:127 if util.IsWindows() && byteaLen >= 2 && bytea[byteaLen-2] == byte('\r') {128 bytea = bytea[:byteaLen-2]129 } else {130 bytea = bytea[:byteaLen-1]140 var frags [][]byte141 fragsLen := 0142 for {143 bytea, err := reader.ReadSlice(delim)144 if err == bufio.ErrBufferFull {145 // Could not find the delimiter in the reader buffer.146 // Need to collect the fragments and merge them later.147 frags = append(frags, bytea)148 fragsLen += len(bytea)149 } else {150 byteaLen := len(bytea)151 if err == nil {152 // No errors. Found the delimiter.153 if util.IsWindows() && byteaLen >= 2 && bytea[byteaLen-2] == byte('\r') {154 bytea = bytea[:byteaLen-2]155 byteaLen -= 2156 } else {157 bytea = bytea[:byteaLen-1]158 byteaLen--159 }160 }161162 itemLen := fragsLen + byteaLen163 pointer += itemLen164 var slice []byte165 if itemLen <= maxBytes { // We can use the slab166 // Allocate a new slab if it doesn't fit167 if pointer > readerSlabSize {168 slab = make([]byte, readerSlabSize)169 pointer = itemLen170 }171 slice = slab[pointer-itemLen : pointer]172 } else { // We can't use the slab because the item is too large173 slice = make([]byte, itemLen)174 }175176 if len(frags) > 0 {177 // Collect the fragments178 n := 0179 for _, frag := range frags {180 n += copy(slice[n:], frag)181 }182 copy(slice[n:], bytea)183 } else if byteaLen > 0 {184 copy(slice, bytea)185 }186 if (err == nil || itemLen > 0) && r.pusher(slice) {187 atomic.StoreInt32(&r.event, int32(EvtReadNew))188 }189 if err != nil {190 return191 }192 break193 }133 if r.pusher(bytea) {134 atomic.StoreInt32(&r.event, int32(EvtReadNew))135 }194 }137 if err != nil {138 break139 }195 }196}197198func (r *Reader) readFromStdin() bool {
60 unmodified lines
modified:BUILD.md+1 added-1 removed
modified:CHANGELOG.md+1 added
1 unmodified line
2=========340.49.05------6- Ingestion performance improved by around 20%7- Added two environment variables exported to the child processes8 - `FZF_PREVIEW_LABEL`9 - `FZF_BORDER_LABEL`10 ```sh
1962 unmodified lines
modified:go.mod+1 added-1 removed
15 unmodified lines
16 github.com/mattn/go-runewidth v0.0.15 // indirect17 golang.org/x/text v0.14.0 // indirect18)1920go 1.1720go 1.20