path/filepath: avoid allocation in Clean of cleaned path
Alternative to https://golang.org/cl/6330044.
Fixes #3681.
R=golang-dev, r, hanwen, iant
CC=golang-dev
https://golang.org/cl/6335056
diff --git a/src/pkg/path/path_test.go b/src/pkg/path/path_test.go
index 77f0804..109005d 100644
--- a/src/pkg/path/path_test.go
+++ b/src/pkg/path/path_test.go
@@ -5,6 +5,7 @@
package path
import (
+ "runtime"
"testing"
)
@@ -67,6 +68,24 @@
if s := Clean(test.path); s != test.result {
t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
}
+ if s := Clean(test.result); s != test.result {
+ t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
+ }
+ }
+
+ var ms runtime.MemStats
+ runtime.ReadMemStats(&ms)
+ allocs := -ms.Mallocs
+ const rounds = 100
+ for i := 0; i < rounds; i++ {
+ for _, test := range cleantests {
+ Clean(test.result)
+ }
+ }
+ runtime.ReadMemStats(&ms)
+ allocs += ms.Mallocs
+ if allocs >= rounds {
+ t.Errorf("Clean cleaned paths: %d allocations per test round, want zero", allocs/rounds)
}
}