Skip to content

Commit

Permalink
improved windows Get() performance (x100) by not returning ChangeTime
Browse files Browse the repository at this point in the history
in order to obtain the ChangeTime is was necessary to open the file, however Get() should only inspect the contents of the passed os.FileInfo. Stat() on the other hand does still open the file and can still obtain ChangeTime.
  • Loading branch information
djherbis committed Jan 5, 2016
1 parent cea2bd6 commit 9d08c75
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions ctime_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func StatFile(file *os.File) (Timespec, error) {
return t, nil
}

func statEx(file string) (Timespec, error) {
func Stat(name string) (Timespec, error) {
if findProcErr != nil {
// fail fast, don't bother opening the file
return nil, findProcErr
}

f, err := os.Open(file)
f, err := os.Open(name)
if err != nil {
return nil, err
}
Expand Down
13 changes: 13 additions & 0 deletions stat_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build !windows

package times

import "os"

func Stat(name string) (Timespec, error) {
fi, err := os.Stat(name)
if err != nil {
return nil, err
}
return getTimespec(fi), nil
}
9 changes: 0 additions & 9 deletions stat.go → times.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ func Get(fi os.FileInfo) Timespec {
return getTimespec(fi)
}

// Stat returns the Timespec for the given filename
func Stat(name string) (Timespec, error) {
fi, err := os.Stat(name)
if err != nil {
return nil, err
}
return getTimespec(fi), nil
}

// Timespec provides access to file times.
// ChangeTime() panics unless HasChangeTime() is true and
// BirthTime() panics unless HasBirthTime() is true.
Expand Down
4 changes: 0 additions & 4 deletions times_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ type timespec struct {
}

func getTimespec(fi os.FileInfo) Timespec {
if ts, err := statEx(fi.Name()); err == nil {
return ts
}

var t timespec
stat := fi.Sys().(*syscall.Win32FileAttributeData)
t.atime.v = time.Unix(0, stat.LastAccessTime.Nanoseconds())
Expand Down

0 comments on commit 9d08c75

Please sign in to comment.