Skip to content

Conversation

@abtreece
Copy link
Owner

Summary

  • Pre-compile check_cmd and reload_cmd templates at construction time instead of parsing on every execution
  • Detect invalid command templates early at initialization rather than at runtime
  • Add benchmarks comparing pre-compiled vs parse-per-call approaches

Benchmark Results

Approach Time Memory Allocations
Pre-compiled (new) ~305 ns/op 320 B/op 8
Parse-per-call (old) ~3010 ns/op 6157 B/op 64

Improvement: 10x faster, 19x less memory, 8x fewer allocations

Test plan

  • All existing command executor tests pass
  • Invalid template tests updated to expect errors at construction time
  • Full test suite passes (go test ./...)
  • Build succeeds (make build)
  • Benchmarks added to verify performance improvement

Fixes #444

Command templates (check_cmd, reload_cmd) were being parsed on every
execution, even though the template strings are static. This change
pre-compiles templates at construction time for efficiency.

Benefits:
- 10x faster template execution (~305 ns vs ~3010 ns)
- 19x less memory usage (320 B vs 6157 B per execution)
- 8x fewer allocations (8 vs 64 per execution)
- Early detection of invalid command templates at initialization

Changes:
- Add checkCmdTmpl and reloadCmdTmpl fields to commandExecutor
- Parse templates in newCommandExecutor(), return error on failure
- Update executeCheck/executeReload to use pre-compiled templates
- Update callers in resource.go to handle construction errors
- Add benchmarks comparing pre-compiled vs parse-per-call

Fixes #444
Copilot AI review requested due to automatic review settings January 22, 2026 03:46
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves performance of command template handling by pre-compiling check and reload command templates at initialization rather than parsing them on every execution. Invalid templates are now detected early during construction, improving error feedback.

Changes:

  • Pre-compile command templates in commandExecutor constructor
  • Update newCommandExecutor to return error for invalid templates
  • Refactor tests to handle new constructor signature and verify early error detection

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
pkg/template/command_executor.go Added pre-compiled template fields and template parsing logic to constructor
pkg/template/command_executor_test.go Updated tests to handle error-returning constructor and moved template validation tests to construction time
pkg/template/resource.go Added error handling for newCommandExecutor calls in three locations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link

codecov bot commented Jan 22, 2026

Codecov Report

❌ Patch coverage is 85.18519% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.76%. Comparing base (408f57b) to head (2bd5bf2).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pkg/template/command_executor.go 86.66% 0 Missing and 2 partials ⚠️
pkg/template/resource.go 83.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #456      +/-   ##
==========================================
- Coverage   59.80%   59.76%   -0.05%     
==========================================
  Files          48       48              
  Lines        5230     5244      +14     
==========================================
+ Hits         3128     3134       +6     
- Misses       1897     1904       +7     
- Partials      205      206       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@abtreece abtreece merged commit ea56f0f into main Jan 22, 2026
11 checks passed
@abtreece abtreece deleted the perf/precompile-command-templates branch January 22, 2026 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pre-compile command templates in CommandExecutor

1 participant