Debian Codegraph @

internal/encoder/string_scan_amd64.go

raw
1 package encoder
2
3 import (
4 "unsafe"
5
6 "github.com/goccy/go-json/internal/runtime"
7 )
8
9 // The scan of a string for a byte to escape by AVX2, 32 bytes at a time.
10
11 //go:noescape
12 func scanStringAVX2(p unsafe.Pointer, n int, tables *nibbleTables) int
13
14 //go:noescape
15 func escapeStringAVX2(dst, src unsafe.Pointer, n int, tables *nibbleTables, seqs *[256]uint64) (consumed, written int)
16
17 //go:noescape
18 func escapeUTF8AVX2(dst, src unsafe.Pointer, n int, tables *nibbleTables, seqs *[256]uint64, utf8 *utf8Tables) (consumed, written int)
19
20 // hasEscapeLoop is whether appendEscapedSIMD escapes: whether the CPU has AVX2.
21 var hasEscapeLoop = runtime.HasAVX2
22
23 // escapeSegmentLength is the number of the bytes of a string which appendEscapedSIMD escapes by one call of the
24 // loop, which has room for 6 bytes of every byte: it bounds the growth of the buffer for a long string.
25 const escapeSegmentLength = 1024
26
27 // appendEscapedSIMD appends the string escaped, 32 bytes at a time, as the appendString functions do by the
28 // tables of their options, and returns the number of the bytes of the string it appended. It stops at a byte
29 // which the caller escapes or when fewer than 32 bytes remain. If UTF-8 is normalized, tables are the ones of
30 // the bytes of ASCII to escape, and the bytes which are not ASCII are validated: it stops at the start of a
31 // character in a block of 32 bytes with invalid UTF-8, U+2028 or U+2029. The CPU has AVX2.
32 func appendEscapedSIMD(buf []byte, s string, tables *nibbleTables, normalizeUTF8 bool) ([]byte, int) {
33 consumed := 0
34 for len(s)-consumed >= 32 {
35 n := min(len(s)-consumed, escapeSegmentLength)
36 if need := 6*n + 32; cap(buf)-len(buf) < need {
37 buf = growForString(buf, need)
38 }
39 dst := unsafe.Add(unsafe.Pointer(unsafe.SliceData(buf)), len(buf))
40 src := unsafe.Add(unsafe.Pointer(unsafe.StringData(s)), consumed)
41 var c, w int
42 if normalizeUTF8 {
43 c, w = escapeUTF8AVX2(dst, src, n, tables, &escapeSequences, &utf8ValidationTables)
44 } else {
45 c, w = escapeStringAVX2(dst, src, n, tables, &escapeSequences)
46 }
47 buf = buf[:len(buf)+w]
48 consumed += c
49 if c+32 <= n {
50 // stopped at a byte which the caller escapes
51 break
52 }
53 }
54 return buf, consumed
55 }
56
57 // minSIMDScanLength is the length from which a string is scanned by SIMD: the setup of the registers costs
58 // about as much as a few words of the scalar scan.
59 const minSIMDScanLength = 32
60
61 // maxOnePassLength is the length of the longest string which AppendString looks at and copies in one pass:
62 // a longer one is looked at by SIMD, which then costs less, and copied after.
63 const maxOnePassLength = minSIMDScanLength - 1
64
65 // scanBytesSIMD is whether a byte of the n bytes at src is in the tables, by SIMD. The second result is false
66 // if the CPU doesn't support it or n is small, and the bytes are not looked at. It is inlined, so that
67 // AppendString calls the scan itself.
68 func scanBytesSIMD(src unsafe.Pointer, n int, tables *nibbleTables) (bool, bool) {
69 if !runtime.HasAVX2 || n < minSIMDScanLength {
70 return false, false
71 }
72 return scanStringAVX2(src, n, tables) != 0, true
73 }