Skip to content

Conversation

camc314
Copy link
Contributor

@camc314 camc314 commented Aug 13, 2025

Replace for _, c := range []byte(fileName) with direct byte indexing to iterate over the string without allocating a new []byte slice. This removes an unnecessary allocation in both the ASCII fast path check and the lowercasing loop, improving performance and reducing GC pressure.

                                                     │ bench-to-file-name-lower-case-main.txt │ bench-to-file-name-lower-case-cbdcb60a5.txt │
                                                     │                 sec/op                 │        sec/op          vs base              │
ToFileNameLowerCase//path/to/file.ext-12                                         8.422n ± ∞ ¹            8.508n ± ∞ ¹       ~ (p=0.151 n=5)
ToFileNameLowerCase//PATH/TO/FILE.EXT-12                                         38.27n ± ∞ ¹            38.27n ± ∞ ¹       ~ (p=0.730 n=5)
ToFileNameLowerCase//path/to/FILE.EXT-12                                         41.08n ± ∞ ¹            41.03n ± ∞ ¹       ~ (p=0.421 n=5)
ToFileNameLowerCase//user/UserName/proje...etc-12                                58.59n ± ∞ ¹            58.32n ± ∞ ¹       ~ (p=0.841 n=5)
ToFileNameLowerCase//user/UserName/proje...etc#01-12                             161.1n ± ∞ ¹            161.5n ± ∞ ¹       ~ (p=0.635 n=5)
ToFileNameLowerCase//user/UserName/proje...etc#02-12                             155.1n ± ∞ ¹            154.1n ± ∞ ¹       ~ (p=0.595 n=5)
ToFileNameLowerCase//user/UserName/proje...etc#03-12                             139.5n ± ∞ ¹            136.7n ± ∞ ¹  -2.01% (p=0.048 n=5)
ToFileNameLowerCase/FoO/FoO/FoO/FoO/FoO/...etc-12                                550.8n ± ∞ ¹            548.7n ± ∞ ¹       ~ (p=0.841 n=5)
geomean                                                                          78.82n                  78.58n        -0.30%

Replace `for _, c := range []byte(fileName)` with direct byte indexing
to iterate over the string without allocating a new []byte slice.
This removes an unnecessary allocation in both the ASCII fast path
check and the lowercasing loop, improving performance and reducing
GC pressure.

```
                                                     │ bench-to-file-name-lower-case-main.txt │ bench-to-file-name-lower-case-cbdcb60a5.txt │
                                                     │                 sec/op                 │        sec/op          vs base              │
ToFileNameLowerCase//path/to/file.ext-12                                         8.422n ± ∞ ¹            8.508n ± ∞ ¹       ~ (p=0.151 n=5)
ToFileNameLowerCase//PATH/TO/FILE.EXT-12                                         38.27n ± ∞ ¹            38.27n ± ∞ ¹       ~ (p=0.730 n=5)
ToFileNameLowerCase//path/to/FILE.EXT-12                                         41.08n ± ∞ ¹            41.03n ± ∞ ¹       ~ (p=0.421 n=5)
ToFileNameLowerCase//user/UserName/proje...etc-12                                58.59n ± ∞ ¹            58.32n ± ∞ ¹       ~ (p=0.841 n=5)
ToFileNameLowerCase//user/UserName/proje...etc#01-12                             161.1n ± ∞ ¹            161.5n ± ∞ ¹       ~ (p=0.635 n=5)
ToFileNameLowerCase//user/UserName/proje...etc#02-12                             155.1n ± ∞ ¹            154.1n ± ∞ ¹       ~ (p=0.595 n=5)
ToFileNameLowerCase//user/UserName/proje...etc#03-12                             139.5n ± ∞ ¹            136.7n ± ∞ ¹  -2.01% (p=0.048 n=5)
ToFileNameLowerCase/FoO/FoO/FoO/FoO/FoO/...etc-12                                550.8n ± ∞ ¹            548.7n ± ∞ ¹       ~ (p=0.841 n=5)
geomean                                                                          78.82n                  78.58n        -0.30%
```
@camc314
Copy link
Contributor Author

camc314 commented Aug 13, 2025

@jakebailey up to you if this is merged, I thought this had a perf benifit, but i explored the compiled code in godbolt, and it appears exactly the same machine code is generated .

Regardless, i think this is slightly more explicit behaviour.

@camc314 camc314 marked this pull request as ready for review August 13, 2025 23:32
@jakebailey
Copy link
Member

Yeah, this kind of conversion I am not surprised does not affect the compile. I agree it's clearer.

@jakebailey jakebailey enabled auto-merge August 13, 2025 23:34
@jakebailey jakebailey added this pull request to the merge queue Aug 14, 2025
Merged via the queue into microsoft:main with commit eb88029 Aug 14, 2025
22 checks passed
andrewbranch pushed a commit to andrewbranch/typescript-go that referenced this pull request Aug 18, 2025
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