aboutsummaryrefslogtreecommitdiff
path: root/src/os/file_unix.go
diff options
context:
space:
mode:
authorPeter Collingbourne <pcc@google.com>2024-03-11 20:10:48 -0700
committerGopher Robot <gobot@golang.org>2024-03-16 00:34:52 +0000
commitb822f098c557ea3d9200fd2d8a2e2b4d641e83e4 (patch)
treeff5bd7207c526885f741ba5d99ee65e7a83765de /src/os/file_unix.go
parentbedda245740d766bfad36b2d91a22781575fe463 (diff)
downloadgo-b822f098c557ea3d9200fd2d8a2e2b4d641e83e4.tar.xz
os: don't try to make the directory FD non-blocking in os.ReadDir
This will fail because epoll_ctl() fails on directory FDs, so we end up issuing unnecessary syscalls. My test program that calls filepath.WalkDir on a large directory tree runs 1.23 ± 0.04 times faster than with the original implementation. Change-Id: Ie33d798c48057a7b2d0bacac80fcdde5b5a8bb1b Reviewed-on: https://go-review.googlesource.com/c/go/+/570877 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/os/file_unix.go')
-rw-r--r--src/os/file_unix.go30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/os/file_unix.go b/src/os/file_unix.go
index 6597186486..924ec25ed9 100644
--- a/src/os/file_unix.go
+++ b/src/os/file_unix.go
@@ -157,7 +157,7 @@ const (
kindNonBlock
// kindNoPoll means that we should not put the descriptor into
// non-blocking mode, because we know it is not a pipe or FIFO.
- // Used by openFdAt for directories.
+ // Used by openFdAt and openDirNolog for directories.
kindNoPoll
)
@@ -256,7 +256,7 @@ func epipecheck(file *File, e error) {
const DevNull = "/dev/null"
// openFileNolog is the Unix implementation of OpenFile.
-// Changes here should be reflected in openFdAt, if relevant.
+// Changes here should be reflected in openFdAt and openDirNolog, if relevant.
func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
setSticky := false
if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
@@ -303,6 +303,32 @@ func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
return f, nil
}
+func openDirNolog(name string) (*File, error) {
+ var r int
+ var s poll.SysFile
+ for {
+ var e error
+ r, s, e = open(name, O_RDONLY|syscall.O_CLOEXEC, 0)
+ if e == nil {
+ break
+ }
+
+ if e == syscall.EINTR {
+ continue
+ }
+
+ return nil, &PathError{Op: "open", Path: name, Err: e}
+ }
+
+ if !supportsCloseOnExec {
+ syscall.CloseOnExec(r)
+ }
+
+ f := newFile(r, name, kindNoPoll)
+ f.pfd.SysFile = s
+ return f, nil
+}
+
func (file *file) close() error {
if file == nil {
return syscall.EINVAL