-
Notifications
You must be signed in to change notification settings - Fork 46
Description
Description
Regex patterns are being compiled inside functions that are called repeatedly during workflow compilation, causing unnecessary O(n) overhead. Moving regex compilation to package-level variables would eliminate this performance bottleneck.
Problem
In pkg/workflow/compiler_jobs.go:455, regexp.MustCompile() is called inside the containsRuntimeImports() function:
macroPattern := `\{\{#runtime-import\??[ \t]+([^\}]+)\}\}`
macroRe := regexp.MustCompile(macroPattern) // ❌ Compiled every call
matches := macroRe.FindAllStringSubmatch(markdownContent, -1)Impact:
- Function called per-job during workflow compilation (potentially 100+ times for complex workflows)
- Each call recompiles identical regex pattern
- O(n) compilation overhead in hot path
- Unnecessary CPU cycles and memory allocation
Additional Instances
These files also require review for similar patterns:
pkg/workflow/expression_extraction.go:45-expressionRegexcompiled in functionpkg/workflow/template_validation.go:50-templateRegionPatterncompiled in functionpkg/workflow/repo_memory.go:77-validPatterncompiled in function
Suggested Changes
Move regex compilation to package-level var declarations (compile once at init time):
// At package level (top of compiler_jobs.go):
var (
compilerJobsLog = logging.MustGetLogger("workflow.compiler.jobs")
// Regex patterns compiled once at init
runtimeImportMacroRe = regexp.MustCompile(`\{\{#runtime-import\??[ \t]+([^\}]+)\}\}`)
)
// In containsRuntimeImports function:
matches := runtimeImportMacroRe.FindAllStringSubmatch(markdownContent, -1)Reference Implementation
Good example already in the codebase:
pkg/workflow/expression_validation.go:65-69- All regexes at package level ✅
Files to Update
pkg/workflow/compiler_jobs.go:455(confirmed critical)pkg/workflow/expression_extraction.go:45(requires review)pkg/workflow/template_validation.go:50(requires review)pkg/workflow/repo_memory.go:77(requires review)
Success Criteria
- All in-function regex compilation moved to package level
- No compilation inside loops or frequently-called functions
- Benchmark shows improved compilation time for large workflows (100+ jobs)
- Memory allocation reduced:
go test -bench=. -benchmem - All existing tests pass:
go test ./pkg/workflow/... -v - Verify no in-function compilation:
grep -rn "regexp.MustCompile" pkg/workflow/*.go | grep -v "var.*=" -
make agent-finishpasses
Priority
High - Eliminates O(n) compilation overhead in workflow compilation hot path
Estimated Effort: Small (1-2 hours)
Source
Extracted from Sergo Performance Optimization Analysis - Discussion #11840
Analysis Quote:
"Regex compilation in hot paths causing O(n) overhead during workflow compilation"
Performance Impact:
"Eliminate O(n) regex compilation overhead in workflow compilation"
References:
AI generated by Discussion Task Miner - Code Quality Improvement Agent
- expires on Feb 16, 2026, 1:28 PM UTC