@@ -35,32 +35,41 @@ func EqualFoldString(a, b string) bool {
3535 n := uintptr (len (a ))
3636 p := * (* unsafe .Pointer )(unsafe .Pointer (& a ))
3737 q := * (* unsafe .Pointer )(unsafe .Pointer (& b ))
38- // If there is more than 32 bytes to copy, use the AVX optimized version,
39- // otherwise the overhead of the function call tends to be greater than
40- // looping 2 or 3 times over 8 bytes.
41- if n >= 32 && asm .equalFoldAVX2 != nil {
42- if asm .equalFoldAVX2 ((* byte )(p ), (* byte )(q ), n ) == 0 {
43- return false
38+ // Pre-check to avoid the other tests that would all evaluate to false.
39+ // For very small strings, this helps reduce the processing overhead.
40+ if n >= 8 {
41+ // If there is more than 32 bytes to copy, use the AVX optimized version,
42+ // otherwise the overhead of the function call tends to be greater than
43+ // looping 2 or 3 times over 8 bytes.
44+ if n > 32 && asm .equalFoldAVX2 != nil {
45+ if asm .equalFoldAVX2 ((* byte )(p ), (* byte )(q ), n ) == 0 {
46+ return false
47+ }
48+ k := (n / 16 ) * 16
49+ p = unsafe .Pointer (uintptr (p ) + k )
50+ q = unsafe .Pointer (uintptr (q ) + k )
51+ n -= k
4452 }
45- k := (n / 16 ) * 16
46- p = unsafe .Pointer (uintptr (p ) + k )
47- q = unsafe .Pointer (uintptr (q ) + k )
48- n -= k
49- }
5053
51- for n >= 8 {
52- const mask = 0xDFDFDFDFDFDFDFDF
54+ for n > 8 {
55+ const mask = 0xDFDFDFDFDFDFDFDF
5356
54- if (* (* uint64 )(p ) & mask ) != (* (* uint64 )(q ) & mask ) {
55- return false
57+ if (* (* uint64 )(p ) & mask ) != (* (* uint64 )(q ) & mask ) {
58+ return false
59+ }
60+
61+ p = unsafe .Pointer (uintptr (p ) + 8 )
62+ q = unsafe .Pointer (uintptr (q ) + 8 )
63+ n -= 8
5664 }
5765
58- p = unsafe .Pointer (uintptr (p ) + 8 )
59- q = unsafe .Pointer (uintptr (q ) + 8 )
60- n -= 8
66+ if n == 8 {
67+ const mask = 0xDFDFDFDFDFDFDFDF
68+ return (* (* uint64 )(p ) & mask ) == (* (* uint64 )(q ) & mask )
69+ }
6170 }
6271
63- if n >= 4 {
72+ if n > 4 {
6473 const mask = 0xDFDFDFDF
6574
6675 if (* (* uint32 )(p ) & mask ) != (* (* uint32 )(q ) & mask ) {
@@ -73,6 +82,8 @@ func EqualFoldString(a, b string) bool {
7382 }
7483
7584 switch n {
85+ case 4 :
86+ return (* (* uint32 )(p ) & 0xDFDFDFDF ) == (* (* uint32 )(q ) & 0xDFDFDFDF )
7687 case 3 :
7788 x := uint32 (* (* uint16 )(p )) | uint32 (* (* uint8 )(unsafe .Pointer (uintptr (p ) + 2 )))<< 16
7889 y := uint32 (* (* uint16 )(q )) | uint32 (* (* uint8 )(unsafe .Pointer (uintptr (q ) + 2 )))<< 16
0 commit comments