Commit bf7870a
authored
feat: lazy stack trace capture with deferred symbolization (#19)
* feat: lazy stack trace capture with deferred symbolization
Replace eager per-frame runtime.Caller + runtime.FuncForPC with batch
runtime.Callers for PC capture, deferring symbolization to first
StackFrame() access via sync.Once.
Performance (errors.New): 3500ns→540ns (6.5x), 1600B→392B (75% less)
Deep stacks (32 frames): 40500ns→1060ns (38x), 17KB→392B (98% less)
Changes:
- Default stack depth reduced from 64 to 16 (configurable via SetMaxStackDepth)
- captureStack: single runtime.Callers call instead of loop of runtime.Caller
- StackFrame: lazy resolution via sync.Once + runtime.CallersFrames
- All receivers changed to pointer (required by sync.Once, consistent)
- SetMaxStackDepth: atomic.Int32 for concurrent safety
- resolveFrames: pre-allocated slice, strings.TrimPrefix for base path
- splitFuncName: takes string instead of uintptr (no runtime.FuncForPC)
* fix: clamp SetMaxStackDepth to 1-256 range
* fix: snapshot basePath at capture time, add tests, document depth limit
- Snapshot basePath in captureStack so lazy resolution uses the value
from error creation time, not access time (eliminates race)
- resolveFrames takes basePath parameter instead of reading global
- Document SetMaxStackDepth accepts [1, 256]
- Add TestSetMaxStackDepth (valid, zero, negative, over-256)
- Add TestStackFrameConsistency (repeated calls, Callers parity)
- Update existing depth tests for new default of 16
* fix: copy basePath on wrap, cap resolveFrames for inlining, relax test assertions
- Copy basePath from wrapped ErrorExt so path trimming works on re-wrapped errors
- Cap resolveFrames output at len(pcs) to handle inlining frame expansion
- Test assertions use Callers() length (PCs) instead of StackFrame() length
- Relax 1:1 PC-to-frame assumption in TestStackFrameConsistency
* fix: use atomic.Value for basePath to eliminate read/write race1 parent c1f9b08 commit bf7870a
5 files changed
Lines changed: 213 additions & 71 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| 110 | + | |
110 | 111 | | |
111 | 112 | | |
112 | 113 | | |
| |||
124 | 125 | | |
125 | 126 | | |
126 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
127 | 136 | | |
128 | | - | |
| 137 | + | |
129 | 138 | | |
130 | 139 | | |
131 | 140 | | |
| |||
134 | 143 | | |
135 | 144 | | |
136 | 145 | | |
137 | | - | |
| 146 | + | |
138 | 147 | | |
139 | 148 | | |
140 | 149 | | |
141 | 150 | | |
142 | 151 | | |
143 | | - | |
| 152 | + | |
144 | 153 | | |
145 | 154 | | |
146 | | - | |
| 155 | + | |
147 | 156 | | |
148 | 157 | | |
149 | 158 | | |
| |||
163 | 172 | | |
164 | 173 | | |
165 | 174 | | |
166 | | - | |
| 175 | + | |
167 | 176 | | |
168 | 177 | | |
169 | 178 | | |
| |||
201 | 210 | | |
202 | 211 | | |
203 | 212 | | |
204 | | - | |
| 213 | + | |
205 | 214 | | |
206 | 215 | | |
207 | 216 | | |
| |||
210 | 219 | | |
211 | 220 | | |
212 | 221 | | |
213 | | - | |
| 222 | + | |
214 | 223 | | |
215 | 224 | | |
216 | 225 | | |
| |||
219 | 228 | | |
220 | 229 | | |
221 | 230 | | |
222 | | - | |
| 231 | + | |
223 | 232 | | |
224 | 233 | | |
225 | 234 | | |
| |||
228 | 237 | | |
229 | 238 | | |
230 | 239 | | |
231 | | - | |
| 240 | + | |
232 | 241 | | |
233 | 242 | | |
234 | 243 | | |
| |||
266 | 275 | | |
267 | 276 | | |
268 | 277 | | |
269 | | - | |
| 278 | + | |
270 | 279 | | |
271 | 280 | | |
272 | 281 | | |
| |||
340 | 349 | | |
341 | 350 | | |
342 | 351 | | |
343 | | - | |
| 352 | + | |
344 | 353 | | |
345 | 354 | | |
346 | 355 | | |
| |||
349 | 358 | | |
350 | 359 | | |
351 | 360 | | |
352 | | - | |
| 361 | + | |
353 | 362 | | |
354 | 363 | | |
355 | 364 | | |
| |||
358 | 367 | | |
359 | 368 | | |
360 | 369 | | |
361 | | - | |
| 370 | + | |
362 | 371 | | |
363 | 372 | | |
364 | 373 | | |
| |||
403 | 412 | | |
404 | 413 | | |
405 | 414 | | |
406 | | - | |
| 415 | + | |
407 | 416 | | |
408 | 417 | | |
409 | 418 | | |
| |||
442 | 451 | | |
443 | 452 | | |
444 | 453 | | |
445 | | - | |
| 454 | + | |
446 | 455 | | |
447 | 456 | | |
448 | 457 | | |
| |||
456 | 465 | | |
457 | 466 | | |
458 | 467 | | |
459 | | - | |
| 468 | + | |
460 | 469 | | |
461 | 470 | | |
462 | 471 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| |||
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| 19 | + | |
| 20 | + | |
17 | 21 | | |
18 | | - | |
19 | | - | |
| 22 | + | |
| 23 | + | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
| |||
52 | 56 | | |
53 | 57 | | |
54 | 58 | | |
| 59 | + | |
| 60 | + | |
55 | 61 | | |
56 | 62 | | |
57 | 63 | | |
| |||
69 | 75 | | |
70 | 76 | | |
71 | 77 | | |
72 | | - | |
| 78 | + | |
73 | 79 | | |
74 | 80 | | |
75 | 81 | | |
76 | 82 | | |
77 | | - | |
| 83 | + | |
78 | 84 | | |
79 | 85 | | |
80 | 86 | | |
81 | 87 | | |
82 | | - | |
| 88 | + | |
83 | 89 | | |
84 | 90 | | |
85 | 91 | | |
86 | 92 | | |
87 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
88 | 100 | | |
89 | 101 | | |
90 | 102 | | |
91 | 103 | | |
92 | | - | |
| 104 | + | |
93 | 105 | | |
94 | 106 | | |
95 | 107 | | |
96 | 108 | | |
97 | | - | |
| 109 | + | |
98 | 110 | | |
99 | 111 | | |
100 | 112 | | |
| |||
106 | 118 | | |
107 | 119 | | |
108 | 120 | | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
120 | 144 | | |
| 145 | + | |
121 | 146 | | |
122 | 147 | | |
123 | | - | |
| 148 | + | |
124 | 149 | | |
125 | 150 | | |
126 | | - | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
127 | 154 | | |
128 | | - | |
129 | | - | |
130 | 155 | | |
131 | 156 | | |
132 | 157 | | |
133 | 158 | | |
134 | | - | |
| 159 | + | |
135 | 160 | | |
136 | 161 | | |
137 | 162 | | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
| 163 | + | |
| 164 | + | |
144 | 165 | | |
145 | | - | |
| 166 | + | |
146 | 167 | | |
147 | 168 | | |
148 | 169 | | |
| |||
220 | 241 | | |
221 | 242 | | |
222 | 243 | | |
223 | | - | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
224 | 247 | | |
225 | 248 | | |
226 | 249 | | |
| |||
234 | 257 | | |
235 | 258 | | |
236 | 259 | | |
237 | | - | |
| 260 | + | |
238 | 261 | | |
239 | 262 | | |
240 | 263 | | |
241 | 264 | | |
242 | 265 | | |
243 | | - | |
| 266 | + | |
| 267 | + | |
244 | 268 | | |
245 | | - | |
246 | | - | |
| 269 | + | |
| 270 | + | |
247 | 271 | | |
248 | 272 | | |
249 | 273 | | |
| |||
261 | 285 | | |
262 | 286 | | |
263 | 287 | | |
264 | | - | |
| 288 | + | |
265 | 289 | | |
266 | 290 | | |
0 commit comments