| 1 |
package encoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/rand" |
| 5 |
"strconv" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
"unsafe" |
| 9 |
|
| 10 |
"github.com/goccy/go-json/internal/runtime" |
| 11 |
) |
| 12 |
|
| 13 |
func TestScanStringAVX2(t *testing.T) { |
| 14 |
if !runtime.HasAVX2 { |
| 15 |
t.Skip("AVX2 is not supported") |
| 16 |
} |
| 17 |
special := []byte{0x00, 0x1f, '"', '\\', '<', '>', '&', 0x7f, 0x80, 0xe3, 0xff, '\n', ' ', '!', '#', '=', '?', '[', ']'} |
| 18 |
for index := range stringEscapes { |
| 19 |
e := &stringEscapes[index] |
| 20 |
expected := func(s string) int { |
| 21 |
for i := 0; i < len(s); i++ { |
| 22 |
if e.table[s[i]] { |
| 23 |
return 1 |
| 24 |
} |
| 25 |
} |
| 26 |
return 0 |
| 27 |
} |
| 28 |
for length := 32; length <= 130; length++ { |
| 29 |
base := strings.Repeat("a", length) |
| 30 |
check := func(s string) { |
| 31 |
t.Helper() |
| 32 |
got := scanStringAVX2(unsafe.Pointer(unsafe.StringData(s)), len(s), &e.tables) |
| 33 |
if expected := expected(s); got != expected { |
| 34 |
t.Fatalf("escape %d, %q: expected %d but got %d", index, s, expected, got) |
| 35 |
} |
| 36 |
} |
| 37 |
check(base) |
| 38 |
for pos := 0; pos < length; pos++ { |
| 39 |
for _, c := range special { |
| 40 |
b := []byte(base) |
| 41 |
b[pos] = c |
| 42 |
check(string(b)) |
| 43 |
if pos+1 < length { |
| 44 |
b[pos+1] = '"' |
| 45 |
check(string(b)) |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// The scan must not read out of the string. |
| 54 |
func TestScanStringAVX2AtEndOfMemory(t *testing.T) { |
| 55 |
if !runtime.HasAVX2 { |
| 56 |
t.Skip("AVX2 is not supported") |
| 57 |
} |
| 58 |
for length := 32; length <= 100; length++ { |
| 59 |
mem := []byte(strings.Repeat("a", length)) |
| 60 |
for start := 0; start+32 <= length; start++ { |
| 61 |
s := string(mem[start:]) |
| 62 |
if got := scanStringAVX2(unsafe.Pointer(unsafe.StringData(s)), len(s), &stringEscapes[stringEscapeHTML|stringEscapeNormalize].tables); got != 0 { |
| 63 |
t.Fatalf("%q: got %d", s, got) |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// the scans alone: the one by words and the one by SIMD. |
| 70 |
func BenchmarkScanString(b *testing.B) { |
| 71 |
e := &stringEscapes[stringEscapeHTML|stringEscapeNormalize] |
| 72 |
for _, n := range []int{36, 100, 1000} { |
| 73 |
s := strings.Repeat("a", n) |
| 74 |
p := unsafe.Pointer(unsafe.StringData(s)) |
| 75 |
b.Run("Words/"+strconv.Itoa(n), func(b *testing.B) { |
| 76 |
for i := 0; i < b.N; i++ { |
| 77 |
if e.hasLooseEscape(p, n) { |
| 78 |
b.Fatal("found") |
| 79 |
} |
| 80 |
} |
| 81 |
}) |
| 82 |
if runtime.HasAVX2 { |
| 83 |
b.Run("AVX2/"+strconv.Itoa(n), func(b *testing.B) { |
| 84 |
for i := 0; i < b.N; i++ { |
| 85 |
if scanStringAVX2(p, n, &e.tables) != 0 { |
| 86 |
b.Fatal("found") |
| 87 |
} |
| 88 |
} |
| 89 |
}) |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestAppendEscapedSIMD(t *testing.T) { |
| 95 |
// The functions of the escapes append the same bytes with the loop of the escapes by SIMD as without it, for |
| 96 |
// strings of every length with bytes to escape by every option anywhere, including strings which are not |
| 97 |
// valid UTF-8 and strings mostly of characters which are not ASCII, across the blocks and the segments. |
| 98 |
if !runtime.HasAVX2 { |
| 99 |
t.Skip("AVX2 is not supported") |
| 100 |
} |
| 101 |
defer func() { hasEscapeLoop = true }() |
| 102 |
r := rand.New(rand.NewSource(1)) |
| 103 |
pieces := []string{"a", "Z", "0", " ", "\n", "\r", "\t", "\"", "\\", "\x00", "\x08", "\x0c", "\x1f", "\x7f", "<", ">", "&", |
| 104 |
"é", "あ", "語", "😀", "\U0010ffff", "\u2027", "\u2028", "\u2029", "\u202a", "\ufffd", "\xff", "\x80", "\xc3", "\xe3\x81", |
| 105 |
"\xf0\x9f", "\xc0\x80", "\xe0\x80\x80", "\xed\xa0\x80", "\xf0\x80\x80\x80", "\xf4\x90\x80\x80", "\xf5\x80\x80\x80"} |
| 106 |
check := func(s string) { |
| 107 |
t.Helper() |
| 108 |
for index := range stringEscapes { |
| 109 |
e := &stringEscapes[index] |
| 110 |
hasEscapeLoop = false |
| 111 |
want := e.appendEscaped([]byte("prefix"), s) |
| 112 |
hasEscapeLoop = true |
| 113 |
got := e.appendEscaped([]byte("prefix"), s) |
| 114 |
if string(got) != string(want) { |
| 115 |
t.Fatalf("%d %q:\n got %q\nwant %q", index, s, got, want) |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
for n := 0; n < 3000; n++ { |
| 120 |
var b strings.Builder |
| 121 |
for k := r.Intn(n%400 + 1); k > 0; k-- { |
| 122 |
if r.Intn(6) == 0 { |
| 123 |
b.WriteString(pieces[r.Intn(len(pieces))]) |
| 124 |
} else { |
| 125 |
b.WriteByte(byte('a' + r.Intn(26))) |
| 126 |
} |
| 127 |
} |
| 128 |
check(b.String()) |
| 129 |
} |
| 130 |
valid := []string{"é", "ß", "あ", "語", "😀", "\U0010ffff", "\u2027", "\u202a", "a", " "} |
| 131 |
for n := 0; n < 1000; n++ { |
| 132 |
var b strings.Builder |
| 133 |
for k := r.Intn(n%3000 + 1); k > 0; k-- { |
| 134 |
if r.Intn(200) == 0 { |
| 135 |
b.WriteString(pieces[r.Intn(len(pieces))]) |
| 136 |
} else { |
| 137 |
b.WriteString(valid[r.Intn(len(valid))]) |
| 138 |
} |
| 139 |
} |
| 140 |
check(b.String()) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
func TestAppendHTMLEscapedSIMD(t *testing.T) { |
| 145 |
// The output of a marshaler is escaped for HTML the same with the loop of the escapes by SIMD as without it, |
| 146 |
// with the characters to escape and the other characters which start as U+2028 and U+2029 do anywhere. |
| 147 |
if !runtime.HasAVX2 { |
| 148 |
t.Skip("AVX2 is not supported") |
| 149 |
} |
| 150 |
defer func() { hasEscapeLoop = true }() |
| 151 |
r := rand.New(rand.NewSource(1)) |
| 152 |
pieces := []string{"<", ">", "&", "\u2028", "\u2029", "\u2027", "—", "“", "\xe2", "\xe2\x80", "é", "\\n", "\\\""} |
| 153 |
for n := 0; n < 3000; n++ { |
| 154 |
var b strings.Builder |
| 155 |
b.WriteByte('"') |
| 156 |
for k := r.Intn(n%400 + 1); k > 0; k-- { |
| 157 |
if r.Intn(8) == 0 { |
| 158 |
b.WriteString(pieces[r.Intn(len(pieces))]) |
| 159 |
} else { |
| 160 |
b.WriteByte(byte('a' + r.Intn(26))) |
| 161 |
} |
| 162 |
} |
| 163 |
b.WriteByte('"') |
| 164 |
src := []byte(b.String()) |
| 165 |
hasEscapeLoop = false |
| 166 |
want := appendHTMLEscaped([]byte("prefix"), src) |
| 167 |
hasEscapeLoop = true |
| 168 |
got := appendHTMLEscaped([]byte("prefix"), src) |
| 169 |
if string(got) != string(want) { |
| 170 |
t.Fatalf("%q:\n got %q\nwant %q", src, got, want) |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
// TestAppendNormalizedStrings without the loop of the escapes by SIMD. |
| 176 |
func TestAppendNormalizedStringsWithoutEscapeLoop(t *testing.T) { |
| 177 |
defer func(loop bool) { hasEscapeLoop = loop }(hasEscapeLoop) |
| 178 |
hasEscapeLoop = false |
| 179 |
checkAppendNormalizedStrings(t) |
| 180 |
} |
| 181 |
|
| 182 |
// A character which goes on after a block of the loop of the escapes of UTF-8, valid or not, followed by a block |
| 183 |
// of ASCII, of characters which are not ASCII or with a byte to escape, is appended as it is without the loop, at |
| 184 |
// every position around the ends of the blocks: the loop validates it with the next block, or stops before it. |
| 185 |
func TestEscapeUTF8AVX2AcrossBlocks(t *testing.T) { |
| 186 |
if !runtime.HasAVX2 { |
| 187 |
t.Skip("AVX2 is not supported") |
| 188 |
} |
| 189 |
defer func(loop bool) { hasEscapeLoop = loop }(hasEscapeLoop) |
| 190 |
text := strings.Repeat("あいう", 40) |
| 191 |
prefix := func(n int) string { |
| 192 |
// n bytes of characters of three bytes, and ASCII after the last whole one. |
| 193 |
s := text[:n/3*3] |
| 194 |
return s + strings.Repeat("x", n-len(s)) |
| 195 |
} |
| 196 |
pieces := []string{"", "\xe3", "\xe3\x81", "\xe3\x81\x82", "\xf0\x9f", "\xf0\x9f\x98", "\xf0\x9f\x98\x80", "\xc3", "\xc3\xa9", |
| 197 |
"\xe2\x80\xa8", "\xe2\x80\xa9", "\xe2\x80", "\xed\xa0\x80", "\x80", "\xff"} |
| 198 |
tails := []string{strings.Repeat("a", 70), strings.Repeat("あ", 24), `"` + strings.Repeat("b", 69), "é" + strings.Repeat("c", 68), ""} |
| 199 |
for n := 0; n <= 100; n++ { |
| 200 |
for _, piece := range pieces { |
| 201 |
for _, tail := range tails { |
| 202 |
s := prefix(n) + piece + tail |
| 203 |
for index := range stringEscapes { |
| 204 |
e := &stringEscapes[index] |
| 205 |
hasEscapeLoop = false |
| 206 |
want := e.appendEscaped([]byte("prefix"), s) |
| 207 |
hasEscapeLoop = true |
| 208 |
got := e.appendEscaped([]byte("prefix"), s) |
| 209 |
if string(got) != string(want) { |
| 210 |
t.Fatalf("%d %q:\n got %q\nwant %q", index, s, got, want) |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |