[go] path/filepath: optimize isReservedName

0 views
Skip to first unread message

Gopher Robot (Gerrit)

unread,
Sep 23, 2022, 12:34:58 AM9/23/22
to Joseph Tsai, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Ian Lance Taylor, Damien Neil, golang-co...@googlegroups.com

Gopher Robot submitted this change.

View Change


Approvals: Joseph Tsai: Run TryBots; Automatically submit change Damien Neil: Looks good to me, approved Ian Lance Taylor: Looks good to me, approved Gopher Robot: TryBots succeeded
path/filepath: optimize isReservedName

A linear search through a list of 22 strings takes ~80ns.
A quick check for 3-4 byte strings reduces this check to 2ns
for a vast majority of inputs.
In the event of a name match, the new logic is either just
as fast (for "CON") or 10x faster (for "LPT9").

Change-Id: I412fa73beebd7c81dc95f9ed12c35ca1d5d6baf0
Reviewed-on: https://go-review.googlesource.com/c/go/+/433175
Reviewed-by: Damien Neil <dn...@google.com>
Run-TryBot: Joseph Tsai <joe...@digital-static.net>
TryBot-Result: Gopher Robot <go...@golang.org>
Auto-Submit: Joseph Tsai <joe...@digital-static.net>
Reviewed-by: Ian Lance Taylor <ia...@google.com>
---
M src/path/filepath/path_windows.go
1 file changed, 35 insertions(+), 14 deletions(-)

diff --git a/src/path/filepath/path_windows.go b/src/path/filepath/path_windows.go
index b4d8ac3..80998de 100644
--- a/src/path/filepath/path_windows.go
+++ b/src/path/filepath/path_windows.go
@@ -13,24 +13,24 @@
return c == '\\' || c == '/'
}

-// reservedNames lists reserved Windows names. Search for PRN in
-// https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
-// for details.
-var reservedNames = []string{
- "CON", "PRN", "AUX", "NUL",
- "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
- "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
-}
-
// isReservedName returns true, if path is Windows reserved name.
// See reservedNames for the full list.
func isReservedName(path string) bool {
- if len(path) == 0 {
- return false
+ toUpper := func(c byte) byte {
+ if 'a' <= c && c <= 'z' {
+ return c - ('a' - 'A')
+ }
+ return c
}
- for _, reserved := range reservedNames {
- if strings.EqualFold(path, reserved) {
- return true
+
+ // For details, search for PRN in
+ // https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file.
+ if 3 <= len(path) && len(path) <= 4 {
+ switch string([]byte{toUpper(path[0]), toUpper(path[1]), toUpper(path[2])}) {
+ case "CON", "PRN", "AUX", "NUL":
+ return len(path) == 3
+ case "COM", "LPT":
+ return len(path) == 4 && '1' <= path[3] && path[3] <= '9'
}
}
return false

To view, visit change 433175. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I412fa73beebd7c81dc95f9ed12c35ca1d5d6baf0
Gerrit-Change-Number: 433175
Gerrit-PatchSet: 4
Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-MessageType: merged
Reply all
Reply to author
Forward
0 new messages