Commit 85f1ca4
Optimize nettrace-to-TraceLog Conversion (#2403)
* Use min-heap for EventCache.SortAndDispatch
Replace the O(N*T) linear-scan merge in SortAndDispatch with an O(N*log(T))
min-heap merge, where N is the number of events and T is the number of
threads. The previous implementation rebuilt a List from LINQ on every call
and linearly scanned all thread queues for the minimum timestamp per event.
The new implementation uses an array-backed binary min-heap keyed by
timestamp. After extracting the minimum, only a single O(log T) sift-down
is needed to restore the heap property. The heap list is reused across
calls to avoid per-call allocations.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Track active thread queues in EventCache.SortAndDispatch
Maintain a HashSet of thread queues that have pending events instead of
iterating all threads in the dictionary on every SortAndDispatch call.
Queues are added to the active set when their first event is enqueued
and removed when drained. This eliminates the Dictionary.Values
enumeration which was ~28% of CPU during nettrace-to-TraceLog conversion.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Cache ParsedSymbolMetadata to avoid repeated JSON deserialization
Cache the result of ProcessMappingSymbolMetadataParser.TryParse() in
ProcessMappingMetadataTraceData so that repeated accesses to the
ParsedSymbolMetadata property do not re-invoke JSON deserialization.
The property is accessed twice per mapping event (for PE and ELF
metadata checks), and the metadata objects are shared across multiple
mappings via MetadataId. This was ~10% of CPU during conversion.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Avoid redundant FileName string allocations in ProcessMapping handler
Read ProcessMappingTraceData.FileName once into a local variable and pass
it directly to UniversalMapping(string, ...) instead of going through the
UniversalMapping(ProcessMappingTraceData, ...) overload. Previously,
FileName was accessed 3 times per mapping event (IsNullOrEmpty check,
StartsWith check, and inside UniversalMapping), each time allocating a
new string via GetShortUTF8StringAt(). String allocation was ~12% of CPU
in Release-mode profiling.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Reset ParsedSymbolMetadata cache in Dispatch to prevent stale data
TraceEvent objects are reused across callbacks. The cached
ParsedSymbolMetadata fields were never cleared between dispatches,
which could return metadata from a previous event if the property
was accessed on the template object rather than a clone.
Reset _parsedSymbolMetadataCached and _parsedSymbolMetadata at the
start of Dispatch() before invoking the callback.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Address code review feedback: MinHeap class, Clone override, comments
Refactor the min-heap helpers into a self-contained private MinHeap
class with XML doc comments on all public methods. Add comments
explaining the binary heap child index formulas (2i+1, 2i+2). Use
C# tuple swap syntax instead of a temp variable.
Add Clone() override to ProcessMappingMetadataTraceData to explicitly
copy the cached ParsedSymbolMetadata fields into the clone. Strings
are immutable so a shallow copy of the reference is sufficient.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Add MinHeap unit tests and improve Build() comment
Make MinHeap generic (MinHeap<TValue>) and internal so it can be
tested from the test project. Add 13 unit tests covering: empty heap,
single element, ascending/descending/random input, duplicate keys,
ReplaceRoot, RemoveRoot, Clear, and mixed operations. Add a comment
to Build() explaining why iteration starts at Count/2-1.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 696960c commit 85f1ca4
4 files changed
Lines changed: 415 additions & 27 deletions
File tree
- src/TraceEvent
- EventPipe
- Parsers
- SourceConverters
- TraceEvent.Tests/Cache
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
| |||
68 | 67 | | |
69 | 68 | | |
70 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
71 | 74 | | |
72 | 75 | | |
73 | 76 | | |
| |||
165 | 168 | | |
166 | 169 | | |
167 | 170 | | |
168 | | - | |
169 | | - | |
170 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
171 | 178 | | |
172 | | - | |
173 | | - | |
174 | | - | |
| 179 | + | |
175 | 180 | | |
176 | | - | |
| 181 | + | |
| 182 | + | |
177 | 183 | | |
178 | | - | |
| 184 | + | |
179 | 185 | | |
180 | | - | |
181 | | - | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
182 | 207 | | |
183 | | - | |
184 | | - | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
185 | 214 | | |
186 | 215 | | |
187 | | - | |
| 216 | + | |
188 | 217 | | |
189 | | - | |
| 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 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
190 | 298 | | |
191 | 299 | | |
192 | 300 | | |
193 | | - | |
194 | | - | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
195 | 304 | | |
196 | 305 | | |
197 | 306 | | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
203 | 312 | | |
204 | | - | |
| 313 | + | |
| 314 | + | |
205 | 315 | | |
206 | | - | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
207 | 337 | | |
208 | 338 | | |
209 | 339 | | |
210 | 340 | | |
| 341 | + | |
| 342 | + | |
211 | 343 | | |
212 | 344 | | |
213 | 345 | | |
| |||
277 | 409 | | |
278 | 410 | | |
279 | 411 | | |
| 412 | + | |
| 413 | + | |
280 | 414 | | |
281 | 415 | | |
282 | 416 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
355 | 355 | | |
356 | 356 | | |
357 | 357 | | |
358 | | - | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
359 | 372 | | |
360 | 373 | | |
361 | 374 | | |
| |||
367 | 380 | | |
368 | 381 | | |
369 | 382 | | |
| 383 | + | |
| 384 | + | |
370 | 385 | | |
371 | 386 | | |
372 | 387 | | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
373 | 396 | | |
374 | 397 | | |
375 | 398 | | |
| |||
Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
| 66 | + | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
73 | | - | |
| 74 | + | |
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
| |||
0 commit comments