]> Cypherpunks.ru repositories - gostls13.git/commitdiff
os: avoid allocating a string for ReadDir skipped entries on Windows
authorqmuntal <quimmuntal@gmail.com>
Thu, 17 Aug 2023 08:26:36 +0000 (10:26 +0200)
committerQuim Muntal <quimmuntal@gmail.com>
Mon, 21 Aug 2023 08:33:51 +0000 (08:33 +0000)
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 <dmitshur@google.com>
Run-TryBot: Quim Muntal <quimmuntal@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/os/dir_windows.go

index 84dee5c7b3c8bdc5cb147aa733872d26d1ffbeb7..4485dffdb184e164db696a34f57b93918c92ac67 100644 (file)
@@ -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 {