Skip to content

Fix storeTag regex performance bottleneck#697

Merged
darkweak merged 1 commit intodarkweak:masterfrom
cdaguerre:fix/regex-performance
Feb 12, 2026
Merged

Fix storeTag regex performance bottleneck#697
darkweak merged 1 commit intodarkweak:masterfrom
cdaguerre:fix/regex-performance

Conversation

@cdaguerre
Copy link
Contributor

Fixes #696

Warning: This was entirely done by Claude Code

Problem

The storeTag function was using regex matching to check for duplicate cache keys in the stored comma-separated list. This caused ~57% CPU consumption because:

  1. A regex was compiled on every Store() call
  2. The regex was executed against potentially very long strings containing thousands of cache keys
  3. Regex backtracking is O(n×m) complexity

Solution

Replace regex matching with simple string operations (strings.HasPrefix, strings.HasSuffix, strings.Contains) that provide O(n) complexity and leverage Go's optimized strings package.

Changes

  • Added containsCacheKey() helper function using fast string operations
  • Removed regex parameter from storeTag()
  • Removed per-request regexp.MustCompile() calls from Store()
  • Updated SurrogateInterface to reflect new signature
  • Added unit tests for containsCacheKey() covering edge cases
  • Applied same fix to traefik and tyk plugin overrides

@netlify
Copy link

netlify bot commented Jan 23, 2026

Deploy Preview for teal-sprinkles-4c7f14 canceled.

Name Link
🔨 Latest commit 69160ab
🔍 Latest deploy log https://app.netlify.com/projects/teal-sprinkles-4c7f14/deploys/698d9ec669eaa000083d971e

@darkweak darkweak force-pushed the fix/regex-performance branch from b2974ab to 69160ab Compare February 12, 2026 09:34
@darkweak darkweak merged commit 0b778c7 into darkweak:master Feb 12, 2026
19 of 21 checks passed
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.

Performance: storeTag regex matching consumes ~57% CPU even with dynamic surrogate keys only

2 participants