Remove pointer indirection by changing Chunk definition
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 7b5ccc4 · Browse files · 7 files·+29 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
7 files+29 added−23 removed
- other:+29 added−23 removed
Provenance
- Commit
d4f3d5a- Tree
2cb3f95- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
7b5ccc4
7 files changed+29 added−23 removed
modified:src/chunklist_test.go+2 added-2 removed
9 unmodified lines10 func TestChunkList(t *testing.T) {11 // FIXME global12 sortCriteria = []criterion{byScore, byLength}13 14- cl := NewChunkList(func(s []byte, i int) *Item {15- return &Item{text: util.ToChars(s), index: int32(i * 2)}14+ cl := NewChunkList(func(s []byte, i int) Item {15+ return Item{text: util.ToChars(s), index: int32(i * 2)}16 })17 18 // Snapshot19 snapshot, count := cl.Snapshot()60 unmodified linesmodified:src/chunklist.go+8 added-8 removed
1 package fzf2 3 import "sync"4 5-// Chunk is a list of Item pointers whose size has the upper limit of chunkSize6-type Chunk []*Item // >>> []Item5+// Chunk is a list of Items whose size has the upper limit of chunkSize6+type Chunk []Item7 8 // ItemBuilder is a closure type that builds Item object from a pointer to a9 // string and an integer10-type ItemBuilder func([]byte, int) *Item10+type ItemBuilder func([]byte, int) Item11 12 // ChunkList is a list of Chunks13 type ChunkList struct {14 chunks []*Chunk12 unmodified lines27 }28 29 func (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 true31+ if item.Nil() {32+ return false33 }34+ *c = append(*c, item)35- return false35+ return true36 }37 38 // IsFull returns true if the Chunk is full39 func (c *Chunk) IsFull() bool {17 unmodified lines57 cl.mutex.Lock()58 defer cl.mutex.Unlock()59 60 if len(cl.chunks) == 0 || cl.lastChunk().IsFull() {61- newChunk := Chunk(make([]*Item, 0, chunkSize))61+ newChunk := Chunk(make([]Item, 0, chunkSize))62 cl.chunks = append(cl.chunks, &newChunk)63 }64 65 if cl.lastChunk().push(cl.trans, data, cl.count) {28 unmodified linesmodified:src/core.go+8 added-8 removed
89 unmodified lines90 // Chunk list91 var chunkList *ChunkList92 header := make([]string, 0, opts.HeaderLines)93 if len(opts.WithNth) == 0 {94- chunkList = NewChunkList(func(data []byte, index int) *Item {94+ chunkList = NewChunkList(func(data []byte, index int) Item {95 if len(header) < opts.HeaderLines {96 header = append(header, string(data))97 eventBox.Set(EvtHeader, header)98- return nil98+ return nilItem99 }100 chars, colors := ansiProcessor(data)101- return &Item{101+ return Item{102 index: int32(index),103 trimLength: -1,104 text: chars,105 colors: colors}106 })107 } else {108- chunkList = NewChunkList(func(data []byte, index int) *Item {108+ chunkList = NewChunkList(func(data []byte, index int) Item {109 tokens := Tokenize(util.ToChars(data), opts.Delimiter)110 trans := Transform(tokens, opts.WithNth)111 if len(header) < opts.HeaderLines {112 header = append(header, string(joinTokens(trans)))113 eventBox.Set(EvtHeader, header)114- return nil114+ return nilItem115 }116 textRunes := joinTokens(trans)117 item := Item{118 index: int32(index),3 unmodified lines122 123 trimmed, colors := ansiProcessorRunes(textRunes)124 item.text = trimmed125 item.colors = colors126- return &item126+ return item127 })128 }129 130 // Reader36 unmodified lines167 slab := util.MakeSlab(slab16Size, slab32Size)168 reader := Reader{169 func(runes []byte) bool {170 item := chunkList.trans(runes, 0)171- if item != nil {172- if result, _, _ := pattern.MatchItem(item, false, slab); result != nil {171+ if !item.Nil() {172+ if result, _, _ := pattern.MatchItem(&item, false, slab); result != nil {173 opts.Printer(item.text.ToString())174 found = true175 }176 }116 unmodified linesmodified:src/item.go+6 added
17 unmodified lines18 func (item *Item) Index() int32 {19 return item.index20 }21 22+var nilItem = Item{index: -1}23+24+func (item *Item) Nil() bool {25+ return item.index < 026+}27+28 func (item *Item) TrimLength() int32 {29 if item.trimLength >= 0 {30 return item.trimLength31 }23 unmodified linesmodified:src/merger.go+1 added-1 removed
63 unmodified lines64 if mg.tac {65 idx = mg.count - idx - 166 }67 chunk := (*mg.chunks)[idx/chunkSize]68- return &Result{item: (*chunk)[idx%chunkSize]}68+ return &Result{item: &(*chunk)[idx%chunkSize]}69 }70 71 if mg.sorted {72 return mg.mergedGet(idx)49 unmodified linesmodified:src/pattern_test.go+2 added-2 removed
137 unmodified lines138 139 origBytes := []byte("junegunn.choi")140 for _, extended := range []bool{false, true} {141 chunk := Chunk{142- &Item{142+ Item{143 text: util.RunesToChars([]rune("junegunn")),144 origText: &origBytes,145 transformed: trans},146 }4 unmodified lines151 reflect.DeepEqual(matches[0].item.transformed, trans)) {152 t.Error("Invalid match result", matches)153 }154 155- match, offsets, pos := pattern.MatchItem(chunk[0], true, slab)155+ match, offsets, pos := pattern.MatchItem(&chunk[0], true, slab)156 if !(match.item.text.ToString() == "junegunn" &&157 string(*match.item.origText) == "junegunn.choi" &&158 offsets[0][0] == 0 && offsets[0][1] == 5 &&159 reflect.DeepEqual(match.item.transformed, trans)) {47 unmodified linesmodified:src/pattern.go+2 added-2 removed
279 unmodified lines280 func (p *Pattern) matchChunk(chunk *Chunk, space []*Result, slab *util.Slab) []*Result {281 matches := []*Result{}282 283 if space == nil {284- for _, item := range *chunk {285- if match, _, _ := p.MatchItem(item, false, slab); match != nil {284+ for idx := range *chunk {285+ if match, _, _ := p.MatchItem(&(*chunk)[idx], false, slab); match != nil {286 matches = append(matches, match)287 }288 }289 } else {109 unmodified lines