Skip to content

[Code Quality] Compile Regexes at Package Level to Avoid Hot-Path Recompilation #13277

@github-actions

Description

@github-actions

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 - expressionRegex compiled in function
  • pkg/workflow/template_validation.go:50 - templateRegionPattern compiled in function
  • pkg/workflow/repo_memory.go:77 - validPattern compiled 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

  1. pkg/workflow/compiler_jobs.go:455 (confirmed critical)
  2. pkg/workflow/expression_extraction.go:45 (requires review)
  3. pkg/workflow/template_validation.go:50 (requires review)
  4. 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-finish passes

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions