From 50f2b7b76481f045649d12d2a26dd36b7cc248a3 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Thu, 17 Aug 2023 10:26:36 +0200 Subject: [PATCH] os: avoid allocating a string for ReadDir skipped entries on Windows Shave off a few allocations while reading a directory by checking if the entry name is "." or ".." before allocating a string for it. Change-Id: I05a87d7572bd4fc191db70aaa9e22a6102f68b4b Reviewed-on: https://go-review.googlesource.com/c/go/+/520415 Reviewed-by: Dmitri Shuralyov Run-TryBot: Quim Muntal Reviewed-by: Bryan Mills TryBot-Result: Gopher Robot --- src/os/dir_windows.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/os/dir_windows.go b/src/os/dir_windows.go index 84dee5c7b3..4485dffdb1 100644 --- a/src/os/dir_windows.go +++ b/src/os/dir_windows.go @@ -153,10 +153,12 @@ func (file *File) readdir(n int, mode readdirMode) (names []string, dirents []Di if islast { d.bufp = 0 } - name := syscall.UTF16ToString(nameslice) - if name == "." || name == ".." { // Useless names + if (len(nameslice) == 1 && nameslice[0] == '.') || + (len(nameslice) == 2 && nameslice[0] == '.' && nameslice[1] == '.') { + // Ignore "." and ".." and avoid allocating a string for them. continue } + name := syscall.UTF16ToString(nameslice) if mode == readdirName { names = append(names, name) } else { -- 2.48.1