From 8a1e54d58a3a3dc0d2001cb9368d3b0f9efc12f6 Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Tue, 23 Nov 2021 19:55:58 +0100 Subject: [PATCH] lib/fs: Optimize Canonicalize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When pathSep is a constant, the compiler precomputes pathSep+pathSep and ".."+pathSep instead of emitting function calls to compute "//" and "../". Benchmark results in lib/osutil: name old time/op new time/op delta TraversesSymlink-8 8.86µs ± 3% 8.53µs ± 4% -3.79% (p=0.000 n=18+20) name old alloc/op new alloc/op delta TraversesSymlink-8 1.06kB ± 0% 1.06kB ± 0% ~ (all equal) name old allocs/op new allocs/op delta TraversesSymlink-8 15.0 ± 0% 15.0 ± 0% ~ (all equal) --- lib/fs/filesystem.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fs/filesystem.go b/lib/fs/filesystem.go index 7ab08600f..c19860dd4 100644 --- a/lib/fs/filesystem.go +++ b/lib/fs/filesystem.go @@ -253,7 +253,7 @@ func IsInternal(file string) bool { // - /foo/bar -> foo/bar // - / -> "." func Canonicalize(file string) (string, error) { - pathSep := string(PathSeparator) + const pathSep = string(PathSeparator) if strings.HasPrefix(file, pathSep+pathSep) { // The relative path may pretend to be an absolute path within