perf: Move regex compilation to package level to eliminate hot-path overhead#11885
Merged
perf: Move regex compilation to package level to eliminate hot-path overhead#11885
Conversation
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Refactor regex compilation to package level
perf: Move regex compilation to package level to eliminate hot-path overhead
Jan 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Regex patterns were being recompiled on every function call in workflow compilation hot paths. For workflows with 100+ jobs, this caused O(n) compilation overhead.
Changes
Moved regex compilation to package-level variables in:
compiler_jobs.go-runtimeImportMacroRe(called per-job during compilation)expression_extraction.go-expressionExtractionRegex(called during expression mapping)template_validation.go-templateRegionPattern(called during template validation)repo_memory.go-branchPrefixValidPattern(called during repo-memory validation)Pattern:
Follows existing pattern from
expression_validation.go. Estimated ~99ms savings on 100-job workflows.Original prompt
This section details on the original issue you should resolve
<issue_title>[Code Quality] Move Regex Compilation to Package Level to Avoid Hot-Path Recompilation</issue_title>
<issue_description>## Description
The workflow compilation pipeline recompiles identical regex patterns on every function call instead of compiling once at package initialization, causing O(n) compilation overhead during workflow builds.
Problem
In
pkg/workflow/compiler_jobs.go:455, thecontainsRuntimeImports()function callsregexp.MustCompile()inside the function:This function is called per-job during workflow compilation. For workflows with 100+ jobs, the same regex is compiled 100+ times.
Impact
Benchmark Impact (Estimated)
Additional Instances to Fix
Found in performance analysis:
pkg/workflow/compiler_jobs.go:455- CONFIRMED CRITICAL (per-job hot path)pkg/workflow/expression_extraction.go:45-expressionRegex(needs review)pkg/workflow/template_validation.go:50-templateRegionPattern(needs review)pkg/workflow/repo_memory.go:77-validPattern(needs review)Good Example Already in Codebase
pkg/workflow/expression_validation.go:65-69already follows best practice:Suggested Changes
Move regex compilation to package-level variable declarations:
Files to Update
Confirmed Critical:
pkg/workflow/compiler_jobs.go:455- Hot path per-job compilationTo Review:
pkg/workflow/expression_extraction.go:45pkg/workflow/template_validation.go:50pkg/workflow/repo_memory.go:77Reference (already correct):
pkg/workflow/expression_validation.go- Follow this pattern ✅Success Criteria
regexp.MustCompile()calls moved to package levelgo test -bench=BenchmarkCompile -benchmem-benchmem(should decrease)go test ./pkg/workflow/... -vgrep -rn "regexp.MustCompile" pkg/workflow/*.go | grep -v "var.*="make agent-finishpassesPriority
High - Performance optimization with clear benefit and low risk
Estimated Effort: Small (2-3 hours including verification across all files)
Source
Extracted from Sergo Performance Optimization Analysis - Discussion githubnext/gh-aw#11840
Analysis Quote:
Impact Assessment:
References:
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.