]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: replace index and contains with bytealg calls
authorAustin Clements <austin@google.com>
Wed, 22 Jul 2020 15:21:36 +0000 (11:21 -0400)
committerAustin Clements <austin@google.com>
Mon, 17 Aug 2020 13:20:03 +0000 (13:20 +0000)
The runtime has its own implementation of string indexing. To reduce
code duplication and cognitive load, replace this with calls to the
internal/bytealg package. We can't do this on Plan 9 because it needs
string indexing in a note handler (which isn't allowed to use the
optimized bytealg version because it uses SSE), so we can't just
eliminate the index function, but this CL does down-scope it so make
it clear it's only for note handlers on Plan 9.

Change-Id: Ie1a142678262048515c481e8c26313b80c5875df
Reviewed-on: https://go-review.googlesource.com/c/go/+/244537
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/runtime/os_plan9.go
src/runtime/proc.go
src/runtime/runtime1.go
src/runtime/string.go
src/runtime/traceback.go

index 9e187d2220514cdbb0a0352b0e66403e94ba2940..128c30adebadb2aaa685947ac1ed1631bf07e476 100644 (file)
@@ -82,10 +82,10 @@ func sigpanic() {
        note := gostringnocopy((*byte)(unsafe.Pointer(g.m.notesig)))
        switch g.sig {
        case _SIGRFAULT, _SIGWFAULT:
-               i := index(note, "addr=")
+               i := indexNoFloat(note, "addr=")
                if i >= 0 {
                        i += 5
-               } else if i = index(note, "va="); i >= 0 {
+               } else if i = indexNoFloat(note, "va="); i >= 0 {
                        i += 3
                } else {
                        panicmem()
@@ -111,6 +111,20 @@ func sigpanic() {
        }
 }
 
+// indexNoFloat is bytealg.IndexString but safe to use in a note
+// handler.
+func indexNoFloat(s, t string) int {
+       if len(t) == 0 {
+               return 0
+       }
+       for i := 0; i < len(s); i++ {
+               if s[i] == t[0] && hasPrefix(s[i:], t) {
+                       return i
+               }
+       }
+       return -1
+}
+
 func atolwhex(p string) int64 {
        for hasPrefix(p, " ") || hasPrefix(p, "\t") {
                p = p[1:]
index 035822216d2bb860e544f2a0f83d31674718075b..ed7e2128aee8956eac3d64243bb98e2c90e3f4c1 100644 (file)
@@ -5,6 +5,7 @@
 package runtime
 
 import (
+       "internal/bytealg"
        "internal/cpu"
        "runtime/internal/atomic"
        "runtime/internal/sys"
@@ -5460,7 +5461,7 @@ func haveexperiment(name string) bool {
        x := sys.Goexperiment
        for x != "" {
                xname := ""
-               i := index(x, ",")
+               i := bytealg.IndexByteString(x, ',')
                if i < 0 {
                        xname, x = x, ""
                } else {
index c65a534ef6887556647121fb8eb5da19834f3324..7c893aa25cb12623f1704473476ac9b3b62ddf71 100644 (file)
@@ -5,6 +5,7 @@
 package runtime
 
 import (
+       "internal/bytealg"
        "runtime/internal/atomic"
        "runtime/internal/sys"
        "unsafe"
@@ -347,13 +348,13 @@ func parsedebugvars() {
 
        for p := gogetenv("GODEBUG"); p != ""; {
                field := ""
-               i := index(p, ",")
+               i := bytealg.IndexByteString(p, ',')
                if i < 0 {
                        field, p = p, ""
                } else {
                        field, p = p[:i], p[i+1:]
                }
-               i = index(field, "=")
+               i = bytealg.IndexByteString(field, '=')
                if i < 0 {
                        continue
                }
index 251044231e7d84bbef9be41d13ff51ad25467d3f..9a601f00946ca435707a64e47a0de71d4161f0e5 100644 (file)
@@ -335,22 +335,6 @@ func gostringn(p *byte, l int) string {
        return s
 }
 
-func index(s, t string) int {
-       if len(t) == 0 {
-               return 0
-       }
-       for i := 0; i < len(s); i++ {
-               if s[i] == t[0] && hasPrefix(s[i:], t) {
-                       return i
-               }
-       }
-       return -1
-}
-
-func contains(s, t string) bool {
-       return index(s, t) >= 0
-}
-
 func hasPrefix(s, prefix string) bool {
        return len(s) >= len(prefix) && s[:len(prefix)] == prefix
 }
index 944c8473d25992a2bdcbaf571fa248cd1ab28732..96e552524ed33c45371aa61eb3bb1579ef1d6c68 100644 (file)
@@ -5,6 +5,7 @@
 package runtime
 
 import (
+       "internal/bytealg"
        "runtime/internal/atomic"
        "runtime/internal/sys"
        "unsafe"
@@ -848,7 +849,7 @@ func showfuncinfo(f funcInfo, firstFrame bool, funcID, childID funcID) bool {
                return true
        }
 
-       return contains(name, ".") && (!hasPrefix(name, "runtime.") || isExportedRuntime(name))
+       return bytealg.IndexByteString(name, '.') >= 0 && (!hasPrefix(name, "runtime.") || isExportedRuntime(name))
 }
 
 // isExportedRuntime reports whether name is an exported runtime function.