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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use Regex;
use ;
use ResidualShard;
// What: `pub fn build_residual_shards(positions, regex_specs) -> Result<Vec<ResidualShard>, String>`
// is the public entry point. Delegates to `greedy_combine`,
// which returns `Vec<ResidualShard>` infallibly: the recursion
// bottoms at `ResidualShard::Single`, and Single shards do not
// require a fresh `Regex::new` (they reuse the already-compiled
// regex from Phase 2a). The `Result` return type is preserved
// for API compatibility with callers that already match on
// `load_ruleset`'s overall Result.
// Why: Wrapping the infallible inner work in an outer `Result` keeps
// `rules.rs::load_ruleset` unchanged.
// TS map: `function buildResidualShards(positions, regexSpecs): ResidualShard[]`.
//
// In TS you'd write (pseudocode):
// ```ts
// function buildResidualShards(positions: number[], regexSpecs: [number, string][]): ResidualShard[] {
// if (positions.length === 0) return [];
// return greedyCombine(positions, regexSpecs);
// }
// ```
// What: `GREEDY_COMBINE_THRESHOLD` is the residual-count above which
// `build_residual_shards` runs the divide-and-conquer
// greedy_combine. Below it, every position is emitted as a
// `Single` shard with no combine attempt.
// Why: For small residual counts (Mono: 4), the per-rule literal-
// prefix optimisation in the `regex` crate beats a combined
// gate; the bench-derived choice of 16 leaves Mono on the
// Singles path while admitting Combined for larger residual
// buckets where the trade flips. See the comment block on
// `build_residual_shards` for the bench data.
// TS map: `const GREEDY_COMBINE_THRESHOLD = 16;`.
//
// In TS you'd write (pseudocode):
// ```ts
// const GREEDY_COMBINE_THRESHOLD = 16;
// ```
const GREEDY_COMBINE_THRESHOLD: usize = 16;
// What: `fn greedy_combine(positions, regex_specs) -> Vec<ResidualShard>`
// is a recursive divide-and-conquer partitioner. For each slice:
// - len == 0: empty.
// - len == 1: one `ResidualShard::Single` (no compile).
// - len >= 2: try compiling all positions into a single
// combined-alternation gate. On success, return one
// `ResidualShard::Combined` covering the whole slice. On
// failure, split in half and recurse on each half in
// parallel via `rayon::join`.
// Why: Replaces the previous `try_build_shards` halving loop, which
// was all-or-nothing: a single combined-compile failure forced
// a global retry at half the shard size, wasting all successful
// compiles at the larger size. Greedy partitioning keeps every
// successful sub-combine and only re-partitions failed
// sub-slices, producing fewer/larger shards on workloads where
// SOME but not all rules are combinable.
// Failure mode: when no rules combine (current Mono ruleset:
// 4 residuals, all hard-shape betterleaks rules), we pay
// log2(N) levels of failed combine attempts before bottoming
// out at all-Singles. With N=4 that's at most 3 failed
// attempts (1 at level 0 + 2 parallel at level 1), bounded
// by the 28ms hyperfine σ on the Mono baseline -- empirically
// invisible. For workloads where many combines succeed, greedy
// wins by reducing per-file scan to one gate.is_match per
// shard instead of one find_all per rule.
// TS map: `function greedyCombine(positions, regexSpecs): ResidualShard[]`.
//
// In TS you'd write (pseudocode):
// ```ts
// function greedyCombine(positions: number[], regexSpecs: [number, string][]): ResidualShard[] {
// if (positions.length === 0) return [];
// if (positions.length === 1) return [{ kind: "single", rulePos: positions[0] }];
// const gate = tryCompileCombined(positions, regexSpecs);
// if (gate) return [{ kind: "combined", gate, positions: [...positions] }];
// const mid = Math.floor(positions.length / 2);
// return [
// ...greedyCombine(positions.slice(0, mid), regexSpecs),
// ...greedyCombine(positions.slice(mid), regexSpecs),
// ];
// }
// ```
// What: `fn try_compile_combined(chunk, regex_specs) -> Option<CompiledRegex>`
// builds a combined-alternation source for `chunk` (joining
// each rule's regex source with `|`, wrapping each in parens),
// dispatches to resharp or regex via the same hybrid-engine
// rule used for individual rules (`requires_resharp` shallow
// string scan), and returns the compiled gate or `None` on
// parse/algebra/HIR-translator failure.
// Why: The combined alternation can fail on resharp when the union
// triggers HIR-translator UnsupportedPattern errors (e.g. the
// bisect-derived 1722 rule cliff for synthetic `_RESID_`
// shapes). A `None` return signals the caller to recurse with
// a smaller slice rather than aborting the whole load.
// TS map: `function tryCompileCombined(chunk, regexSpecs): Regex | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function tryCompileCombined(chunk: number[], regexSpecs: [number, string][]): Regex | null {
// const combined = chunk.map(p => `(${regexSpecs[p][1]})`).join("|");
// try { return new Regex(combined); }
// catch { return null; }
// }
// ```