aboutsummaryrefslogtreecommitdiff
path: root/src/syscall
diff options
context:
space:
mode:
authorKir Kolyshkin <kolyshkin@gmail.com>2021-12-01 19:48:58 -0800
committerEmmanuel Odeke <emmanuel@orijtech.com>2022-03-28 16:55:57 +0000
commit0d651041a9f48bbd1456317dfc784cdfa253e877 (patch)
treeecd2e945e2adf25d1956b87fe2f7ee8e5cb25f4f /src/syscall
parentd6a1ffd624bd0d6dbf3a15070e378749612b35c9 (diff)
downloadgo-0d651041a9f48bbd1456317dfc784cdfa253e877.tar.xz
syscall: optimize Byte{Ptr,Slice}FromString
Use bytealg.IndexByteString(str, 0) instead of looping through the string to check for a zero byte. A quick and dirty benchmark shows 10x performance improvement (on amd64 machine, using go 1.17.3). BytePtrFromString is used by many functions with string arguments. This change should make many functions in os package, such as those accepting a filename (os.Open, os.Stat, etc.), a tad faster. PS I am aware that syscall package is deprecated and frozen, but this change is mainly for the os package and the likes. The alternative would be for os to switch to x/sys, which is a much bigger change. Change-Id: I18fdd50f9fbfe0a23a4a71bc4bd0a5f5b0eaa475 Reviewed-on: https://go-review.googlesource.com/c/go/+/368457 Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/syscall')
-rw-r--r--src/syscall/syscall.go8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/syscall/syscall.go b/src/syscall/syscall.go
index 91173033ee..98e3005253 100644
--- a/src/syscall/syscall.go
+++ b/src/syscall/syscall.go
@@ -26,6 +26,8 @@
//
package syscall
+import "internal/bytealg"
+
//go:generate go run ./mksyscall_windows.go -systemdll -output zsyscall_windows.go syscall_windows.go security_windows.go
// StringByteSlice converts a string to a NUL-terminated []byte,
@@ -45,10 +47,8 @@ func StringByteSlice(s string) []byte {
// containing the text of s. If s contains a NUL byte at any
// location, it returns (nil, EINVAL).
func ByteSliceFromString(s string) ([]byte, error) {
- for i := 0; i < len(s); i++ {
- if s[i] == 0 {
- return nil, EINVAL
- }
+ if bytealg.IndexByteString(s, 0) != -1 {
+ return nil, EINVAL
}
a := make([]byte, len(s)+1)
copy(a, s)