Skip to content

perf(tspath): optimize hasRelativePathSegment #1569

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

camc314
Copy link

@camc314 camc314 commented Aug 12, 2025

This pull request optimises hasRelativePathSegment, delivering a ~56% perf improvement.

This function is Called 27_628_418 times via tsgolint when linting vscode repo.

                                                     │   old.txt   │               new.txt               │
                                                     │   sec/op    │   sec/op     vs base                │
HasRelativePathSegment/foo/bar/baz-12                  29.10n ± 2%   14.79n ± 1%  -49.16% (p=0.000 n=10)
HasRelativePathSegment/./some/path-12                  2.065n ± 0%   2.039n ± 2%   -1.26% (p=0.022 n=10)
HasRelativePathSegment//foo/./bar/../../.-12           7.030n ± 0%   2.025n ± 0%  -71.20% (p=0.000 n=10)
HasRelativePathSegment/foo/foo/foo/foo/foo/...etc-12   9.076n ± 1%   2.308n ± 1%  -74.56% (p=0.000 n=10)
geomean                                                7.869n        3.446n       -56.21%

this function has tests here to ensure we don't regress vs the old version:

func FuzzHasRelativePathSegment(f *testing.F) {
for _, tt := range hasRelativePathSegmentTests {
f.Add(tt.p)
}
f.Fuzz(func(t *testing.T, p string) {
assert.Equal(t, oldHasRelativePathSegment(p), hasRelativePathSegment(p))
})
}

                                                     │   old.txt   │               new.txt               │
                                                     │   sec/op    │   sec/op     vs base                │
HasRelativePathSegment/foo/bar/baz-12                  29.10n ± 2%   14.79n ± 1%  -49.16% (p=0.000 n=10)
HasRelativePathSegment/./some/path-12                  2.065n ± 0%   2.039n ± 2%   -1.26% (p=0.022 n=10)
HasRelativePathSegment//foo/./bar/../../.-12           7.030n ± 0%   2.025n ± 0%  -71.20% (p=0.000 n=10)
HasRelativePathSegment/foo/foo/foo/foo/foo/...etc-12   9.076n ± 1%   2.308n ± 1%  -74.56% (p=0.000 n=10)
geomean                                                7.869n        3.446n       -56.21%
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.

1 participant