[PR #390] [MERGED] perf: O(1) locale-static redirect index #534

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

📋 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: mainHead: perf/locale-redirect-index


📝 Commits (1)

  • 3536f0f perf: 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):

  • Indexed in a `Map<suffix, entry[]>` keyed by the static path suffix
  • At match time: one `indexOf('/')` to extract the optional locale segment, one map lookup, then validate the locale against a tiny anchored alternation regex

`linear` — all other rules, handled with the original `matchConfigPattern` scan

Hot path (no redirect matches — the common case)

Before After
88 regex `exec()` calls 2 map lookups (miss) + 25 `matchConfigPattern` calls

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:

  • locale present / locale omitted
  • no-match returns null (fast path)
  • locale not in alternation → null
  • multi-segment locales (`pt-br`, `zh-cn`)
  • ordering: linear-before-locale-static wins
  • ordering: locale-static-before-linear wins
  • `has`/`missing` conditions on locale-static rules
  • 63-rule no-match scenario

🔄 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/390 **Author:** [@james-elicx](https://github.com/james-elicx) **Created:** 3/9/2026 **Status:** ✅ Merged **Merged:** 3/9/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `perf/locale-redirect-index` --- ### 📝 Commits (1) - [`3536f0f`](https://github.com/cloudflare/vinext/commit/3536f0f9ac128707dd498c37c13aeb1375fda442) perf: O(1) locale-static redirect index eliminates per-request regex scan ### 📊 Changes **2 files changed** (+362 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/config/config-matchers.ts` (+215 -2) 📝 `tests/shims.test.ts` (+147 -0) </details> ### 📄 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): - Indexed in a \`Map<suffix, entry[]>\` keyed by the static path suffix - At match time: one \`indexOf('/')\` to extract the optional locale segment, one map lookup, then validate the locale against a tiny anchored alternation regex **\`linear\`** — all other rules, handled with the original \`matchConfigPattern\` scan ### Hot path (no redirect matches — the common case) | Before | After | |--------|-------| | 88 regex \`exec()\` calls | 2 map lookups (miss) + 25 \`matchConfigPattern\` calls | ### 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: - locale present / locale omitted - no-match returns null (fast path) - locale not in alternation → null - multi-segment locales (\`pt-br\`, \`zh-cn\`) - ordering: linear-before-locale-static wins - ordering: locale-static-before-linear wins - \`has\`/\`missing\` conditions on locale-static rules - 63-rule no-match scenario --- <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:37 +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#534
No description provided.