Skip to content

Commit d945152

Browse files
qmuntalcellularmitosis
authored andcommitted
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 <[email protected]> Run-TryBot: Quim Muntal <[email protected]> Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 82655b9 commit d945152

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/os/dir_windows.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,12 @@ func (file *File) readdir(n int, mode readdirMode) (names []string, dirents []Di
153153
if islast {
154154
d.bufp = 0
155155
}
156-
name := syscall.UTF16ToString(nameslice)
157-
if name == "." || name == ".." { // Useless names
156+
if (len(nameslice) == 1 && nameslice[0] == '.') ||
157+
(len(nameslice) == 2 && nameslice[0] == '.' && nameslice[1] == '.') {
158+
// Ignore "." and ".." and avoid allocating a string for them.
158159
continue
159160
}
161+
name := syscall.UTF16ToString(nameslice)
160162
if mode == readdirName {
161163
names = append(names, name)
162164
} else {

0 commit comments

Comments
 (0)