]> Cypherpunks repositories - gostls13.git/commitdiff
strings: remove overengineered Compare implementation
authorRuss Cox <rsc@golang.org>
Sun, 18 Jan 2015 17:50:22 +0000 (12:50 -0500)
committerRuss Cox <rsc@golang.org>
Mon, 19 Jan 2015 02:19:17 +0000 (02:19 +0000)
The function is here ONLY for symmetry with package bytes.
This function should be used ONLY if it makes code clearer.
It is not here for performance. Remove any performance benefit.

If performance becomes an issue, the compiler should be fixed to
recognize the three-way compare (for all comparable types)
rather than encourage people to micro-optimize by using this function.

Change-Id: I71f4130bce853f7aef724c6044d15def7987b457
Reviewed-on: https://go-review.googlesource.com/3012
Reviewed-by: Rob Pike <r@golang.org>
src/runtime/asm_386.s
src/runtime/asm_amd64.s
src/runtime/asm_amd64p32.s
src/runtime/noasm.go
src/strings/compare.go [new file with mode: 0644]
src/strings/strings_decl.go

index 625bf7bf4e2434b22948e0a4ebffac26aa98e207..0a58faf19b23fe405c5661fb9e0f05c070849f74 100644 (file)
@@ -1431,9 +1431,6 @@ TEXT runtime·cmpstring(SB),NOSPLIT,$0-20
        MOVL    AX, ret+16(FP)
        RET
 
-TEXT strings·Compare(SB),NOSPLIT,$0
-        JMP    runtime·cmpstring(SB)
-
 TEXT bytes·Compare(SB),NOSPLIT,$0-28
        MOVL    s1+0(FP), SI
        MOVL    s1+4(FP), BX
index 2f9d520d448a363fa658bc2065990af123b2abf5..8547228ee37804e50ce02bf079661d5b2bce1e83 100644 (file)
@@ -1364,9 +1364,6 @@ TEXT runtime·cmpstring(SB),NOSPLIT,$0-40
        MOVQ    AX, ret+32(FP)
        RET
 
-TEXT strings·Compare(SB),NOSPLIT,$0
-        JMP    runtime·cmpstring(SB)
-
 TEXT bytes·Compare(SB),NOSPLIT,$0-56
        MOVQ    s1+0(FP), SI
        MOVQ    s1+8(FP), BX
index 807bb56f2a80d07036b63eb20eb15991d301740f..77355bb99870590caa1b5512e7cd0c7237b42341 100644 (file)
@@ -832,9 +832,6 @@ TEXT runtime·cmpstring(SB),NOSPLIT,$0-20
        MOVL    AX, ret+16(FP)
        RET
 
-TEXT strings·Compare(SB),NOSPLIT,$0
-        JMP    runtime·cmpstring(SB)
-
 TEXT bytes·Compare(SB),NOSPLIT,$0-28
        MOVL    s1+0(FP), SI
        MOVL    s1+4(FP), BX
index c6e63257cbd6842244f89d316080ec639a01daa9..7ffde379921ab044915b9a6b5c4305c37453873a 100644 (file)
@@ -10,11 +10,6 @@ package runtime
 
 import _ "unsafe" // for go:linkname
 
-//go:linkname strings_Compare strings.Compare
-func strings_Compare(s1, s2 string) int {
-       return cmpstring(s1, s2)
-}
-
 func cmpstring(s1, s2 string) int {
        l := len(s1)
        if len(s2) < l {
diff --git a/src/strings/compare.go b/src/strings/compare.go
new file mode 100644 (file)
index 0000000..b84ddde
--- /dev/null
@@ -0,0 +1,28 @@
+// Copyright 2015 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strings
+
+// Compare returns an integer comparing two strings lexicographically.
+// The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
+//
+// Compare is included only for symmetry with package bytes.
+// It is usually clearer and always faster to use the built-in
+// string comparison operators ==, <, >, and so on.
+func Compare(a, b string) int {
+       // NOTE(rsc): This function does NOT call the runtime cmpstring function,
+       // because we do not want to provide any performance justification for
+       // using strings.Compare. Basically no one should use strings.Compare.
+       // As the comment above says, it is here only for symmetry with package bytes.
+       // If performance is important, the compiler should be changed to recognize
+       // the pattern so that all code doing three-way comparisons, not just code
+       // using strings.Compare, can benefit.
+       if a == b {
+               return 0
+       }
+       if a < b {
+               return -1
+       }
+       return +1
+}
index 9dc2a9a6c67e8d9f3acc962f853e8313f4e2e873..810a696af2767f7f877e1ca57c71a2412adb3090 100644 (file)
@@ -6,10 +6,3 @@ package strings
 
 // IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
 func IndexByte(s string, c byte) int // ../runtime/asm_$GOARCH.s
-
-// Compare returns an integer comparing two strings lexicographically.
-// The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
-//
-// In most cases it is simpler to use the built-in comparison operators
-// ==, <, >, and so on.
-func Compare(a, b string) int // ../runtime/noasm.go or ../runtime/asm_*.s