Repository navigation
Commit 182b010
perf: resolve sysmon branch events lazily, one pair at a time (#2221)
* perf: resolve sysmon branch events lazily, one pair at a time
On the first BRANCH_LEFT/RIGHT event of every code object, the sysmon
core precomputed branch trails for every branch site in the code object:
two full dis.get_instructions() decodes (branch_trails and always_jumps
each built their own InstructionWalker), two trail walks per conditional
jump, and re-registration of every trail under every offset it contains.
On test-suite-shaped workloads — thousands of code objects, most
branches executed a handful of times — this analysis dominates: branch
mode on the pyca/cryptography suite (4,277 tests, 5,671 traced code
objects) costs +35% vs +2% for line mode (issue #2172).
But branch events are one-shot: each (site, direction) fires once and is
then DISABLEd, so at most two pairs per branch site ever need resolving,
and only for branches that actually execute. Replace the precompute
with a BranchArcResolver that resolves each (source offset, destination
offset) pair as it arrives, walking the raw co_code bytes from the
destination — following unconditional jumps, skipping CACHE and
EXTENDED_ARG, stopping at a new source line (arc), a return (arc to
exit), or another branch possibility (no arc: that branch produces its
own events). Lines come from the byte_to_line dict the tracer already
builds, so nothing in the branch measurement path disassembles whole
code objects any more. branch_trails(), always_jumps(), and
InstructionWalker become unused and are removed.
Computing jump targets needs the number of inline CACHE entries that
follow the jump instruction (jump distances are measured from the end
of the caches). Rather than reading opcode._inline_cache_entries — a
private table whose shape has already changed once (list in 3.11, dict
in 3.12) — the resolver counts the CACHE opcodes directly in co_code:
exact by construction, self-consistent with the code object being
walked, and no private API.
Measured on the cryptography suite (Python 3.14.2, branch mode, wall
time best of 3, base 35.44s): 47.90s (+35.2%) before, 38.33s (+8.2%)
after; combined with the sysmon-multiline-map branch the overhead is at
the run-to-run noise floor (~0-2%). Correctness verified by: coverage's
own test suite under COVERAGE_CORE=sysmon on 3.14 (identical failure set
to main, all environmental); resolving 17,445 (source, dest) pairs
across 3,571 code objects identically to a dis-based reference
implementation; and byte-identical 'coverage report' output on the
cryptography suite (26,376 statements, 2,484 branches). The raw arc
data differs by five arcs on that suite: the old code's byte_to_line
fallback recorded arcs between two lines inside the same multi-line
statement, which the resolver correctly attributes to the statement's
first line; all five collapse to self-arcs at report time.
Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0137DLbSXfm5v5bhCcz7xEKM
* update changes
---------
Co-authored-by: Claude <[email protected]>
Co-authored-by: Ned Batchelder <[email protected]>1 parent ee271ee commit 182b010
3 files changed
Lines changed: 110 additions & 177 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| 42 | + | |
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
12 | | - | |
13 | 11 | | |
14 | 12 | | |
15 | 13 | | |
| |||
108 | 106 | | |
109 | 107 | | |
110 | 108 | | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | 109 | | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
130 | 114 | | |
131 | | - | |
132 | | - | |
133 | | - | |
| 115 | + | |
| 116 | + | |
134 | 117 | | |
135 | | - | |
136 | | - | |
| 118 | + | |
| 119 | + | |
137 | 120 | | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | 121 | | |
| 122 | + | |
| 123 | + | |
158 | 124 | | |
159 | | - | |
160 | | - | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
161 | 131 | | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
162 | 138 | | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | 139 | | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | 140 | | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
196 | 163 | | |
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 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
191 | 183 | | |
192 | 184 | | |
193 | 185 | | |
| |||
374 | 366 | | |
375 | 367 | | |
376 | 368 | | |
377 | | - | |
378 | | - | |
| 369 | + | |
379 | 370 | | |
380 | 371 | | |
381 | 372 | | |
| |||
460 | 451 | | |
461 | 452 | | |
462 | 453 | | |
463 | | - | |
| 454 | + | |
| 455 | + | |
464 | 456 | | |
465 | 457 | | |
466 | | - | |
467 | | - | |
468 | | - | |
469 | | - | |
470 | | - | |
471 | | - | |
472 | | - | |
473 | | - | |
474 | | - | |
475 | | - | |
476 | | - | |
477 | | - | |
478 | | - | |
479 | | - | |
480 | | - | |
481 | | - | |
482 | | - | |
483 | | - | |
484 | | - | |
485 | | - | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
491 | 469 | | |
492 | 470 | | |
493 | 471 | | |
| |||
0 commit comments