Skip to content

Commit fa15b0c

Browse files
committed
perf(tspath): void []byte allocation in ToFileNameLowerCase
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% ```
1 parent 1ee2ba9 commit fa15b0c

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

internal/tspath/path.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,9 @@ func ToFileNameLowerCase(fileName string) string {
583583

584584
ascii := true
585585
needsLower := false
586-
for _, c := range []byte(fileName) {
586+
fileNameLen := len(fileName)
587+
for i := range fileNameLen {
588+
c := fileName[i]
587589
if c >= 0x80 {
588590
ascii = false
589591
break
@@ -596,8 +598,9 @@ func ToFileNameLowerCase(fileName string) string {
596598
if !needsLower {
597599
return fileName
598600
}
599-
b := make([]byte, len(fileName))
600-
for i, c := range []byte(fileName) {
601+
b := make([]byte, fileNameLen)
602+
for i := range fileNameLen {
603+
c := fileName[i]
601604
if 'A' <= c && c <= 'Z' {
602605
c += 'a' - 'A' // +32
603606
}

0 commit comments

Comments
 (0)