]> Cypherpunks repositories - gostls13.git/commitdiff
os: only call GetConsoleMode for char devices
authorqmuntal <quimmuntal@gmail.com>
Mon, 3 Mar 2025 11:28:01 +0000 (12:28 +0100)
committerQuim Muntal <quimmuntal@gmail.com>
Mon, 3 Mar 2025 19:33:48 +0000 (11:33 -0800)
There is no need to call GetConsoleMode if we know that the file
type is not FILE_TYPE_CHAR. This is a tiny performance optimization,
as I sometimes see this call in profiles.

Change-Id: I9e9237908585d0ec8360930a0406b26f52699b92
Reviewed-on: https://go-review.googlesource.com/c/go/+/654155
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
src/os/file_windows.go

index c209a9f003b39bdc0571c496fabfdb8640faca56..07984be5c40cb623c2aacb6b94c4b817fea9760b 100644 (file)
@@ -44,11 +44,13 @@ func (file *File) fd() uintptr {
 // Unlike NewFile, it does not check that h is syscall.InvalidHandle.
 func newFile(h syscall.Handle, name string, kind string) *File {
        if kind == "file" {
-               var m uint32
-               if syscall.GetConsoleMode(h, &m) == nil {
-                       kind = "console"
-               }
-               if t, err := syscall.GetFileType(h); err == nil && t == syscall.FILE_TYPE_PIPE {
+               t, err := syscall.GetFileType(h)
+               if err != nil || t == syscall.FILE_TYPE_CHAR {
+                       var m uint32
+                       if syscall.GetConsoleMode(h, &m) == nil {
+                               kind = "console"
+                       }
+               } else if t == syscall.FILE_TYPE_PIPE {
                        kind = "pipe"
                }
        }