mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime/bytes: fast Compare for byte arrays and strings.
Uses SSE instructions to process 16 bytes at a time. fixes #5354 R=bradfitz, google CC=golang-dev https://golang.org/cl/8853048
This commit is contained in:
parent
f1583bb956
commit
b3946dc119
9 changed files with 568 additions and 64 deletions
|
|
@ -11,32 +11,6 @@ import (
|
|||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Compare returns an integer comparing two byte slices lexicographically.
|
||||
// The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
|
||||
// A nil argument is equivalent to an empty slice.
|
||||
func Compare(a, b []byte) int {
|
||||
m := len(a)
|
||||
if m > len(b) {
|
||||
m = len(b)
|
||||
}
|
||||
for i, ac := range a[0:m] {
|
||||
bc := b[i]
|
||||
switch {
|
||||
case ac > bc:
|
||||
return 1
|
||||
case ac < bc:
|
||||
return -1
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case len(a) < len(b):
|
||||
return -1
|
||||
case len(a) > len(b):
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func equalPortable(a, b []byte) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue