Skip to content

[performance][medium] Reduce allocations and string copies when generating command lists #5

Description

@upupwrite

Summary

Interactive flows such as generateList()/interactiveSearch build strings and data structures each time a search is performed. For large numbers of commands or directories, string allocations and copying may dominate runtime and cause latency. The UI builds concatenated strings for display each time.

Reproduction / evidence

  • src/search.cpp: generateList constructs a long string by iterating all paths and commands. Each invocation will allocate memory proportional to the total size.

Impact

  • Increased latency for generating lists and for interactive sessions with large datasets.
  • Higher memory churn leads to garbage in runtime and CPU overhead.

Suggested fixes / improvements

  1. Use more efficient data structures for display generation:

    • Avoid building a single giant std::string; use streaming to stdout or buffered writers that pre-allocate capacity when the size is known.
    • Reuse buffers across calls when possible.
  2. Implement pagination / lazy evaluation:

    • Only generate and render portions of the list visible to the user (or requested), especially when integrated with fzf or pagers.
  3. Profile hot paths and optimize critical loops:

    • Use tools like perf or valgrind to find hotspots and inline small helpers, minimize copies, and use references where appropriate.

Files/places to review first

  • src/search.cpp (generateList, interactiveSearch)

Severity: medium

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions