[PR #395] [MERGED] perf: cache compiled regexes in middleware matcher #539

Closed
opened 2026-05-06 13:08:38 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/395
Author: @NathanDrake2406
Created: 3/10/2026
Status: Merged
Merged: 3/10/2026
Merged by: @james-elicx

Base: mainHead: fix/middleware-regex-cache


📝 Commits (1)

  • fb52807 perf: cache compiled regexes in middleware matcher

📊 Changes

3 files changed (+67 additions, -37 deletions)

View changed files

📝 packages/vinext/src/server/middleware-codegen.ts (+12 -10)
📝 packages/vinext/src/server/middleware.ts (+31 -7)
📝 tests/__snapshots__/entry-templates.test.ts.snap (+24 -20)

📄 Description

Summary

  • Middleware matcher patterns (from config.matcher) are static but matchPattern() was re-running the full tokeniser + isSafeRegex scan + new RegExp() on every request for every matcher entry
  • This is the same problem that config-matchers.ts solved with _compiledPatternCache (which eliminated ~2.4s of CPU self-time in profiling) — the middleware path was never given the same fix
  • Add a Map<string, RegExp | null> cache to both the runtime matchPattern() and the codegen matchMiddlewarePattern() so each pattern is compiled exactly once per process lifetime

Test plan

  • All 57 existing middleware matcher tests pass (exact match, params, wildcards, regex, ReDoS rejection, bypass prevention, double-encoding)
  • All 3 codegen parity tests pass (eval'd generated code produces identical results)
  • Entry template snapshots updated
  • Typecheck clean
  • Lint clean

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/cloudflare/vinext/pull/395 **Author:** [@NathanDrake2406](https://github.com/NathanDrake2406) **Created:** 3/10/2026 **Status:** ✅ Merged **Merged:** 3/10/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `fix/middleware-regex-cache` --- ### 📝 Commits (1) - [`fb52807`](https://github.com/cloudflare/vinext/commit/fb52807c3e7a7868544417ab20185eff42866c06) perf: cache compiled regexes in middleware matcher ### 📊 Changes **3 files changed** (+67 additions, -37 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/server/middleware-codegen.ts` (+12 -10) 📝 `packages/vinext/src/server/middleware.ts` (+31 -7) 📝 `tests/__snapshots__/entry-templates.test.ts.snap` (+24 -20) </details> ### 📄 Description ## Summary - Middleware matcher patterns (from `config.matcher`) are static but `matchPattern()` was re-running the full tokeniser + `isSafeRegex` scan + `new RegExp()` on **every request** for every matcher entry - This is the same problem that `config-matchers.ts` solved with `_compiledPatternCache` (which eliminated ~2.4s of CPU self-time in profiling) — the middleware path was never given the same fix - Add a `Map<string, RegExp | null>` cache to both the runtime `matchPattern()` and the codegen `matchMiddlewarePattern()` so each pattern is compiled exactly once per process lifetime ## Test plan - [x] All 57 existing middleware matcher tests pass (exact match, params, wildcards, regex, ReDoS rejection, bypass prevention, double-encoding) - [x] All 3 codegen parity tests pass (eval'd generated code produces identical results) - [x] Entry template snapshots updated - [x] Typecheck clean - [x] Lint clean --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 13:08:38 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#539
No description provided.