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
Provenance
- Commit
41b3511- Tree
02385f6- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
128e4a2
6 files changed+85 added−23 removed
modified:src/constants.go+1 added
12 unmodified lines13 coordinatorDelayStep time.Duration = 10 * time.Millisecond14 15 // Reader16 readerBufferSize = 64 * 102417+ readerSlabSize = 128 * 102418 readerPollIntervalMin = 10 * time.Millisecond19 readerPollIntervalStep = 5 * time.Millisecond20 readerPollIntervalMax = 50 * time.Millisecond21 54 unmodified linesmodified:src/core.go+9 added-4 removed
3 unmodified lines4 import (5 "fmt"6 "os"7 "time"8+ "unsafe"9 10 "github.com/junegunn/fzf/src/util"11 )12 5 unmodified lines18 Matcher -> EvtSearchFin -> Terminal (update list)19 Matcher -> EvtHeader -> Terminal (update header)20 */21 22+func ustring(data []byte) string {23+ return unsafe.String(unsafe.SliceData(data), len(data))24+}25+26 // Run starts fzf27 func Run(opts *Options, version string, revision string) {28 sort := opts.Sort > 029 sortCriteria = opts.Criteria19 unmodified lines49 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 lines70 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 lines81 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 := *prevLineAnsiState302 unmodified linesmodified:src/reader.go+72 added-17 removed
5 unmodified lines6 "io"7 "os"8 "os/exec"9 "path/filepath"10+ "strconv"11 "sync"12 "sync/atomic"13 "time"14 96 unmodified lines111 r.fin(success)112 }113 114 func (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+ }127+128+ slab := make([]byte, readerSlabSize)129+ pointer := 0130 delim := byte('\n')131 if r.delimNil {132 delim = '\000'133 }134 reader := bufio.NewReaderSize(src, readerBufferSize)135+136+ // We do not put a slice longer than 10% of the slab to reduce fragmentation137+ maxBytes := readerBufferSize / 10138+139 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 }161+162+ 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+ }175+176+ 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 }197 198 func (r *Reader) readFromStdin() bool {60 unmodified linesmodified:BUILD.md+1 added-1 removed
4 unmodified lines5678991011121338 unmodified lines4 unmodified lines------------------
### Prerequisites
- Go 1.18 or above- Go 1.20 or above
### Using Makefile
```sh38 unmodified linesmodified:CHANGELOG.md+1 added
1 unmodified line2 =========3 4 0.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 ```sh1962 unmodified linesmodified:go.mod+1 added-1 removed
15 unmodified lines16 github.com/mattn/go-runewidth v0.0.15 // indirect17 golang.org/x/text v0.14.0 // indirect18 )19 20-go 1.1720+go 1.20