Skip to content

fix(perf): hoist regexp.MustCompile outside loop in validateExpressionForDangerousProps#20070

Closed
JasonOA888 wants to merge 1 commit intogithub:mainfrom
JasonOA888:fix/issue-20026-regexp-performance
Closed

fix(perf): hoist regexp.MustCompile outside loop in validateExpressionForDangerousProps#20070
JasonOA888 wants to merge 1 commit intogithub:mainfrom
JasonOA888:fix/issue-20026-regexp-performance

Conversation

@JasonOA888
Copy link

Fixes #20026

Problem

Two regexp.MustCompile calls in validateExpressionForDangerousProps:

  1. Line 212: regexp.MustCompile("[.\\[\\]]+") - called per function invocation
  2. Line 216: regexp.MustCompile("^\\d+$") - called inside for loop, per iteration

Since validateSingleExpression is recursive and called for every expression in every compiled workflow, these allocations accumulate significantly.

Solution

Hoist both regex patterns to package-level var declarations alongside existing regex vars:

var (
    exprPartSplitRe   = regexp.MustCompile(`[.\[\]]+`)
    exprNumericPartRe = regexp.MustCompile(`^\d+$`)
)

Replace inline MustCompile calls with references to these pre-compiled patterns.

Impact

  • Eliminates repeated regex compilation in hot validation path
  • No functional changes, only performance optimization
  • Follows existing pattern in the file (lines 66-79)

Reported in discussion #19993 (Sergo audit).

@JasonOA888 JasonOA888 force-pushed the fix/issue-20026-regexp-performance branch from 6434e2c to 502aa1d Compare March 8, 2026 14:00
Fixes github#20026

Problem:
- validateExpressionForDangerousProps called regexp.MustCompile twice
- One call was inside a for loop (per-iteration allocation)
- This is called for every expression in every compiled workflow
- Regex allocations accumulated significantly in hot path

Solution:
- Added exprPartSplitRe and exprNumericPartRe as package-level vars
- These are compiled once at init time, not per-call/iteration
- Replaced inline MustCompile calls with references to these vars

Impact:
- Eliminates repeated regex compilation in expression validation
- Reduces allocations in hot path
- No behavior change, pure performance optimization
@JasonOA888 JasonOA888 force-pushed the fix/issue-20026-regexp-performance branch from 502aa1d to 7598346 Compare March 8, 2026 14:01
@pelikhan pelikhan closed this Mar 8, 2026
@pelikhan
Copy link
Contributor

pelikhan commented Mar 8, 2026

Please contribution guide.

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.

[plan] Fix regexp.MustCompile inside for loop in validateExpressionForDangerousProps

2 participants