Skip to content
Myr

Performance fix - unnecessary rune convertion on --ansi

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

> time cat /tmp/list | fzf-0.10.1-darwin_amd64 --ansi -fqwerty > /dev/null real 0m4.364s user 0m8.231s sys 0m0.820s > time cat /tmp/list | fzf --ansi -fqwerty > /dev/null real 0m4.624s user 0m5.755s sys 0m0.732s

authored by Junegunn Choi · committed by Junegunn Choi · parent 0ea6632 · Browse files · 6 files+52 added−42 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+52 added−42 removed

  • other:+52 added−42 removed

6 files+52 added−42 removed

modified:src/util/util.go+16 added
4 unmodified lines
56import (7 "os"8 "time"9 "unicode/utf8"10)1112// Max returns the largest integer13func Max(first int, items ...int) int {
74 unmodified lines
88 }89 }90 return runes[0 : i+1]91}9293func BytesToRunes(bytea []byte) []rune {94 runes := make([]rune, 0, len(bytea))95 for i := 0; i < len(bytea); {96 if bytea[i] < utf8.RuneSelf {97 runes = append(runes, rune(bytea[i]))98 i++99 } else {100 r, sz := utf8.DecodeRune(bytea[i:])101 i += sz102 runes = append(runes, r)103 }104 }105 return runes106}
modified:src/chunklist_test.go+7 added-7 removed
4 unmodified lines
5 "testing"6)78func TestChunkList(t *testing.T) {9 cl := NewChunkList(func(s []rune, i int) *Item {10 return &Item{text: s, rank: Rank{0, 0, uint32(i * 2)}}9 cl := NewChunkList(func(s []byte, i int) *Item {10 return &Item{text: []rune(string(s)), rank: Rank{0, 0, uint32(i * 2)}}11 })1213 // Snapshot14 snapshot, count := cl.Snapshot()
1 unmodified line
16 t.Error("Snapshot should be empty now")17 }1819 // Add some data20 cl.Push([]rune("hello"))21 cl.Push([]rune("world"))20 cl.Push([]byte("hello"))21 cl.Push([]byte("world"))2223 // Previously created snapshot should remain the same24 if len(snapshot) > 0 {25 t.Error("Snapshot should not have changed")
19 unmodified lines
45 }4647 // Add more data48 for i := 0; i < chunkSize*2; i++ {49 cl.Push([]rune(fmt.Sprintf("item %d", i)))49 cl.Push([]byte(fmt.Sprintf("item %d", i)))50 }5152 // Previous snapshot should remain the same53 if len(snapshot) != 1 {
9 unmodified lines
63 if len(*snapshot[2]) != 2 {64 t.Error("Unexpected number of items")65 }6667 cl.Push([]rune("hello"))68 cl.Push([]rune("world"))67 cl.Push([]byte("hello"))68 cl.Push([]byte("world"))6970 lastChunkCount := len(*snapshot[len(snapshot)-1])71 if lastChunkCount != 2 {72 t.Error("Unexpected number of items:", lastChunkCount)
2 unmodified lines
modified:src/chunklist.go+3 added-3 removed
5 unmodified lines
6type Chunk []*Item // >>> []Item78// ItemBuilder is a closure type that builds Item object from a pointer to a9// string and an integer10type ItemBuilder func([]rune, int) *Item10type ItemBuilder func([]byte, int) *Item1112// ChunkList is a list of Chunks13type ChunkList struct {14 chunks []*Chunk
10 unmodified lines
25 mutex: sync.Mutex{},26 trans: trans}27}2829func (c *Chunk) push(trans ItemBuilder, data []rune, index int) bool {29func (c *Chunk) push(trans ItemBuilder, data []byte, index int) bool {30 item := trans(data, index)31 if item != nil {32 *c = append(*c, item)33 return true
18 unmodified lines
52 return chunkSize*(len(cs)-1) + len(*(cs[len(cs)-1]))53}5455// Push adds the item to the list56func (cl *ChunkList) Push(data []rune) bool {56func (cl *ChunkList) Push(data []byte) bool {57 cl.mutex.Lock()58 defer cl.mutex.Unlock()5960 if len(cl.chunks) == 0 || cl.lastChunk().IsFull() {
33 unmodified lines
modified:src/core.go+22 added-16 removed
61 unmodified lines
62 // Event channel63 eventBox := util.NewEventBox()6465 // ANSI code processor66 ansiProcessor := func(runes []rune) ([]rune, []ansiOffset) {67 // By default, we do nothing68 return runes, nil66 ansiProcessor := func(data []byte) ([]rune, []ansiOffset) {67 return util.BytesToRunes(data), nil68 }69 ansiProcessorRunes := func(data []rune) ([]rune, []ansiOffset) {70 return data, nil71 }72 if opts.Ansi {73 if opts.Theme != nil {74 var state *ansiState73 ansiProcessor = func(runes []rune) ([]rune, []ansiOffset) {74 trimmed, offsets, newState := extractColor(string(runes), state)75 ansiProcessor = func(data []byte) ([]rune, []ansiOffset) {76 trimmed, offsets, newState := extractColor(string(data), state)77 state = newState78 return []rune(trimmed), offsets79 }80 } else {81 // When color is disabled but ansi option is given,82 // we simply strip out ANSI codes from the input81 ansiProcessor = func(runes []rune) ([]rune, []ansiOffset) {82 trimmed, _, _ := extractColor(string(runes), nil)83 ansiProcessor = func(data []byte) ([]rune, []ansiOffset) {84 trimmed, _, _ := extractColor(string(data), nil)85 return []rune(trimmed), nil86 }87 }88 ansiProcessorRunes = func(data []rune) ([]rune, []ansiOffset) {89 return ansiProcessor([]byte(string(data)))90 }91 }9293 // Chunk list94 var chunkList *ChunkList95 header := make([]string, 0, opts.HeaderLines)96 if len(opts.WithNth) == 0 {92 chunkList = NewChunkList(func(data []rune, index int) *Item {97 chunkList = NewChunkList(func(data []byte, index int) *Item {98 if len(header) < opts.HeaderLines {99 header = append(header, string(data))100 eventBox.Set(EvtHeader, header)101 return nil102 }98 data, colors := ansiProcessor(data)103 runes, colors := ansiProcessor(data)104 return &Item{100 text: data,105 text: runes,106 index: uint32(index),107 colors: colors,108 rank: Rank{0, 0, uint32(index)}}109 })110 } else {106 chunkList = NewChunkList(func(data []rune, index int) *Item {107 tokens := Tokenize(data, opts.Delimiter)111 chunkList = NewChunkList(func(data []byte, index int) *Item {112 runes := util.BytesToRunes(data)113 tokens := Tokenize(runes, opts.Delimiter)114 trans := Transform(tokens, opts.WithNth)115 if len(header) < opts.HeaderLines {116 header = append(header, string(joinTokens(trans)))117 eventBox.Set(EvtHeader, header)118 return nil119 }120 item := Item{121 text: joinTokens(trans),116 origText: &data,122 origText: &runes,123 index: uint32(index),124 colors: nil,125 rank: Rank{0, 0, uint32(index)}}126121 trimmed, colors := ansiProcessor(item.text)127 trimmed, colors := ansiProcessorRunes(item.text)128 item.text = trimmed129 item.colors = colors130 return &item131 })
1 unmodified line
133134 // Reader135 streamingFilter := opts.Filter != nil && !sort && !opts.Tac && !opts.Sync136 if !streamingFilter {131 reader := Reader{func(data []rune) bool {137 reader := Reader{func(data []byte) bool {138 return chunkList.Push(data)139 }, eventBox, opts.ReadZero}140 go reader.ReadSource()141 }
14 unmodified lines
156 pattern := patternBuilder([]rune(*opts.Filter))157158 if streamingFilter {159 reader := Reader{154 func(runes []rune) bool {160 func(runes []byte) bool {161 item := chunkList.trans(runes, 0)162 if item != nil && pattern.MatchItem(item) {163 fmt.Println(string(item.text))164 }
109 unmodified lines
modified:src/reader_test.go+1 added-1 removed
8 unmodified lines
9func TestReadFromCommand(t *testing.T) {10 strs := []string{}11 eb := util.NewEventBox()12 reader := Reader{13 pusher: func(s []rune) bool { strs = append(strs, string(s)); return true },13 pusher: func(s []byte) bool { strs = append(strs, string(s)); return true },14 eventBox: eb}1516 // Check EventBox17 if eb.Peek(EvtReadNew) {
39 unmodified lines
modified:src/reader.go+3 added-15 removed
3 unmodified lines
4 "bufio"5 "io"6 "os"7 "os/exec"8 "unicode/utf8"89 "github.com/junegunn/fzf/src/util"10)1112// Reader reads from command or standard input13type Reader struct {15 pusher func([]rune) bool14 pusher func([]byte) bool15 eventBox *util.EventBox16 delimNil bool17}18
21 unmodified lines
40 // ReadBytes returns err != nil if and only if the returned data does not41 // end in delim.42 bytea, err := reader.ReadBytes(delim)43 if len(bytea) > 0 {45 runes := make([]rune, 0, len(bytea))46 for i := 0; i < len(bytea); {47 if bytea[i] < utf8.RuneSelf {48 runes = append(runes, rune(bytea[i]))49 i++50 } else {51 r, sz := utf8.DecodeRune(bytea[i:])52 i += sz53 runes = append(runes, r)54 }55 }44 if err == nil {57 runes = runes[:len(runes)-1]45 bytea = bytea[:len(bytea)-1]46 }59 if r.pusher(runes) {47 if r.pusher(bytea) {48 r.eventBox.Set(EvtReadNew, nil)49 }50 }51 if err != nil {
22 unmodified lines