Skip to content

[performance][medium] Cache parsed configuration to avoid repeated JSON reloading #4

Description

@upupwrite

Summary

The application repeatedly reads and parses the JSON configuration (File::loadConfig and related helpers) on many code paths (e.g., getCommandIndex, interactiveSearch). Re-parsing and reloading the config for frequent operations adds CPU and I/O overhead, especially for large configurations or when performing many interactive searches in a session.

Reproduction / evidence

  • src/loadfile.cpp: File::getCommandIndex calls loadConfig() at the start of the function. Similar patterns appear in other call sites that run during interactive flows.

Impact

  • Increased latency when listing/searching commands or when the CLI performs frequent operations.
  • Unnecessary disk I/O and CPU usage for repeated JSON parsing.

Suggested fixes / improvements

  1. Implement an in-memory config cache:

    • Load and parse the configuration once at startup or on first access and keep it in memory.
    • Invalidate and reload the cache only when the config file changes (watch file mtime or provide an explicit reload API).
  2. Use incremental updates for small modifications:

    • When the program modifies a single command or alias, update the in-memory structure and persist the change rather than reloading the entire file.
  3. Reduce JSON parsing overhead:

    • Consider using a faster JSON library or enable streaming parsing for large files. Profile current parsing to determine hotspots.

Files/places to review first

  • src/loadfile.cpp (loadConfig, saveConfig, getCommandIndex)
  • Any code that calls loadConfig frequently during interactive flows

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