Repository navigation
Commit e8b295c
Improve GC stack scanning and GcInfoDecoder performance (#135807)
## Summary
When the managed heap is small or when most marking is concurrent, stack
scanning (`GcScanRoots`) can account for a substantial portion of GC
pause duration.
Profiling and instrumenting a 500-frame deep stack walk during
`GC.Collect(0)` showed that each frameless activation was doing
redundant work:
- **3–4 `GetGCInfoToken` virtual calls and 3–4 `GcInfoDecoder`
constructions per frame** (up to 4 on ReadyToRun frames and 4–5 on
shared generic frames).
- **1 full `T_CONTEXT` (1,232 bytes on Windows x64; 3,232 bytes on Linux
x64) + `T_KNONVOLATILE_CONTEXT_POINTERS` (128 bytes) copy per frame**
due to `CheckForSkippedFrames` unconditionally forcing an out-of-place
unwind via `EnsureCallerContextIsValid` even when `m_crawl.pFrame` is at
the base of the stack (`DebuggerU2MCatchHandlerFrame`) or `FRAME_TOP`.
- **Full FP/vector state copies** whenever `EnsureCallerContextIsValid`
*is* needed during a GC scan (e.g., on frames with a GS cookie).
- **Bit-at-a-time `ReadOneFast()` loops** in
`GcInfoDecoder::EnumerateLiveSlots` when scanning
partially-interruptible live slot bitmasks and `couldBeLive`
bit-vectors.
## Changes
### 1. Eliminate redundant per-frame GCInfo lookups and header decodes
- **1a. R2R `JitCodeToMethodInfo` (`codeman.cpp`):** Reuse the
already-computed `HotColdMappingLookupTable::LookupMappingForMethod`
result (`isColdCode`). When the frame is in hot code, compute
`m_relOffset = currentInstr -
JitTokenToStartAddress(pCodeInfo->m_methodToken)` directly instead of
calling `JitTokenToMethodRegionInfo` (which decoded unwind info size,
constructed a `GcInfoDecoder(DECODE_CODE_LENGTH)` to get `hotSize`, and
ran a second binary search in `LookupMappingForMethod`). Checked builds
assert equality against `JitTokenToMethodRegionInfo`.
- **1b. Cache `GCInfoToken` in `EECodeInfo` (`codeman.h`, `codeman.cpp`,
`jitinterface.cpp`, `AsmOffsets.cs`):** Lazily cache `m_gcInfoToken` in
`EECodeInfo::GetGCInfoToken()` and reset it in `EECodeInfo`
initialization and `JitCodeToMethodInfo`. Reduces
`IJitManager::GetGCInfoToken` calls from 3–4 per frame to 1 per frame.
- **1c. Gate `GetParamContextType` in `GcStackCrawlCallBack`
(`gcenv.ee.common.cpp`):** Only query `GetParamContextType` when the
`MethodDesc` requires a generic context parameter
(`RequiresInstMethodTableArg() || RequiresInstMethodDescArg()`, with
Checked-build assertions verifying that
`AcquiresInstMethodTableFromThis()` only produces `THIS`/`NONE` and all
other methods produce `NONE`).
- **1d. Merge generic context decoders (`eetwain.h`, `eetwain.cpp`,
`codeman.cpp`, `gcenv.ee.common.cpp`):** Extend
`ICodeManager::GetParamContextType` to optionally return the generic
context token and prolog check from a single `GcInfoDecoder(gcInfoToken,
DECODE_GENERICS_INST_CONTEXT | DECODE_PROLOG_LENGTH)`, replacing
separate `GetParamContextType`, `SafeToReportGenericParamContext`, and
`GetExactGenericsToken` decoders.
### 2. Avoid unnecessary `T_CONTEXT` copies during stack walks
- **2a. Lazy caller context in `CheckForSkippedFrames` (`stackwalk.cpp`,
`eetwain.cpp`, `exceptionhandling.cpp`, `threadsuspend.cpp`,
`frameinfo.cpp`, `dacdbiimplstackwalk.cpp`):** Avoid forcing
`EnsureCallerContextIsValid` in `CheckForSkippedFrames` when
`m_crawl.pFrame == FRAME_TOP` or when `m_crawl.pFrame` is above
`currentSP` and cannot belong to the current frameless activation. For
frames without a GS cookie, `pRD->IsCallerContextValid` stays `FALSE`
until `NextRaw()`, allowing `Thread::VirtualUnwindCallFrame` to unwind
`pCurrentContext` in place with zero context copies. Checked builds
assert after unwinding that `m_crawl.pFrame` was not skipped.
- **2b. Cheaper context copy for GC root scans (`threads.h`,
`eetwain.h`, `gcenv.ee.cpp`, `stackwalk.cpp`, `eetwain.cpp`,
`frames.cpp`):** Add `GC_ROOT_SCAN` / `ICodeManagerFlags::GcRootScan`
set by `ScanStackRoots`. When `EnsureCallerContextIsValid` is called on
a GC scan (e.g., for GS-cookie frames), `CopyCallerContextForGCScan`
copies only flags, GPRs, FP, SP, and IP (144 bytes instead of 1,232
bytes on Windows x64), preserving `ContextPointers` and ARM64 PAC SP
fields while skipping the FP/vector state.
### 3. Word-level bit scanning in `GcInfoDecoder` (`gcinfodecoder.h`,
`gcinfodecoder.cpp`)
- **4a. Word reads + `TrailingZeroCount`:** Add
`BitOperations::TrailingZeroCount`, `BitStreamReader::ReadWord`, and
`BitStreamReader::SkipZerosAndReadOne`, replacing the bit-at-a-time
`ReadOneFast()` loops in `TGcInfoDecoder::EnumerateLiveSlots` (shared
across CoreCLR, DAC, and NativeAOT).
---
## Performance Measurements
Measured on Windows x64 with a 500-frame deep stack benchmark
(`GC.Collect(0)` in a loop of 2,000 iterations $\times$ 9 rounds after
warmup, `DOTNET_TieredCompilation=0`) covering:
- **`R2R_Small`**: 500 ReadyToRun frames (`crossgen2`-compiled)
- **`JIT_Small`**: 500 small JIT frames (slim headers)
- **`JIT_GenericShared`**: 500 shared generic frames (`GenericClass<T>`,
`GenericStruct<T>`, `GenericStatic.Recurse<T>`)
- **`JIT_GSCookie`**: 500 frames with `stackalloc` GS cookies (fat
headers)
- **`JIT_ManySlots`**: 500 frames with 32 tracked GC slots and sparse
non-RLE live bit-vectors
- **`Mixed_All`**: 500 frames (100 of each category above)
### Per-Frame Counter Breakdown (Depth = 500)
| Scenario | Frames | `GetGCInfoToken` (Before -> After) |
`GcInfoDecoder` (Before -> After) | `CallerCtxCopy` (Before -> After) |
`ReadOneFast` Live-Slot Bits (Before -> After) |
|---|---:|---:|---:|---:|---:|
| `R2R_Small` | 511 | 2,040 (**3.99/f**) -> **511 (1.00/f)** | 2,040
(**3.99/f**) -> **1,022 (2.00/f)** | 511 (**1.00/f**) -> **1 (0.00/f)**
| 1,398 -> **0** |
| `JIT_Small` | 510 | 1,535 (**3.01/f**) -> **510 (1.00/f)** | 1,535
(**3.01/f**) -> **1,020 (2.00/f)** | 510 (**1.00/f**) -> **1 (0.00/f)**
| 1,394 -> **0** |
| `JIT_GenericShared` | 507 | 1,857 (**3.66/f**) -> **507 (1.00/f)** |
1,857 (**3.66/f**) -> **1,355 (2.67/f)** | 507 (**1.00/f**) -> **1
(0.00/f)** | 2,354 -> **0** |
| `JIT_GSCookie` | 510 | 1,535 (**3.01/f**) -> **510 (1.00/f)** | 1,535
(**3.01/f**) -> **1,020 (2.00/f)** | 510 (**1.00/f** full) -> **501
(GPR-only)** | 2,020 -> **0** |
| `JIT_ManySlots` | 511 | 1,539 (**3.01/f**) -> **511 (1.00/f)** | 1,539
(**3.01/f**) -> **1,022 (2.00/f)** | 511 (**1.00/f**) -> **1 (0.00/f)**
| 15,047 -> **0** |
| `Mixed_All` | 521 | 1,738 (**3.34/f**) -> **521 (1.00/f)** | 1,738
(**3.34/f**) -> **1,111 (2.13/f)** | 521 (**1.00/f**) -> **101
(GPR-only)** | 4,499 -> **0** |
### Workstation GC (`DOTNET_gcServer=0`) — Median
`GC.GetTotalPauseDuration()` ($\mu\text{s}$/GC)
| Scenario | Baseline | + 1a (R2R Offset) | + 1b (Token Cache) | + 1c
(Gate Generic) | + 1d (Merge Decoders) | + 2a (Lazy CallerCtx) | + 2b
(GPR-only Copy) | + 4a (Word BitScan) | Total Change |
|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|
| `R2R_Small` | 174.64 | 160.06 | 156.67 | 156.64 | 155.03 | 150.94 |
146.44 | **140.13** | **-34.51 $\mu\text{s}$ (-19.8%)** |
| `JIT_Small` | 154.95 | 145.14 | 146.03 | 148.23 | 143.74 | 134.89 |
138.28 | **130.81** | **-24.14 $\mu\text{s}$ (-15.6%)** |
| `JIT_GenericShared` | 209.17 | 191.45 | 188.61 | 202.24 | 180.55 |
174.11 | 172.35 | **169.68** | **-39.49 $\mu\text{s}$ (-18.9%)** |
| `JIT_GSCookie` | 203.32 | 186.78 | 187.26 | 182.42 | 177.60 | 181.05 |
171.92 | **174.15** | **-29.17 $\mu\text{s}$ (-14.3%)** |
| `JIT_ManySlots` | 328.74 | 317.23 | 325.93 | 331.13 | 320.19 | 321.13
| 328.38 | **317.58** | **-11.16 $\mu\text{s}$ (-3.4%)** |
| `Mixed_All` | 219.63 | 204.76 | 205.31 | 204.21 | 195.45 | 197.12 |
194.44 | **193.07** | **-26.56 $\mu\text{s}$ (-12.1%)** |
### Server GC (`DOTNET_gcServer=1`) — Median
`GC.GetTotalPauseDuration()` ($\mu\text{s}$/GC)
| Scenario | Baseline | Final | Total Change |
|---|---:|---:|---:|
| `R2R_Small` | 249.78 | **219.30** | **-30.48 $\mu\text{s}$ (-12.2%)**
|
| `JIT_Small` | 228.74 | **211.69** | **-17.05 $\mu\text{s}$ (-7.5%)** |
| `JIT_GenericShared` | 286.12 | **247.11** | **-39.01 $\mu\text{s}$
(-13.6%)** |
| `JIT_GSCookie` | 278.73 | **252.43** | **-26.30 $\mu\text{s}$
(-9.4%)** |
| `JIT_ManySlots` | 410.27 | **398.15** | **-12.12 $\mu\text{s}$
(-3.0%)** |
| `Mixed_All` | 298.87 | **274.87** | **-24.00 $\mu\text{s}$ (-8.0%)** |
---
## Validation
- `.\build.cmd clr+libs -rc Release -lc Release` (including
`mscordaccore` and `Runtime.WorkstationGC` / `Runtime.ServerGC`
NativeAOT libraries): **Passed**
- `.\build.cmd clr -rc Checked`: **Passed**
- Library test suites against Checked CoreCLR:
- `dotnet build /t:Test
src\libraries\System.Runtime.Loader\tests\System.Runtime.Loader.Tests.csproj
-p:Configuration=Release`: **Passed**
- `dotnet build /t:Test
src\libraries\System.Collections\tests\System.Collections.Tests.csproj
-p:Configuration=Release`: **Passed**
- `dotnet build /t:Test
src\libraries\System.Runtime.InteropServices\tests\System.Runtime.InteropServices.UnitTests\System.Runtime.InteropServices.Tests.csproj
-p:Configuration=Release`: **Passed**
- Runtime test suites against Checked CoreCLR:
- `src\tests\build.cmd -Test JIT\Generics\JIT.Generics.csproj x64
Checked` + `corerun.exe JIT.Generics.dll`: **Passed (exit code 100)**
- `src\tests\build.cmd -Test async\async.csproj x64 Checked -Priority 1`
+ `corerun.exe async.dll`: **Passed (exit code 100)**
> [!NOTE]
> This pull request was authored with assistance from GitHub Copilot.
---------
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Jan Kotas <[email protected]>1 parent 64c190b commit e8b295c
20 files changed
Lines changed: 729 additions & 262 deletions
File tree
- src
- coreclr
- System.Private.CoreLib/src/System/Runtime/ExceptionServices
- inc
- vm
- tests
- Loader/CollectibleAssemblies/ByRefLocals
- Regressions/coreclr/GitHub_135530
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
62 | | - | |
63 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
67 | | - | |
68 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
72 | | - | |
73 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | | - | |
123 | | - | |
124 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
125 | 125 | | |
126 | 126 | | |
127 | | - | |
128 | | - | |
129 | | - | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
130 | 130 | | |
131 | 131 | | |
132 | | - | |
133 | | - | |
134 | | - | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
135 | 135 | | |
136 | 136 | | |
137 | 137 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| 98 | + | |
98 | 99 | | |
99 | 100 | | |
100 | 101 | | |
| |||
199 | 200 | | |
200 | 201 | | |
201 | 202 | | |
| 203 | + | |
| 204 | + | |
202 | 205 | | |
203 | 206 | | |
204 | 207 | | |
| |||
228 | 231 | | |
229 | 232 | | |
230 | 233 | | |
231 | | - | |
| 234 | + | |
| 235 | + | |
232 | 236 | | |
233 | | - | |
| 237 | + | |
| 238 | + | |
234 | 239 | | |
235 | 240 | | |
236 | 241 | | |
| |||
426 | 431 | | |
427 | 432 | | |
428 | 433 | | |
429 | | - | |
| 434 | + | |
| 435 | + | |
430 | 436 | | |
431 | | - | |
| 437 | + | |
| 438 | + | |
432 | 439 | | |
433 | 440 | | |
434 | 441 | | |
435 | | - | |
| 442 | + | |
436 | 443 | | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | 444 | | |
442 | 445 | | |
443 | 446 | | |
| |||
515 | 518 | | |
516 | 519 | | |
517 | 520 | | |
518 | | - | |
| 521 | + | |
519 | 522 | | |
520 | 523 | | |
521 | 524 | | |
| |||
600 | 603 | | |
601 | 604 | | |
602 | 605 | | |
603 | | - | |
| 606 | + | |
| 607 | + | |
604 | 608 | | |
605 | 609 | | |
606 | 610 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
204 | 227 | | |
205 | 228 | | |
206 | 229 | | |
| |||
347 | 370 | | |
348 | 371 | | |
349 | 372 | | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
350 | 422 | | |
351 | 423 | | |
352 | 424 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
832 | 832 | | |
833 | 833 | | |
834 | 834 | | |
835 | | - | |
836 | | - | |
837 | | - | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
838 | 838 | | |
839 | 839 | | |
840 | 840 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3625 | 3625 | | |
3626 | 3626 | | |
3627 | 3627 | | |
3628 | | - | |
3629 | | - | |
3630 | | - | |
| 3628 | + | |
| 3629 | + | |
| 3630 | + | |
| 3631 | + | |
| 3632 | + | |
| 3633 | + | |
| 3634 | + | |
| 3635 | + | |
| 3636 | + | |
| 3637 | + | |
3631 | 3638 | | |
3632 | | - | |
3633 | | - | |
3634 | | - | |
3635 | | - | |
3636 | | - | |
3637 | | - | |
3638 | | - | |
3639 | | - | |
3640 | | - | |
3641 | | - | |
3642 | | - | |
3643 | | - | |
3644 | | - | |
3645 | | - | |
3646 | | - | |
| 3639 | + | |
| 3640 | + | |
| 3641 | + | |
| 3642 | + | |
| 3643 | + | |
| 3644 | + | |
| 3645 | + | |
| 3646 | + | |
| 3647 | + | |
| 3648 | + | |
| 3649 | + | |
| 3650 | + | |
| 3651 | + | |
3647 | 3652 | | |
3648 | | - | |
3649 | | - | |
3650 | | - | |
3651 | | - | |
3652 | | - | |
3653 | | - | |
3654 | | - | |
| 3653 | + | |
3655 | 3654 | | |
| 3655 | + | |
3656 | 3656 | | |
3657 | | - | |
| 3657 | + | |
3658 | 3658 | | |
3659 | | - | |
| 3659 | + | |
3660 | 3660 | | |
3661 | | - | |
| 3661 | + | |
3662 | 3662 | | |
3663 | | - | |
3664 | | - | |
3665 | | - | |
| 3663 | + | |
| 3664 | + | |
3666 | 3665 | | |
3667 | 3666 | | |
3668 | 3667 | | |
| |||
4688 | 4687 | | |
4689 | 4688 | | |
4690 | 4689 | | |
| 4690 | + | |
4691 | 4691 | | |
4692 | 4692 | | |
4693 | 4693 | | |
| |||
7242 | 7242 | | |
7243 | 7243 | | |
7244 | 7244 | | |
7245 | | - | |
| 7245 | + | |
| 7246 | + | |
7246 | 7247 | | |
7247 | 7248 | | |
7248 | 7249 | | |
| |||
7265 | 7266 | | |
7266 | 7267 | | |
7267 | 7268 | | |
| 7269 | + | |
7268 | 7270 | | |
7269 | 7271 | | |
7270 | 7272 | | |
7271 | | - | |
7272 | | - | |
7273 | | - | |
| 7273 | + | |
| 7274 | + | |
7274 | 7275 | | |
| 7276 | + | |
| 7277 | + | |
| 7278 | + | |
7275 | 7279 | | |
7276 | 7280 | | |
7277 | 7281 | | |
7278 | 7282 | | |
| 7283 | + | |
7279 | 7284 | | |
7280 | | - | |
| 7285 | + | |
7281 | 7286 | | |
| 7287 | + | |
| 7288 | + | |
| 7289 | + | |
| 7290 | + | |
| 7291 | + | |
| 7292 | + | |
| 7293 | + | |
| 7294 | + | |
| 7295 | + | |
7282 | 7296 | | |
7283 | 7297 | | |
7284 | 7298 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3232 | 3232 | | |
3233 | 3233 | | |
3234 | 3234 | | |
3235 | | - | |
| 3235 | + | |
| 3236 | + | |
| 3237 | + | |
| 3238 | + | |
| 3239 | + | |
| 3240 | + | |
| 3241 | + | |
3236 | 3242 | | |
3237 | 3243 | | |
3238 | 3244 | | |
| |||
3306 | 3312 | | |
3307 | 3313 | | |
3308 | 3314 | | |
| 3315 | + | |
3309 | 3316 | | |
3310 | 3317 | | |
3311 | 3318 | | |
| |||
3351 | 3358 | | |
3352 | 3359 | | |
3353 | 3360 | | |
3354 | | - | |
3355 | | - | |
3356 | 3361 | | |
0 commit comments