From 17c84383f75c3dd54e18aea62f41c8868a4945be Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 26 Jun 2025 19:21:04 +0530 Subject: [PATCH] Dont store abspath on each result Optimisation as abspath is calculated in hot path. Instead calculate on demand. --- kittens/choose_files/main.go | 2 +- kittens/choose_files/results.go | 3 ++- kittens/choose_files/scan.go | 13 ++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/kittens/choose_files/main.go b/kittens/choose_files/main.go index c4808ba0a..5f6cf7b81 100644 --- a/kittens/choose_files/main.go +++ b/kittens/choose_files/main.go @@ -230,7 +230,7 @@ func (h *Handler) current_abspath() string { matches, _ := h.get_results() if len(matches) > 0 { if idx := h.state.CurrentIndex(); idx < len(matches) { - return matches[idx].abspath + return filepath.Join(h.state.CurrentDir(), matches[idx].text) } } return "" diff --git a/kittens/choose_files/results.go b/kittens/choose_files/results.go index edec49c33..f35cdbdfc 100644 --- a/kittens/choose_files/results.go +++ b/kittens/choose_files/results.go @@ -123,10 +123,11 @@ func icon_for(path string, x os.FileMode) string { } func (h *Handler) draw_column_of_matches(matches ResultsType, current_idx int, x, available_width int) { + root_dir := h.state.CurrentDir() for i, m := range matches { h.lp.QueueWriteString("\r") h.lp.MoveCursorHorizontally(x) - icon := icon_for(m.abspath, m.ftype) + icon := icon_for(filepath.Join(root_dir, m.text), m.ftype) text := m.text add_ellipsis := false if wcswidth.Stringwidth(text) > available_width-3 { diff --git a/kittens/choose_files/scan.go b/kittens/choose_files/scan.go index 8df6fde64..22dbefe0e 100644 --- a/kittens/choose_files/scan.go +++ b/kittens/choose_files/scan.go @@ -29,10 +29,10 @@ func (c CombinedScore) String() string { } type ResultItem struct { - text, abspath string - ftype fs.FileMode - positions []int // may be nil - score CombinedScore + text string + ftype fs.FileMode + positions []int // may be nil + score CombinedScore } type ResultsType []*ResultItem @@ -46,7 +46,7 @@ func (r ResultItem) IsMatching() bool { } func (r ResultItem) String() string { - return fmt.Sprintf("{text: %#v, abspath: %#v, %s, positions: %#v}", r.text, r.abspath, r.score, r.positions) + return fmt.Sprintf("{text: %#v, %s, positions: %#v}", r.text, r.score, r.positions) } func (r *ResultItem) sorted_positions() []int { @@ -261,7 +261,6 @@ func (fss *FileSystemScanner) worker() { new_items := ns[len(ns):new_sz] for i, e := range sortable { new_items[i].ftype = e.ftype - new_items[i].abspath = dir + e.name new_items[i].text = base + e.name new_items[i].score.Set_index(idx) idx++ @@ -280,8 +279,8 @@ func (fss *FileSystemScanner) worker() { dir = "" for pos < len(fss.results) && dir == "" { if fss.results[pos].ftype&fs.ModeDir != 0 { - dir = fss.results[pos].abspath + string(os.PathSeparator) base = fss.results[pos].text + string(os.PathSeparator) + dir = root_dir + string(os.PathSeparator) + base } pos++ }