Skip to content

Commit 0a8b115

Browse files
committed
perf(path,io): CleanPath OS-native fast path + WriteString zero-copy fallback
Two more lifts driven by go-mlx hot-path usage tally (PathJoin/JoinPath/ Path 800+ calls, WriteString 318 calls): * CleanPath: when ds == PathSeparator (the >99% case), delegate to stdlib filepath.Clean — byte-level scan vs the Split/Join pipeline. Non-OS-native separators fall through to the existing impl. Bench impact: CleanPath_NoChange 143.5 ns / 6 allocs → 24.1 ns / 0 allocs 6.0x CleanPath_DotDot 179.1 ns / 6 allocs → 75.2 ns / 2 allocs 2.4x * WriteString: when the writer exposes WriteString (strings.Builder, bytes.Buffer, *os.File), delegate. For writers without one, swap stdlib io.WriteString's []byte(s) copy for AsBytes(s) — safe under the io.Writer contract (implementations must not retain or mutate the slice past the call). Bench impact: WriteString_Short ~7 ns / 1 alloc → 4.97 ns / 0 allocs 1.4x WriteString_1KB ~30 ns → 18.65 ns / 1 alloc go-mlx hot-path audit found these were the two remaining wins; the inference per-token path itself runs through core primitives at near-zero alloc (Sprintf only fires on error branches that don't hit in steady state, Env is cached, error construction is one alloc).
1 parent ffb1abb commit 0a8b115

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

io.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,22 @@ func CopyN(dst Writer, src Reader, n int64) Result {
125125
// the number of bytes written (int).
126126
//
127127
// r := core.WriteString(stdout, "hello\n")
128+
//
129+
// Fast path: when the writer exposes a WriteString method we delegate
130+
// straight to it (strings.Builder, bytes.Buffer, *os.File on most
131+
// platforms). For writers without one, we use AsBytes to skip the
132+
// []byte(s) copy that stdlib io.WriteString does in its fallback —
133+
// safe because the io.Writer contract forbids retention or mutation
134+
// of the slice past the call.
128135
func WriteString(w Writer, s string) Result {
129-
n, err := io.WriteString(w, s)
136+
if sw, ok := w.(interface{ WriteString(string) (int, error) }); ok {
137+
n, err := sw.WriteString(s)
138+
if err != nil {
139+
return Result{err, false}
140+
}
141+
return Result{n, true}
142+
}
143+
n, err := w.Write(AsBytes(s))
130144
if err != nil {
131145
return Result{err, false}
132146
}

path.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,17 @@ func PathIsAbs(p string) bool {
155155
//
156156
// core.CleanPath("/tmp//file", "/") // "/tmp/file"
157157
// core.CleanPath("a/b/../c", "/") // "a/c"
158+
//
159+
// Fast path: when ds is the OS-native separator (the >99% case),
160+
// delegate to stdlib filepath.Clean — byte-level scan, 2 allocs vs
161+
// the Split/Join pipeline's 6.
158162
func CleanPath(p, ds string) string {
159163
if p == "" {
160164
return "."
161165
}
166+
if ds == string(PathSeparator) {
167+
return filepath.Clean(p)
168+
}
162169

163170
rooted := HasPrefix(p, ds)
164171
parts := Split(p, ds)

0 commit comments

Comments
 (0)