Skip to content

Defer field type resolution to fix #1428 perf regression#1430

Merged
leculver merged 1 commit intomicrosoft:mainfrom
leculver:issue_1428
Apr 28, 2026
Merged

Defer field type resolution to fix #1428 perf regression#1430
leculver merged 1 commit intomicrosoft:mainfrom
leculver:issue_1428

Conversation

@leculver
Copy link
Copy Markdown
Contributor

@leculver leculver commented Apr 28, 2026

ClrField.InitData was eagerly resolving the field's type signature even when the caller only requested Name or Attributes. The type-resolution path calls ContainingType.GetConcreteGenericTypeArguments, which calls ClrHeap.GetTypeByName, which performs a module-wide scan and triggers expensive mscordacwks GetMethodTableName DAC calls. This made field-heavy workloads (e.g. enumerating async state machines) ~10x slower than ClrMD 3.1.

Split the lazy load:

Type-resolution state is tracked in a separate _typeInitialized field (0 = not computed, 1 = resolved, 2 = no resolvable type) so the _attributes sentinel no longer doubles as a type-resolution flag, and so deterministically failed resolutions don't retry on every Type access.

No locks or Interlocked are used on the type-resolution path. Reads and writes of int and reference are atomic in .NET, and the result is deterministic for a given field, so racing threads always agree. GetClrType() re-reads _type after _typeInitialized and recomputes if it sees _typeInitialized == 1 with a null _type, which tolerates write-write reordering on weakly-ordered architectures (ARM).

I also made this change adding a field because of the current alignment of ClrField. Adding this int is free since padding is added already (even on 32bit), this just eats the hidden padding. I'd pursue a different fix if that wasn't the case.

Add regression tests verifying that reading Name (or Attributes) before Type still produces the correct concrete generic instantiation.

Fixes #1428.

ClrField.InitData was eagerly resolving the field's type signature even when
the caller only requested Name or Attributes. The type-resolution path calls
ContainingType.GetConcreteGenericTypeArguments, which calls ClrHeap.GetTypeByName,
which performs a module-wide scan and triggers expensive mscordacwks
GetMethodTableName DAC calls. This made field-heavy workloads (e.g. enumerating
async state machines) ~10x slower than ClrMD 3.1.

Split the lazy load:

  - InitData(forName) populates _name and _attributes from metadata only.
  - GetClrType() / ResolveType() resolves _type from the field signature,
    including the ClrGenericType + MethodTable fallback from PR microsoft#1373. Runs
    only when the Type property is actually accessed.

Type-resolution state is tracked in a separate _typeInitialized field
(0 = not computed, 1 = resolved, 2 = no resolvable type) so the _attributes
sentinel no longer doubles as a type-resolution flag, and so deterministically
failed resolutions don't retry on every Type access.

No locks or Interlocked are used on the type-resolution path. Reads and writes
of int and reference are atomic in .NET, and the result is deterministic for
a given field, so racing threads always agree. GetClrType() re-reads _type
after _typeInitialized and recomputes if it sees _typeInitialized == 1 with
a null _type, which tolerates write-write reordering on weakly-ordered
architectures (ARM).

Add regression tests verifying that reading Name (or Attributes) before Type
still produces the correct concrete generic instantiation.
@leculver leculver merged commit d5b1426 into microsoft:main Apr 28, 2026
10 checks passed
@leculver leculver deleted the issue_1428 branch April 28, 2026 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ClrMD 4.0 performance regression: ClrField.InitData triggers expensive GetMethodTableName DAC calls

2 participants