| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"unsafe" |
| 5 |
|
| 6 |
"github.com/goccy/go-json/internal/runtime" |
| 7 |
) |
| 8 |
|
| 9 |
// hasStringSIMD is whether indexStringSpecial scans: whether the CPU has AVX2. |
| 10 |
var hasStringSIMD = runtime.HasAVX2 |
| 11 |
|
| 12 |
//go:noescape |
| 13 |
func indexStringSpecialAVX2(p unsafe.Pointer, n int) (index int, high uint64) |
| 14 |
|
| 15 |
// minStringSIMDLength is the number of the bytes from which the rest of a string is scanned by SIMD, after its |
| 16 |
// first words: the call and the setup of the registers cost about as much as a few words of the scalar scan. |
| 17 |
const minStringSIMDLength = 32 |
| 18 |
|
| 19 |
// indexStringSpecial returns the index of the first byte of the n bytes at p which is a quote, a backslash or a |
| 20 |
// control character, or n if there is none, and high, which is msb if a byte before it is not ASCII and else 0, |
| 21 |
// as the scan by words accumulates it. The last result is false if the CPU has no SIMD scan or n is less than |
| 22 |
// minStringSIMDLength: nothing is scanned. |
| 23 |
func indexStringSpecial(p unsafe.Pointer, n int) (int, uint64, bool) { |
| 24 |
if !runtime.HasAVX2 || n < minStringSIMDLength { |
| 25 |
return 0, 0, false |
| 26 |
} |
| 27 |
index, high := indexStringSpecialAVX2(p, n) |
| 28 |
if high != 0 { |
| 29 |
// the scan by AVX2 has a bit for every byte which is not ASCII: the callers look at the top bits of bytes. |
| 30 |
high = msb |
| 31 |
} |
| 32 |
return index, high, true |
| 33 |
} |