-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Write path normalization without array allocations #60812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a880da
Write path normalization without array allocations or regexes
andrewbranch 9ab5fbd
Restore regex fast path
andrewbranch 44f665b
Fix tests and improve based on benchmarks
andrewbranch 4add8b8
Daniel’s optimization
andrewbranch e20e5c7
Move normalizeSlashes
andrewbranch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix tests and improve based on benchmarks
- Loading branch information
commit 44f665bdbf10f4d6ae39e30097550a0c738f2db0
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -630,50 +630,46 @@ export function getNormalizedAbsolutePath(path: string, currentDirectory: string | |
| path = combinePaths(currentDirectory, path); | ||
| rootLength = getRootLength(path); | ||
| } | ||
| const simple = simpleNormalizePath(path); | ||
| if (simple !== undefined) { | ||
| return simple; | ||
| else { | ||
| // combinePaths normalizes slashes, so not necessary in the other branch | ||
| path = normalizeSlashes(path); | ||
| } | ||
|
|
||
| const simpleNormalized = simpleNormalizePath(path); | ||
| if (simpleNormalized !== undefined) { | ||
| return simpleNormalized.length > rootLength ? removeTrailingDirectorySeparator(simpleNormalized) : simpleNormalized; | ||
| } | ||
|
|
||
| const length = path.length; | ||
| const root = path.substring(0, rootLength); | ||
| const normalizedRoot = root && normalizeSlashes(root); | ||
| // `normalized` is only initialized once `path` is determined to be non-normalized | ||
| let normalized = normalizedRoot === root ? undefined : normalizedRoot; | ||
| let normalized; | ||
| let index = rootLength; | ||
| let segmentStart = index; | ||
| let normalizedUpTo = index; | ||
| let seenNonDotDotSegment = rootLength !== 0; | ||
| while (index < path.length) { | ||
| while (index < length) { | ||
| // At beginning of segment | ||
| segmentStart = index; | ||
| let ch = path.charCodeAt(index); | ||
| while (isAnyDirectorySeparator(ch) && index + 1 < path.length) { | ||
| while (ch === CharacterCodes.slash && index + 1 < length) { | ||
| index++; | ||
| ch = path.charCodeAt(index); | ||
| } | ||
| if (index > segmentStart) { | ||
| if (normalized === undefined) { | ||
| // Seen superfluous separator | ||
| normalized = path.substring(0, segmentStart - 1); | ||
| } | ||
| // Seen superfluous separator | ||
| normalized ??= path.substring(0, segmentStart - 1); | ||
| segmentStart = index; | ||
| } | ||
| // Past any superfluous separators | ||
| const sepIndex = path.indexOf(directorySeparator, index + 1); | ||
| const altSepIndex = path.indexOf(altDirectorySeparator, index + 1); | ||
| let segmentEnd = sepIndex === -1 ? altSepIndex : altSepIndex === -1 ? sepIndex : Math.min(sepIndex, altSepIndex); | ||
| let segmentEnd = path.indexOf(directorySeparator, index + 1); | ||
| if (segmentEnd === -1) { | ||
| segmentEnd = path.length; | ||
| } | ||
| if (segmentEnd === altSepIndex && normalized === undefined) { | ||
| // Seen backslash | ||
| normalized = path.substring(0, segmentStart); | ||
| segmentEnd = length; | ||
| } | ||
| const segmentLength = segmentEnd - segmentStart; | ||
| if (segmentLength === 1 && path.charCodeAt(index) === CharacterCodes.dot) { | ||
| // "." segment (skip) | ||
| if (normalized === undefined) { | ||
| normalized = path.substring(0, normalizedUpTo); | ||
| } | ||
| normalized ??= path.substring(0, normalizedUpTo); | ||
| } | ||
| else if (segmentLength === 2 && path.charCodeAt(index) === CharacterCodes.dot && path.charCodeAt(index + 1) === CharacterCodes.dot) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nit: If the previous |
||
| // ".." segment | ||
|
|
@@ -699,7 +695,7 @@ export function getNormalizedAbsolutePath(path: string, currentDirectory: string | |
| normalized = normalized.substring(0, Math.max(rootLength, lastSlash)); | ||
| } | ||
| else { | ||
| normalized = normalizedRoot; | ||
| normalized = root; | ||
| } | ||
| if (normalized.length === rootLength) { | ||
| seenNonDotDotSegment = rootLength !== 0; | ||
|
|
@@ -719,7 +715,7 @@ export function getNormalizedAbsolutePath(path: string, currentDirectory: string | |
| } | ||
| index = segmentEnd + 1; | ||
| } | ||
| return normalized ?? (path.length > rootLength ? removeTrailingDirectorySeparator(path) : path); | ||
| return normalized ?? (length > rootLength ? removeTrailingDirectorySeparator(path) : path); | ||
| } | ||
|
|
||
| /** @internal */ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simpleNormalizePathalso callsnormalizeSlasheson the first line of the function, which is redundant. I'd suggest you remove the line fromsimpleNormalizePathand just ensure its other callers normalize slashes before calling.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, that was my intention, good catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The benchmarks I shared already reflect that; I just made a copy/paste error updating this PR.