mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-15 04:05:48 -07:00
Fix #151 - reduce initial memory footprint
This commit is contained in:
@@ -134,6 +134,10 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
|
|||||||
}
|
}
|
||||||
pattern := request.pattern
|
pattern := request.pattern
|
||||||
empty := pattern.IsEmpty()
|
empty := pattern.IsEmpty()
|
||||||
|
if empty {
|
||||||
|
return PassMerger(&request.chunks, m.tac), false
|
||||||
|
}
|
||||||
|
|
||||||
cancelled := util.NewAtomicBool(false)
|
cancelled := util.NewAtomicBool(false)
|
||||||
|
|
||||||
slices := m.sliceChunks(request.chunks)
|
slices := m.sliceChunks(request.chunks)
|
||||||
|
@@ -10,6 +10,7 @@ var EmptyMerger = NewMerger([][]*Item{}, false, false)
|
|||||||
type Merger struct {
|
type Merger struct {
|
||||||
lists [][]*Item
|
lists [][]*Item
|
||||||
merged []*Item
|
merged []*Item
|
||||||
|
chunks *[]*Chunk
|
||||||
cursors []int
|
cursors []int
|
||||||
sorted bool
|
sorted bool
|
||||||
tac bool
|
tac bool
|
||||||
@@ -17,11 +18,24 @@ type Merger struct {
|
|||||||
count int
|
count int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PassMerger(chunks *[]*Chunk, tac bool) *Merger {
|
||||||
|
mg := Merger{
|
||||||
|
chunks: chunks,
|
||||||
|
tac: tac,
|
||||||
|
count: 0}
|
||||||
|
|
||||||
|
for _, chunk := range *mg.chunks {
|
||||||
|
mg.count += len(*chunk)
|
||||||
|
}
|
||||||
|
return &mg
|
||||||
|
}
|
||||||
|
|
||||||
// NewMerger returns a new Merger
|
// NewMerger returns a new Merger
|
||||||
func NewMerger(lists [][]*Item, sorted bool, tac bool) *Merger {
|
func NewMerger(lists [][]*Item, sorted bool, tac bool) *Merger {
|
||||||
mg := Merger{
|
mg := Merger{
|
||||||
lists: lists,
|
lists: lists,
|
||||||
merged: []*Item{},
|
merged: []*Item{},
|
||||||
|
chunks: nil,
|
||||||
cursors: make([]int, len(lists)),
|
cursors: make([]int, len(lists)),
|
||||||
sorted: sorted,
|
sorted: sorted,
|
||||||
tac: tac,
|
tac: tac,
|
||||||
@@ -41,12 +55,20 @@ func (mg *Merger) Length() int {
|
|||||||
|
|
||||||
// Get returns the pointer to the Item object indexed by the given integer
|
// Get returns the pointer to the Item object indexed by the given integer
|
||||||
func (mg *Merger) Get(idx int) *Item {
|
func (mg *Merger) Get(idx int) *Item {
|
||||||
|
if mg.chunks != nil {
|
||||||
|
if mg.tac {
|
||||||
|
idx = mg.count - idx - 1
|
||||||
|
}
|
||||||
|
chunk := (*mg.chunks)[idx/ChunkSize]
|
||||||
|
return (*chunk)[idx%ChunkSize]
|
||||||
|
}
|
||||||
|
|
||||||
if mg.sorted {
|
if mg.sorted {
|
||||||
return mg.mergedGet(idx)
|
return mg.mergedGet(idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
if mg.tac {
|
if mg.tac {
|
||||||
idx = mg.Length() - idx - 1
|
idx = mg.count - idx - 1
|
||||||
}
|
}
|
||||||
for _, list := range mg.lists {
|
for _, list := range mg.lists {
|
||||||
numItems := len(list)
|
numItems := len(list)
|
||||||
|
Reference in New Issue
Block a user