mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #390] [MERGED] perf: O(1) locale-static redirect index #534
Labels
No labels
enhancement
enhancement
good first issue
help wanted
nextjs-tracking
nextjs-tracking
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/vinext#534
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/cloudflare/vinext/pull/390
Author: @james-elicx
Created: 3/9/2026
Status: ✅ Merged
Merged: 3/9/2026
Merged by: @james-elicx
Base:
main← Head:perf/locale-redirect-index📝 Commits (1)
3536f0fperf: O(1) locale-static redirect index eliminates per-request regex scan📊 Changes
2 files changed (+362 additions, -2 deletions)
View changed files
📝
packages/vinext/src/config/config-matchers.ts(+215 -2)📝
tests/shims.test.ts(+147 -0)📄 Description
Problem
Profiling showed `matchConfigPattern` at 2992ms self-time (50% of active CPU), all in `compiled.re.exec`.
63 of 88 redirect rules use a locale alternation prefix:
```
/:locale(en|es|fr|id|ja|ko|pt-br|pt|ro|ta|tr|uk|zh-cn|zh-tw|)?/security
/:locale(en|es|fr|id|ja|ko|pt-br|pt|ro|ta|tr|uk|zh-cn|zh-tw|)?/advisory-board
... 61 more
```
For every request that matches no redirect (the vast majority of page loads), `matchRedirect` ran `exec()` on all 88 compiled regexes. The regex compilation was already cached by a previous fix — what remained was the execution cost: each locale alternation regex requires backtracking through `(en|es|fr|...|)?` on no-match paths.
Fix
Build a per-redirects-array index (stored in a `WeakMap`, computed once on first call) that classifies each rule into two buckets:
`localeStatic` — rules matching `/:param(alt1|alt2|...)?/static-suffix` (no other dynamic segments):
`linear` — all other rules, handled with the original `matchConfigPattern` scan
Hot path (no redirect matches — the common case)
Correctness
Original ordering is preserved. Each locale-static entry stores its `originalIndex`. When a locale-static match is found at position N, any linear rules at indices < N are still checked via `matchConfigPattern` first (they are zero in practice for auto-generated locale configs, so this adds no overhead to the typical case).
Tests
Added `describe("matchRedirect locale-static index")` in `tests/shims.test.ts` covering:
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.