Skip to content

Commit 169c79a

Browse files
committed
perf(path): PathBase + PathExt zero-alloc via lastIndex
Replace Split([]string) + last-element index with lastIndex + string-slice. PathExt inherits the win because it calls PathBase. PathBase 56.9 ns / 1 alloc → 33.1 ns / 0 allocs 1.7x PathExt 58.2 ns / 1 alloc → 34.5 ns / 0 allocs 1.7x
1 parent d9522dc commit 169c79a

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

path.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,13 @@ func PathBase(p string) string {
9595
if p == "" {
9696
return ds
9797
}
98-
parts := Split(p, ds)
99-
return parts[len(parts)-1]
98+
// LastIndex + string-slice instead of Split — zero alloc instead
99+
// of an N-element []string just to read the last entry.
100+
i := lastIndex(p, ds)
101+
if i < 0 {
102+
return p
103+
}
104+
return p[i+len(ds):]
100105
}
101106

102107
// PathDir returns all but the last element of a path.

0 commit comments

Comments
 (0)