mirror of
https://github.com/junegunn/fzf.git
synced 2025-11-15 14:53:47 -05:00
Micro-optimizations
- Make structs smaller - Introduce Result struct and use it to represent matched items instead of reusing Item struct for that purpose - Avoid unnecessary memory allocation - Avoid growing slice from the initial capacity - Code cleanup
This commit is contained in:
@@ -3,13 +3,13 @@ package fzf
|
||||
import "fmt"
|
||||
|
||||
// EmptyMerger is a Merger with no data
|
||||
var EmptyMerger = NewMerger([][]*Item{}, false, false)
|
||||
var EmptyMerger = NewMerger([][]*Result{}, false, false)
|
||||
|
||||
// Merger holds a set of locally sorted lists of items and provides the view of
|
||||
// a single, globally-sorted list
|
||||
type Merger struct {
|
||||
lists [][]*Item
|
||||
merged []*Item
|
||||
lists [][]*Result
|
||||
merged []*Result
|
||||
chunks *[]*Chunk
|
||||
cursors []int
|
||||
sorted bool
|
||||
@@ -33,10 +33,10 @@ func PassMerger(chunks *[]*Chunk, tac bool) *Merger {
|
||||
}
|
||||
|
||||
// NewMerger returns a new Merger
|
||||
func NewMerger(lists [][]*Item, sorted bool, tac bool) *Merger {
|
||||
func NewMerger(lists [][]*Result, sorted bool, tac bool) *Merger {
|
||||
mg := Merger{
|
||||
lists: lists,
|
||||
merged: []*Item{},
|
||||
merged: []*Result{},
|
||||
chunks: nil,
|
||||
cursors: make([]int, len(lists)),
|
||||
sorted: sorted,
|
||||
@@ -55,14 +55,14 @@ func (mg *Merger) Length() int {
|
||||
return mg.count
|
||||
}
|
||||
|
||||
// Get returns the pointer to the Item object indexed by the given integer
|
||||
func (mg *Merger) Get(idx int) *Item {
|
||||
// Get returns the pointer to the Result object indexed by the given integer
|
||||
func (mg *Merger) Get(idx int) *Result {
|
||||
if mg.chunks != nil {
|
||||
if mg.tac {
|
||||
idx = mg.count - idx - 1
|
||||
}
|
||||
chunk := (*mg.chunks)[idx/chunkSize]
|
||||
return (*chunk)[idx%chunkSize]
|
||||
return &Result{item: (*chunk)[idx%chunkSize]}
|
||||
}
|
||||
|
||||
if mg.sorted {
|
||||
@@ -86,9 +86,9 @@ func (mg *Merger) cacheable() bool {
|
||||
return mg.count < mergerCacheMax
|
||||
}
|
||||
|
||||
func (mg *Merger) mergedGet(idx int) *Item {
|
||||
func (mg *Merger) mergedGet(idx int) *Result {
|
||||
for i := len(mg.merged); i <= idx; i++ {
|
||||
minRank := buildEmptyRank(0)
|
||||
minRank := minRank()
|
||||
minIdx := -1
|
||||
for listIdx, list := range mg.lists {
|
||||
cursor := mg.cursors[listIdx]
|
||||
@@ -97,7 +97,7 @@ func (mg *Merger) mergedGet(idx int) *Item {
|
||||
continue
|
||||
}
|
||||
if cursor >= 0 {
|
||||
rank := list[cursor].Rank(false)
|
||||
rank := list[cursor].rank
|
||||
if minIdx < 0 || compareRanks(rank, minRank, mg.tac) {
|
||||
minRank = rank
|
||||
minIdx = listIdx
|
||||
|
||||
Reference in New Issue
Block a user