os: only call GetConsoleMode for char devices

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>
This commit is contained in:
qmuntal 2025-03-03 12:28:01 +01:00 committed by Quim Muntal
parent 05354fc3b4
commit 14647b0ac8
1 changed files with 7 additions and 5 deletions

View 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"
}
}