Skip to content

path: faster posix path.resolve() without arguments #58362

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmark/path/join-posix.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function main({ n, paths }) {

bench.start();
for (let i = 0; i < n; i++) {
if (i % 3 === 0) {
copy[1] = `${orig}${i}`;
if (i % 5 === 0) {
copy[1] = `${orig}/${i}`;
posix.join(...copy);
} else {
posix.join(...args);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/path/join-win32.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function main({ n, paths }) {

bench.start();
for (let i = 0; i < n; i++) {
if (i % 3 === 0) {
copy[1] = `${orig}${i}`;
if (i % 5 === 0) {
copy[1] = `${orig}\\${i}`;
win32.join(...copy);
} else {
win32.join(...args);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/path/normalize-posix.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const bench = common.createBenchmark(main, {
function main({ n, path }) {
bench.start();
for (let i = 0; i < n; i++) {
posix.normalize(i % 3 === 0 ? `${path}${i}` : path);
posix.normalize(i % 5 === 0 ? `${path}/${i}` : path);
}
bench.end(n);
}
2 changes: 1 addition & 1 deletion benchmark/path/normalize-win32.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const bench = common.createBenchmark(main, {
function main({ n, path }) {
bench.start();
for (let i = 0; i < n; i++) {
win32.normalize(i % 3 === 0 ? `${path}${i}` : path);
win32.normalize(i % 5 === 0 ? `${path}\\${i}` : path);
}
bench.end(n);
}
11 changes: 7 additions & 4 deletions benchmark/path/resolve-posix.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ const { posix } = require('path');

const bench = common.createBenchmark(main, {
paths: [
'empty',
'',
'.',
['', ''].join('|'),
['foo/bar', '/tmp/file/', '..', 'a/../subfile'].join('|'),
['a/b/c/', '../../..'].join('|'),
['/a/b/c/', 'abc'].join('|'),
],
n: [1e5],
});

function main({ n, paths }) {
const args = paths.split('|');
const args = paths === 'empty' ? [] : paths.split('|');
const copy = [...args];
const orig = copy[0];
const orig = copy[0] ?? '';

bench.start();
for (let i = 0; i < n; i++) {
if (i % 3 === 0) {
copy[0] = `${orig}${i}`;
if (i % 5 === 0) {
copy[0] = `${orig}/${i}`;
posix.resolve(...copy);
} else {
posix.resolve(...args);
Expand Down
10 changes: 6 additions & 4 deletions benchmark/path/resolve-win32.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const { win32 } = require('path');

const bench = common.createBenchmark(main, {
paths: [
'empty',
'',
'.',
['', ''].join('|'),
['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'].join('|'),
['c:/blah\\blah', 'd:/games', 'c:../a'].join('|'),
Expand All @@ -13,14 +15,14 @@ const bench = common.createBenchmark(main, {
});

function main({ n, paths }) {
const args = paths.split('|');
const args = paths === 'empty' ? [] : paths.split('|');
const copy = [...args];
const orig = copy[0];
const orig = copy[0] ?? '';

bench.start();
for (let i = 0; i < n; i++) {
if (i % 3 === 0) {
copy[0] = `${orig}${i}`;
if (i % 5 === 0) {
copy[0] = `${orig}\\${i}`;
win32.resolve(...copy);
} else {
win32.resolve(...args);
Expand Down
18 changes: 17 additions & 1 deletion lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
StringPrototypeCharCodeAt(res, res.length - 1) !== CHAR_DOT ||
StringPrototypeCharCodeAt(res, res.length - 2) !== CHAR_DOT) {
if (res.length > 2) {
const lastSlashIndex = StringPrototypeLastIndexOf(res, separator);
const lastSlashIndex = res.length - lastSegmentLength - 1;
if (lastSlashIndex === -1) {
res = '';
lastSegmentLength = 0;
Expand Down Expand Up @@ -163,6 +163,8 @@ function _format(sep, pathObject) {
return dir === pathObject.root ? `${dir}${base}` : `${dir}${sep}${base}`;
}

const forwardSlashRegExp = /\//g;

const win32 = {
/**
* path.resolve([from ...], to)
Expand All @@ -186,6 +188,14 @@ const win32 = {
}
} else if (resolvedDevice.length === 0) {
path = process.cwd();
// Fast path for current directory
if (args.length === 0 || ((args.length === 1 && (args[0] === '' || args[0] === '.')) &&
isPathSeparator(StringPrototypeCharCodeAt(path, 0)))) {
if (!isWindows) {
path = StringPrototypeReplace(path, forwardSlashRegExp, '\\');
}
return path;
}
} else {
// Windows has the concept of drive-specific current working
// directories. If we've resolved a drive letter but not yet an
Expand Down Expand Up @@ -1171,6 +1181,12 @@ const posix = {
* @returns {string}
*/
resolve(...args) {
if (args.length === 0 || (args.length === 1 && (args[0] === '' || args[0] === '.'))) {
const cwd = posixCwd();
if (StringPrototypeCharCodeAt(cwd, 0) === CHAR_FORWARD_SLASH) {
return cwd;
}
}
let resolvedPath = '';
let resolvedAbsolute = false;

Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-path-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const resolveTests = [
[['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
[['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
[['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'],
[[], process.cwd()],
[[''], process.cwd()],
[['.'], process.cwd()],
[['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'],
[['c:/', '//'], 'c:\\'],
Expand All @@ -42,6 +44,8 @@ const resolveTests = [
[[['/var/lib', '../', 'file/'], '/var/file'],
[['/var/lib', '/../', 'file/'], '/file'],
[['a/b/c/', '../../..'], posixyCwd],
[[], posixyCwd],
[[''], posixyCwd],
[['.'], posixyCwd],
[['/some/dir', '.', '/absolute/'], '/absolute'],
[['/foo/tmp.3/', '../tmp.3/cycles/root.js'], '/foo/tmp.3/cycles/root.js'],
Expand Down
Loading