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

Merged

Conversation

camc314
Copy link
Contributor

@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%
Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran with go test -fuzz=FuzzToFileNameLowerCase ./internal/tspath for a while and nothing broke.

Thanks for this; when I improved this the first time, it was way better than the original one, but I always meant to come back and improve it further. It's just never been a bottleneck for anything we see in tsc/LSP.

@jakebailey jakebailey added this pull request to the merge queue Aug 12, 2025
@jakebailey jakebailey removed this pull request from the merge queue due to a manual request Aug 12, 2025
Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I ran the wrong one. This PR is FuzzHasRelativePathSegment.

go test -fuzz=FuzzHasRelativePathSegment ./internal/tspath immediately fails with:

fuzz: elapsed: 0s, gathering baseline coverage: 0/9 completed
fuzz: elapsed: 0s, gathering baseline coverage: 9/9 completed, now fuzzing with 20 workers
fuzz: minimizing 37-byte failing input file
fuzz: elapsed: 0s, minimizing
--- FAIL: FuzzHasRelativePathSegment (0.11s)
    --- FAIL: FuzzHasRelativePathSegment (0.00s)
        path_test.go:616: assertion failed: false (bool) != true (bool)
    
    Failing input written to testdata/fuzz/FuzzHasRelativePathSegment/9cd9b70960b4a733
    To re-run:
    go test -run=FuzzHasRelativePathSegment/9cd9b70960b4a733
FAIL
exit status 1
FAIL    github.com/microsoft/typescript-go/internal/tspath      0.129s

Which tests the string /.

@camc314
Copy link
Contributor Author

camc314 commented Aug 12, 2025

Oops, I ran the wrong one. This PR is FuzzHasRelativePathSegment.

🫣 oops, thanks for testing, should be fixed now:

fuzz: elapsed: 2m36s, execs: 26438586 (168071/sec), new interesting: 67 (total: 137)

@jakebailey jakebailey enabled auto-merge August 12, 2025 17:56
@jakebailey jakebailey added this pull request to the merge queue Aug 12, 2025
Merged via the queue into microsoft:main with commit 8e8766a Aug 12, 2025
22 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