superpowers icon indicating copy to clipboard operation
superpowers copied to clipboard

perf: optimize session-start hook performance

Open marovole opened this issue 10 months ago • 1 comments

Summary

Replace O(N) character-by-character JSON escaping with optimized approach using jq > python3 > sed fallback chain.

Performance Impact

Metric Before After Improvement
Time for 3.5KB file ~0.24s ~0.02s 12x faster

This optimization significantly improves Claude Code startup time when the superpowers plugin is installed.

Changes

  • Replace escape_for_json() function that iterates character-by-character (O(N) complexity)
  • Add escape_file_for_json() for file content (reads from file directly, uses jq)
  • Add escape_string_for_json() for small strings (like warning messages)
  • Uses tool priority: jq (fastest), python3 (fallback), or sed (last resort)

Test Plan

  • [x] Tested locally with time bash hooks/run-hook.cmd session-start.sh
  • [x] Verified JSON output is correctly escaped
  • [x] Tested with and without jq installed

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Improved JSON content escaping utilities for enhanced performance and reliability.

✏️ Tip: You can customize this high-level summary in your review settings.

marovole avatar Jan 12 '26 16:01 marovole

📝 Walkthrough

Walkthrough

The hooks/session-start.sh file has been refactored to improve JSON escaping logic by introducing two dedicated helper functions—escape_file_for_json() and escape_string_for_json()—that replace previous inline escaping with a tiered approach (jq, Python, then sed fallback). A new file-path variable for the using-superpowers content is introduced to support this new function-based pattern.

Changes

Cohort / File(s) Summary
JSON Escaping Refactor
hooks/session-start.sh
Introduced escape_file_for_json() and escape_string_for_json() helper functions with fallback support (jq → Python → sed). Replaced inline escaping logic with dedicated function calls. Added file-path variable for using-superpowers content. Updated comments to reflect new escaping strategy.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit script hops with care,
New functions now escape strings fair,
jq, Python, sed—a trio so wise,
JSON flows pure, with no surprise!
One file refined, the logic takes flight,
Cleaner and stronger, oh what delight!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'perf: optimize session-start hook performance' directly and accurately summarizes the main change: refactoring the session-start.sh hook for performance optimization through an improved JSON escaping implementation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • [ ] 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Jan 12 '26 16:01 coderabbitai[bot]

Tamam ya

huseyinsabuncu154-gif avatar Jan 14 '26 20:01 huseyinsabuncu154-gif

Thanks for the contribution! However, the current hook is intentionally pure bash with no external dependencies. We're not looking to add jq/python3 as runtime dependencies in the name of performance - the startup time is acceptable as-is and we value the simplicity of zero dependencies.

Appreciate you profiling and identifying the bottleneck though!

obra avatar Jan 14 '26 22:01 obra