Repository navigation
Expand file tree
/
Copy pathraw_byte_bench_fixture_test.go
More file actions
93 lines (83 loc) · 3.32 KB
/
Copy pathraw_byte_bench_fixture_test.go
File metadata and controls
93 lines (83 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package casei
import "strings"
// These benchmark fixture sizes are independent of raw-plan admission. They
// make the construction and candidate-density shapes available before the
// transition optimization so the same public benchmark can measure both arms.
const (
rawByteBenchmarkCorpusBytes = 2 << 20
rawBytePublicationCorpusBytes = 5 << 20
rawByteFreshSampleBytes = 4 << 10
)
var rawByteCyrillicPatterns = []string{
"Шерлок Холмс",
"Джон Уотсон",
"Ирен Адлер",
"инспектор Лестрейд",
"профессор Мориарти",
}
// rawByteFalseCandidates emits ordinary Cyrillic root units followed by an
// ASCII mismatch. Each planted start reaches exactly two shared-plan unit
// transitions: the root and the reset byte.
func rawByteFalseCandidates(period, groups int) string {
if period < len("Дx") {
panic("raw byte candidate period is too short")
}
bytes := []byte(strings.Repeat("x", period*groups))
for at := 0; at+len("Дx") <= len(bytes); at += period {
copy(bytes[at:], "Дx")
}
return string(bytes)
}
// rawByteFalseCandidatesAtLeast rounds groups up so fixtures cross a requested
// byte boundary even when the candidate period does not divide it.
func rawByteFalseCandidatesAtLeast(period, bytes int) string {
return rawByteFalseCandidates(period, (bytes+period-1)/period)
}
// rawByteLateMatchCandidatesAtLeast keeps the same repeated false-root shape
// through the final sample window, then replaces one late slot with the first
// literal. The returned offset is the ordinary byte offset of that occurrence.
func rawByteLateMatchCandidatesAtLeast(period, bytes int) (string, int) {
pattern := rawByteCyrillicPatterns[0]
if period < len(pattern) {
panic("raw byte late-match period is too short")
}
out := []byte(rawByteFalseCandidatesAtLeast(period, bytes))
start := len(out) - period
copy(out[start:], pattern)
return string(out), start
}
// rawByteEarlyMatchCandidatesAtLeast plants one complete literal at a fixed
// early offset while retaining a long dense suffix. It guards the public Find
// call that can stop before later admission sample windows.
func rawByteEarlyMatchCandidatesAtLeast(period, bytes, start int) (string, int) {
pattern := rawByteCyrillicPatterns[0]
if period < len(pattern) || start < 0 || start%period != 0 {
panic("invalid raw byte early-match placement")
}
out := []byte(rawByteFalseCandidatesAtLeast(period, bytes))
if start+len(pattern) > len(out) {
panic("raw byte early match is outside its corpus")
}
copy(out[start:], pattern)
return string(out), start
}
// rawByteNearMissCandidates plants a whole literal prefix with a final Cyrillic
// mismatch. It exercises candidate confirmation without returning a match.
func rawByteNearMissCandidates(period, groups int) string {
const nearMiss = "Шерлок Холми"
if period < len(nearMiss) {
panic("raw byte near-miss period is too short")
}
bytes := []byte(strings.Repeat("x", period*groups))
for at := 0; at+len(nearMiss) <= len(bytes); at += period {
copy(bytes[at:], nearMiss)
}
return string(bytes)
}
func rawByteNearMissCandidatesAtLeast(period, bytes int) string {
return rawByteNearMissCandidates(period, (bytes+period-1)/period)
}
func rawByteLongPrefixPatterns() []string {
prefix := strings.Repeat("Д", 100)
return []string{prefix + "a", prefix + "b"}
}