Skip to content

perf(file-system): avoid per-call regex work in planFileTreeReveal - #3957

Merged
kev1n77 merged 1 commit into
openBitFun:mainfrom
Raing5Days:openbitfun/file-tree-reveal-plan-cost
Nov 19, 2026
Merged

kev1n77 merged 1 commit into
openBitFun:mainfrom
Raing5Days:openbitfun/file-tree-reveal-plan-cost

Conversation

@Raing5Days

Copy link
Copy Markdown
Contributor

Summary

planFileTreeReveal paid for two things that do not depend on what the path means:

  • /\/+$/ ran on every call, even when the last character was not a slash. That pattern is anchored to the end, so the engine starts a match attempt at every position and the whole string is scanned — the dominant cost once the input is longer than a few characters.
  • Four regexes and three closures were rebuilt per call. They are module constants and plain locals now.

The slash strip now runs only when the last character actually is /, and the backslash pass only when the input contains one.

Behaviour is unchanged: the lowercased copies are comparison-only, so returned paths keep the caller's casing, and remote paths keep literal backslashes.

Validation

  • node scripts/run-vitest.mjs run src/tools/file-system/utils/fileTreeReveal.test.ts — 5 passed (Windows separators, remote literal backslashes, traversal rejection, workspace and filesystem root, compressed-row index)
  • pnpm run type-check:web — passed
  • Microbenchmark (201,600 calls per run, 5 baseline samples before the change, median of 3 runs after): 4.18 µs → 0.240 µs per call, worst call 0.263 µs, cycle p95 30.7 ms → 1.62 ms
  • pnpm run check:web — fails on main at the miniapp-chrome-theme generated-artifact check; the failing output is identical with and without this diff

The workload passes a 1.7 KB text as the path, so this measures the pathological part of the regex. Real short paths gain less; the number quoted above is this workload's, not a frame-time claim.

Generated with OpenBitFun

Every reveal compiled four regexes and three closures, and stripped trailing
slashes with /\/+$/ even when the last character was not a slash. On a long
path-like string that anchored pattern is tried from every start position, so
it scans the whole string and dominated the call.

Hoist the patterns, skip the backslash pass when the input has none, and enter
the trailing-slash regex only when the last character is actually a slash.

Behaviour is unchanged: the lowercased copies are comparison-only, so returned
paths keep the caller's casing, and remote paths keep literal backslashes.
@Raing5Days
Raing5Days marked this pull request as ready for review November 17, 2026 03:51
@kev1n77
kev1n77 merged commit 83872ee into openBitFun:main Nov 19, 2026
9 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.

2 participants