Skip to content

Optimize memory usage and performance in fuzzy search (help menu + sidebar) #1026

@coderabbitai

Description

@coderabbitai

Problem Description

Memory leak and performance issues in fuzzy search functionality affecting help menu (PR #1011) and potentially sidebar search.

SEVERE: Memory Leak in Search Operations

Location: src/internal/handle_modal.go in fuzzySearch() function

Issue: New slice allocations on every keystroke without cleanup:

haystack := []string{}          // New allocation every keystroke
idxMap := []int{}              // New allocation every keystroke
results := []helpMenuModalData{} // New allocation every keystroke

Impact:

  • Memory usage grows with each search operation
  • Garbage collection pressure increases
  • Performance degrades during extended search sessions

Current State

  • Similar pattern likely exists in sidebar fuzzy search
  • No debouncing implemented for rapid typing
  • Existing optimization comments in codebase acknowledge this issue

Suggested Solutions

1. Memory Pool Implementation

Reuse slices to reduce GC pressure:

type searchPool struct {
    haystack []string
    idxMap   []int  
    results  []helpMenuModalData
}

func (p *searchPool) reset() {
    p.haystack = p.haystack[:0]
    p.idxMap = p.idxMap[:0] 
    p.results = p.results[:0]
}

2. Input Debouncing

Implement 250ms debouncing to reduce excessive filtering:

// Debounce search input to avoid excessive filtering
searchDebouncer := time.NewTimer(250 * time.Millisecond)

3. Capacity Pre-allocation

Pre-allocate slices with expected capacity to reduce reallocations.

4. Performance Monitoring

Add metrics to track:

  • Search operation duration
  • Memory allocation patterns
  • GC frequency during search

Scope

This optimization should be applied to:

  • Help menu fuzzy search (immediate)
  • Sidebar search functionality (if similar pattern exists)
  • Any other fuzzy search implementations in the codebase

Acceptance Criteria

  • Memory allocations reduced by implementing slice reuse
  • Input debouncing implemented (250ms)
  • Performance testing shows improved search responsiveness
  • Memory usage remains stable during extended search sessions
  • Existing optimization TODOs in code are addressed

References

Metadata

Metadata

Assignees

Labels

performanceIssue related to performance of superfile

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions