Skip to content

Commit 7c10f37

Browse files
authored
Merge pull request #327 from PhysShell/claude/dotnet-perf-research-6147se
docs(notes): measured .NET optimization limits and GC/LOH observability
2 parents 29ccc80 + 0cd4311 commit 7c10f37

16 files changed

Lines changed: 1268 additions & 0 deletions
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# GC observability and the LOH — what is worth adopting, and what stays runtime-only
2+
3+
Working note. **Trigger:** the *GCExperiment* write-up ("Making .NET GC
4+
behaviour observable"), proposed as material to fold into our runtime layer.
5+
This note records what is genuinely new in it, corrects one number our own docs
6+
imply, and pins the boundary that must not move.
7+
8+
Companion to [`llvm-codegen-feasibility.md`](llvm-codegen-feasibility.md) (the
9+
other half of the same discussion) and to
10+
[P-034](../proposals/P-034-runtime-lifetime-guard.md), whose "ClrMD-free
11+
complement" argument this reuses verbatim.
12+
13+
## The one factual correction worth taking
14+
15+
Our docs treat the Large Object Heap threshold as **85,000 bytes** of payload.
16+
That is the documented **default** constant, but it is not the predicate. Two
17+
things are wrong with the folklore reading:
18+
19+
1. **The threshold is configurable.** `System.GC.LOHThreshold`
20+
(`runtimeconfig.json`) and `DOTNET_GCLOHThreshold` (environment, hex) raise
21+
it, so 85,000 describes a default configuration, not a law.
22+
2. **The comparison is against full object size**, not payload — payload +
23+
object header + method-table pointer + the array length field + alignment
24+
padding — so an array whose *payload* sits comfortably under the limit can
25+
still land on the LOH.
26+
27+
Worked on the environment this was checked against (**.NET CoreCLR, x64,
28+
default GC configuration**), where that overhead is 24 bytes:
29+
30+
> `byte[84_999]` → 85,023 bytes → rounds to 85,024 → **allocated on the LOH.**
31+
32+
The header size is a **platform and runtime detail**, not a portable constant —
33+
object layout and alignment differ by architecture and runtime version, so the
34+
24 above should not be copied into another context as a given.
35+
36+
The practical consequence, and the reason it is worth writing down: the familiar
37+
"keep buffers under 85,000" folklore is **off by roughly one header**, and a
38+
pool sized to exactly `85_000 - 1` is on the wrong heap under the default
39+
configuration. The portable advice is not a corrected arithmetic constant — it
40+
is **measure on your target runtime**, since both the threshold and the overhead
41+
can move.
42+
43+
Where this touches our docs: [`ROADMAP.md`](../ROADMAP.md) and
44+
[`Plan.md`](../../Plan.md) both list LOH fragmentation in the detectability
45+
matrix. Neither states a threshold, so **neither is wrong** — but if a threshold
46+
is ever quoted in a rule, a diagnostic message, or a talk, it must be the
47+
full-object-size version, not the payload one.
48+
49+
## What GCExperiment is, and what is actually adoptable
50+
51+
Four self-contained experiments (LOH placement; generation promotion;
52+
allocation pressure; LOH fragmentation) built on ordinary public APIs —
53+
`GC.GetGeneration`, `GC.Collect` with forced modes,
54+
`GC.WaitForPendingFinalizers`, `GCSettings.LargeObjectHeapCompactionMode`,
55+
plus small `GCMonitor`/`GCInfo` helpers for snapshots and size estimation. It
56+
also flags a real measurement trap: without `GC.KeepAlive`, the JIT can shorten
57+
an object's lifetime and skew the result.
58+
59+
The adoptable idea is **not** the GC content, which is well-trodden. It is the
60+
**delivery shape**, and it is the same shape P-034 already argued for from a
61+
different direction:
62+
63+
> a lifetime/GC observation that runs in an ordinary `dotnet test`, on any OS,
64+
> with no PerfView, no ETW, no Windows stand, and no ClrMD heap walk.
65+
66+
Today our runtime layer ([`Plan.md`](../../Plan.md) §2, category 12) routes
67+
*everything* GC-shaped to PerfView + ETW. That is correct for **evidence** and
68+
badly overweight for **orientation** — it means no GC fact can be established in
69+
CI, on Linux, or in a unit test. A `GC.GetGCMemoryInfo` /
70+
`GC.CollectionCount(n)` snapshot around a scenario costs nothing and needs no
71+
stand — with one caveat that has to travel with it: **both are process-wide, not
72+
scenario-scoped.** `CollectionCount(n)` counts every collection since process
73+
start (and a higher-generation collection bumps the lower ones too);
74+
`GetGCMemoryInfo()` describes the *last* collection and returns an all-zero
75+
struct with `Index == 0` when none of the requested kind has happened. Parallel
76+
tests or background GC move both from outside the scenario. So a probe used as
77+
an assertion has to record counts at the scenario boundary and compare
78+
`GetGCMemoryInfo()` only when the GC index matches — otherwise it is a heuristic
79+
wearing an assertion's clothes.
80+
81+
So: a cheap in-test GC probe is a reasonable sibling to P-034's disposal
82+
quarantine, under the same honest caveat P-034 already states — it proves *what
83+
the counters did during this test*, bounded by test coverage, and it is blind to
84+
*why*. It is a debug assertion, not an auditor.
85+
86+
## The boundary that does not move
87+
88+
**None of this makes LOH fragmentation statically detectable.** The matrix rows
89+
stay exactly as written:
90+
91+
- `ROADMAP.md`: LOH fragmentation → ❌ **impossible** (depends on runtime data
92+
volume / GC timing)
93+
- `Plan.md` category 12: heavy dictionaries / LOH fragmentation / Gen2 bloat →
94+
**impossible** static → **RUNTIME**
95+
96+
A GC probe is a **runtime witness**, and it lives on the runtime side of the
97+
line the detectability matrix draws. The matrix exists specifically to stop
98+
runtime-shaped problems from being hung on a static checker
99+
([`Plan.md`](../../Plan.md) §1: it "*forbids*" exactly that, killing a class of
100+
false positives in advance). Making GC behaviour cheaper to *observe* is not an
101+
argument for making it *inferred*, and this note must not be cited as one.
102+
103+
The one thing genuinely on the static side is unchanged and already ours: the
104+
`ArrayPool`/`Span` misuse family (`POOL001`–`003`, P-007). Rent/return balance
105+
is structurally visible; fragmentation is not.
106+
107+
## Status
108+
109+
**Recorded, not scheduled.** Per the `research-landscape-2026.md` discipline,
110+
notes record and the ROADMAP schedules. Concretely, if anything is ever picked
111+
up from here:
112+
113+
1. **The LOH threshold correction** — free, and the only item with a
114+
correctness argument behind it. Applies wherever a number gets quoted.
115+
2. **A GC-counter probe as a P-034 sibling** — small, attaches to the open
116+
question P-034 already asks (a new `Own.Diagnostics` package vs living in
117+
OwnAudit's `runtime/`). Do not file it separately; it is the same decision.
118+
3. **Nothing else.** The generation-promotion and allocation-pressure
119+
experiments are educational rather than diagnostic, and we do not need to
120+
re-derive published GC behaviour to ship a checker.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Runtime.CompilerServices;
6+
7+
// Question: a static rule can find "loop-invariant query evaluated inside a
8+
// loop". Can it tell you how much it COSTS? Every case below is the SAME
9+
// syntactic shape -- an invariant call in a loop body -- so a static rule sees
10+
// them as identical. Measure the actual penalty.
11+
static class Program
12+
{
13+
static long sink;
14+
15+
// ---- shape A: Any() over a List, predicate over a captured local ----
16+
[MethodImpl(MethodImplOptions.NoInlining)]
17+
static long A_Inline(List<int> data, int threshold, int iters)
18+
{
19+
long acc = 0;
20+
for (int i = 0; i < iters; i++)
21+
if (data.Any(x => x > threshold)) acc += i; // loop-invariant
22+
return acc;
23+
}
24+
25+
[MethodImpl(MethodImplOptions.NoInlining)]
26+
static long A_Hoisted(List<int> data, int threshold, int iters)
27+
{
28+
long acc = 0;
29+
bool inv = data.Any(x => x > threshold); // hoisted by hand
30+
for (int i = 0; i < iters; i++) if (inv) acc += i;
31+
return acc;
32+
}
33+
34+
// ---- shape B: Count() on a Select-wrapped sequence (O(n) here; Count()
35+
// is O(1) when the source implements ICollection<T>) vs .Count property ----
36+
[MethodImpl(MethodImplOptions.NoInlining)]
37+
static long B_Inline(IEnumerable<int> data, int iters)
38+
{
39+
long acc = 0;
40+
for (int i = 0; i < iters; i++) acc += data.Count();
41+
return acc;
42+
}
43+
44+
[MethodImpl(MethodImplOptions.NoInlining)]
45+
static long B_Hoisted(IEnumerable<int> data, int iters)
46+
{
47+
long acc = 0; int c = data.Count();
48+
for (int i = 0; i < iters; i++) acc += c;
49+
return acc;
50+
}
51+
52+
// ---- shape C: OrderBy().First() -- repeated linear key scanning every
53+
// iteration (.NET 9 takes a specialized TryGetFirst path rather than
54+
// materializing and sorting a buffer) ----
55+
[MethodImpl(MethodImplOptions.NoInlining)]
56+
static long C_Inline(List<int> data, int iters)
57+
{
58+
long acc = 0;
59+
for (int i = 0; i < iters; i++) acc += data.OrderBy(x => x).First();
60+
return acc;
61+
}
62+
63+
[MethodImpl(MethodImplOptions.NoInlining)]
64+
static long C_Hoisted(List<int> data, int iters)
65+
{
66+
long acc = 0; int v = data.OrderBy(x => x).First();
67+
for (int i = 0; i < iters; i++) acc += v;
68+
return acc;
69+
}
70+
71+
// ---- shape D: a trivially cheap invariant -- the control ----
72+
[MethodImpl(MethodImplOptions.NoInlining)]
73+
static long D_Inline(List<int> data, int iters)
74+
{
75+
long acc = 0;
76+
for (int i = 0; i < iters; i++) acc += data.Count; // O(1) property
77+
return acc;
78+
}
79+
80+
[MethodImpl(MethodImplOptions.NoInlining)]
81+
static long D_Hoisted(List<int> data, int iters)
82+
{
83+
long acc = 0; int c = data.Count;
84+
for (int i = 0; i < iters; i++) acc += c;
85+
return acc;
86+
}
87+
88+
static double Bench(Func<long> f, int reps)
89+
{
90+
for (int i = 0; i < 20; i++) sink += f(); // warm
91+
var sw = Stopwatch.StartNew();
92+
for (int i = 0; i < reps; i++) sink += f();
93+
sw.Stop();
94+
return sw.Elapsed.TotalMilliseconds / reps;
95+
}
96+
97+
static void Row(string shape, int n, Func<long> inline, Func<long> hoisted, int reps)
98+
{
99+
// Correctness gate FIRST, and it must fail the run: timing two
100+
// non-equivalent implementations produces a meaningless ratio, and a
101+
// printed warning would still exit 0 and look like a good measurement.
102+
long a = inline(), b = hoisted();
103+
if (a != b)
104+
throw new InvalidOperationException($"{shape} n={n}: MISMATCH {a} vs {b}");
105+
106+
double ti = Bench(inline, reps), th = Bench(hoisted, reps);
107+
Console.WriteLine($"{shape,-38} n={n,-7} inline={ti,9:F4} ms hoisted={th,9:F4} ms penalty = {ti / th,8:F1}x");
108+
}
109+
110+
static void Main()
111+
{
112+
const int Iters = 1000;
113+
Console.WriteLine("Same syntactic shape everywhere: a loop-invariant call in a loop body.");
114+
Console.WriteLine($"Inner loop = {Iters} iterations. 'penalty' = how much the un-hoisted version costs.\n");
115+
116+
foreach (int n in new[] { 4, 64, 4096, 100_000 })
117+
{
118+
var list = Enumerable.Range(0, n).ToList();
119+
IEnumerable<int> seq = list.Select(x => x); // hides ICollection fast path
120+
int th = -1; // Any() hits on the FIRST element
121+
122+
Row("A: Any(x => x > t) [hit@0]", n, () => A_Inline(list, th, Iters), () => A_Hoisted(list, th, Iters), 20);
123+
Row("A: Any(x => x > t) [miss]", n, () => A_Inline(list, int.MaxValue, Iters), () => A_Hoisted(list, int.MaxValue, Iters), 5);
124+
Row("B: Count() on Select-wrapped IEnumerable", n, () => B_Inline(seq, Iters), () => B_Hoisted(seq, Iters), 5);
125+
Row("C: OrderBy().First()", n, () => C_Inline(list, Iters), () => C_Hoisted(list, Iters), n > 10000 ? 1 : 3);
126+
Row("D: .Count property (cheap)", n, () => D_Inline(list, Iters), () => D_Hoisted(list, Iters), 50);
127+
Console.WriteLine();
128+
}
129+
Console.WriteLine($"(sink={sink})");
130+
}
131+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>disable</Nullable>
6+
<Optimize>true</Optimize>
7+
</PropertyGroup>
8+
</Project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
# Reproduces the table in ../invariant-cost-static-vs-runtime.md.
3+
# Requires a .NET 9 SDK. The published numbers were taken on 9.0.316; any 9.0.x
4+
# should reproduce the ORDER and the spread, but exact multipliers will differ
5+
# by machine and patch level -- the selected SDK/runtime is printed below so a
6+
# rerun is self-describing rather than merely claiming reproduction.
7+
set -euo pipefail
8+
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9+
10+
# Only clean up a workdir we created ourselves; never delete a caller's.
11+
if [[ $# -ge 1 ]]; then
12+
WORK="$1"
13+
else
14+
WORK="$(mktemp -d)"
15+
trap 'rm -rf -- "$WORK"' EXIT
16+
fi
17+
18+
mkdir -p "$WORK"; cp "$HERE"/{Program.cs,linq.csproj} "$WORK/"
19+
cd "$WORK"
20+
21+
echo "== toolchain actually used =="
22+
dotnet --version
23+
dotnet --list-runtimes | grep -E '^Microsoft\.NETCore\.App 9\.0\.' || true
24+
echo
25+
26+
dotnet build -c Release -v q --nologo
27+
# Tiering disabled: every method is compiled straight to FullOpts, so the
28+
# measurement is steady-state code. (This is non-tiered mode -- "tier 1" is a
29+
# tiering concept and does not apply when tiering is off.)
30+
DOTNET_TieredCompilation=0 dotnet bin/Release/net9.0/linq.dll

0 commit comments

Comments
 (0)