Skip to content

Commit 545944b

Browse files
authored
perf(tspath): avoid string copy in ToFileNameLowerCase (#1575)
1 parent cc53c33 commit 545944b

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

internal/tspath/path.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"slices"
66
"strings"
77
"unicode"
8+
"unsafe"
89

910
"github.com/microsoft/typescript-go/internal/stringutil"
1011
)
@@ -613,7 +614,17 @@ func ToFileNameLowerCase(fileName string) string {
613614
}
614615
b[i] = c
615616
}
616-
return string(b)
617+
// SAFETY: We construct a string that aliases b’s backing array without copying.
618+
// (1) Lifetime: The address of b’s elements escapes via the returned string,
619+
// so escape analysis allocates b’s backing array on the heap. The string
620+
// header points to that heap allocation, ensuring it remains live for the
621+
// string’s lifetime.
622+
// (2) Initialization: We assign to every b[i] before creating the string.
623+
// (Note: Go zeroes all allocated memory, so “uninitialized” bytes cannot occur.)
624+
// (3) Immutability: We do not modify b after this point, so the string view
625+
// observes immutable data.
626+
// (4) Non-empty: On this path len(b) > 0, so &b[0] is a valid, non-nil pointer.
627+
return unsafe.String(&b[0], len(b))
617628
}
618629

619630
return strings.Map(func(r rune) rune {

0 commit comments

Comments
 (0)