dotnet/runtime Code Review
Review code changes against conventions and patterns established by dotnet/runtime maintainers. These rules were extracted from 43,000+ maintainer review comments across 6,600+ PRs and represent the actual standards enforced in practice.
Reviewer mindset: Be polite but very skeptical. Your job is to help speed the review process for maintainers, which includes not only finding problems the PR author may have missed but also questioning the value of the PR in its entirety. Treat the PR description and linked issues as claims to verify, not facts to accept. Question the stated direction, probe edge cases, and don't hesitate to flag concerns even when unsure.
When to Use This Skill
Use this skill when:
- Reviewing a PR or code change in dotnet/runtime
- Checking code for correctness, performance, style, or consistency issues before submitting a PR
- Asked to review, critique, or provide feedback on code changes
- Validating that a change follows dotnet/runtime conventions
Review Process
Step 0: Gather Code Context (No PR Narrative Yet)
Before analyzing anything, collect as much relevant code context as you can. Critically, do NOT read the PR description, linked issues, or existing review comments yet. You must form your own independent assessment of what the code does, why it might be needed, what problems it has, and whether the approach is sound — before being exposed to the author's framing. Reading the author's narrative first anchors your judgment and makes you less likely to find real problems.
- Diff and file list: Fetch the full diff and the list of changed files.
- Full source files: For every changed file, read the entire source file (not just the diff hunks). You need the surrounding code to understand invariants, locking protocols, call patterns, and data flow. Diff-only review is the #1 cause of false positives and missed issues.
- Consumers and callers: If the change modifies a public/internal API, a type that others depend on, or a virtual/interface method, search for how consumers use the functionality. Grep for callers, usages, and test sites. Understanding how the code is consumed reveals whether the change could break existing behavior or violate caller assumptions.
- Sibling types and related code: If the change fixes a bug or adds a pattern in one type, check whether sibling types (e.g., other abstraction implementations, other collection types, platform-specific variants) have the same issue or need the same fix. Fetch and read those files too.
- Key utility/helper files: If the diff calls into shared utilities, read those to understand the contracts (thread-safety, idempotency, etc.).
- Git history: Check recent commits to the changed files (
git log --oneline -20 -- <file>). Look for related recent changes, reverts, or prior attempts to fix the same problem. This reveals whether the area is actively churning, whether a similar fix was tried and reverted, or whether the current change conflicts with recent work.
Step 1: Form an Independent Assessment
Based only on the code context gathered above (without the PR description or issue), answer these questions:
- What does this change actually do? Describe the behavioral change in your own words by reading the diff and surrounding code. What was the old behavior? What is the new behavior?
- Why might this change be needed? Infer the motivation from the code itself. What bug, gap, or improvement does it appear to address?
- Is this the right approach? Would a simpler alternative be more consistent with the codebase? Could the goal be achieved with existing functionality? Are there correctness, performance, or safety concerns?
- What problems do you see? Identify bugs, edge cases, missing validation, thread-safety issues, performance regressions, API design problems, test gaps, and anything else that concerns you.
Write down your independent assessment before proceeding. You must produce a holistic assessment (see Holistic PR Assessment) at this stage.
Step 2: Incorporate PR Narrative and Reconcile
Now read the PR description, labels, linked issues (in full), author information, existing review comments, and any related open issues in the same area. Treat all of this as claims to verify, not facts to accept.
- PR metadata: Fetch the PR description, labels, linked issues, and author. Read linked issues in full — they often contain the repro, root cause analysis, and constraints the fix must satisfy.
- Related issues: Search for other open issues in the same area (same labels, same component). This can reveal known problems the PR should also address, or constraints the author may not be aware of.
- Existing review comments: Check if there are already review comments on the PR to avoid duplicating feedback.
- Reconcile your assessment with the author's claims. Where your independent reading of the code disagrees with the PR description or issue, investigate further — but do not simply defer to the author's framing. If the PR claims a bug fix, a performance improvement, or a behavioral correction, verify those claims against the code and any provided evidence. If your independent assessment found problems the PR narrative doesn't acknowledge, those problems are more likely to be real, not less.
- Update your holistic assessment if the additional context reveals information that genuinely changes your evaluation (e.g., a linked issue proves the bug is real, or an existing review comment already identified the same concern). But do not soften findings just because the PR description sounds reasonable.
Step 3: Detailed Analysis
- Focus on what matters. Prioritize bugs, performance regressions, safety issues, race conditions, resource management problems, incorrect assumptions about data or state, and API design problems. Do not comment on trivial style issues unless they violate an explicit rule below.
- Consider collateral damage. For every changed code path, actively brainstorm: what other scenarios, callers, or inputs flow through this code? Could any of them break or behave differently after this change? If you identify any plausible risk — even one you can't fully confirm — surface it so the author can evaluate. Do not dismiss behavioral changes because you believe the fix justifies them. The tradeoff is the author's decision — your job is to make it visible.
- Be specific and actionable. Every comment should tell the author exactly what to change and why. Reference the relevant convention. Include evidence of how you verified the issue is real, e.g., "looked at all callers and none of them validate this parameter".
- Flag severity clearly:
- ❌ error — Must fix before merge. Bugs, security issues, API violations, test gaps for behavior changes.
- ⚠️ warning — Should fix. Performance issues, missing validation, inconsistency with established patterns.
- 💡 suggestion — Consider changing. Style improvements, minor readability wins, optional optimizations.
- Don't pile on. If the same issue appears many times, flag it once on the primary file with a note listing all affected files. Do not leave separate comments for each occurrence.
- Respect existing style. When modifying existing files, the file's current style takes precedence over general guidelines.
- Don't flag what CI catches. Do not flag issues that a linter, typechecker, compiler, analyzer, or CI build step would catch, e.g., missing usings, unsupported syntax, formatting. Assume CI will run separately.
- Avoid false positives. Before flagging any issue:
- Verify the concern actually applies given the full context, not just the diff. Open the surrounding code to check. Confirm the issue isn't already handled by a caller, callee, or wrapper layer before claiming something is missing.
- Skip theoretical concerns with negligible real-world probability. "Could happen" is not the same as "will happen."
- If you're unsure, either investigate further until you're confident, or surface it explicitly as a low-confidence question rather than a firm claim. Do not speculate about issues you have no concrete basis for. Every comment should be worth the reader's time.
- Trust the author's context. The author knows their codebase. If a pattern seems odd but is consistent with the repo, assume it's intentional.
- Never assert that something "does not exist," "is deprecated," or "is unavailable" based on training data alone. Your knowledge has a cutoff date. When uncertain, ask rather than assert.
- Ensure code suggestions are valid. Any code you suggest must be syntactically correct and complete. Ensure any suggestion would result in working code.
- Label in-scope vs. follow-up. Distinguish between issues the PR should fix and out-of-scope improvements. Be explicit when a suggestion is a follow-up rather than a blocker.
Multi-Model Review
When the environment supports launching sub-agents with different models (e.g., the task tool with a model parameter), run the review in parallel across multiple model families to get diverse perspectives. Different models catch different classes of issues. If the environment does not support this, proceed with a single-model review.
How to execute (when supported):
- Inspect the available model list and select one model from each distinct model family (e.g., one Anthropic Claude, one Google Gemini, one OpenAI GPT). Use at least 2 and at most 4 models. Model selection rules:
- Pick only from models explicitly listed as available in the environment. Do not guess or assume model names.
- From each family, pick the model with the highest capability tier (prefer "premium" or "standard" over "fast/cheap").
- Never pick models labeled "mini", "fast", or "cheap" for code review.
- If multiple standard-tier models exist in the same family (e.g.,
gpt-5andgpt-5.1), pick the one with the highest version number. - Do not select the same model that is already running the primary review (i.e., your own model). The goal is diverse perspectives from different model families.
- Launch a sub-agent for each selected model in parallel, giving each the same review prompt: the PR diff, the review rules from this skill, and instructions to produce findings in the severity format defined above.
- Wait for all agents to complete, then synthesize: deduplicate findings that appear across models, elevate issues flagged by multiple models (higher confidence), and include unique findings from individual models that meet the confidence bar. Timeout handling: If a sub-agent has not completed after 10 minutes and you have results from other agents, proceed with the results you have. Do not block the review indefinitely waiting for a single slow model. Note in the output which models contributed.
- Present a single unified review to the user, noting when an issue was flagged by multiple models.
Review Output Format
When presenting the final review (whether as a PR comment or as output to the user), use the following structure. This ensures consistency across reviews and makes the output easy to scan.
Structure
## 🤖 Copilot Code Review — PR #<number>
### Holistic Assessment
**Motivation**: <1-2 sentences on whether the PR is justified and the problem is real>
**Approach**: <1-2 sentences on whether the fix/change takes the right approach>
**Summary**: <✅ LGTM / ⚠️ Needs Human Review / ⚠️ Needs Changes / ❌ Reject>. <2-3 sentence summary of the overall verdict and key points. If "Needs Human Review," explicitly state which findings you are uncertain about and what a human reviewer should focus on.>
---
### Detailed Findings
#### ✅/⚠️/❌ <Category Name> — <Brief description>
<Explanation with specifics. Reference code, line numbers, interleavings, etc.>
(Repeat for each finding category. Group related findings under a single heading.)
Guidelines
- Holistic Assessment comes first and covers Motivation, Approach, and Summary.
- Detailed Findings uses emoji-prefixed category headers:
- ✅ for things that are correct / look good (use to confirm important aspects were verified)
- ⚠️ for warnings or impactful suggestions (should fix, or follow-up)
- ❌ for errors (must fix before merge)
- 💡 for minor suggestions or observations (nice-to-have)
- Cross-cutting analysis should be included when relevant: check whether related code (sibling types, callers, other platforms) is affected by the same issue or needs a similar fix.
- Test quality should be assessed as its own finding when tests are part of the PR.
- Summary gives a clear verdict: LGTM (no blocking issues — use only when confident), Needs Human Review (code may be correct but you have unresolved concerns or uncertainty that require human judgment), Needs Changes (with blocking issues listed), or Reject (explaining why this should be closed outright). Never give a blanket LGTM when you are unsure. When in doubt, use "Needs Human Review" and explain what a human should focus on.
- Keep the review concise but thorough. Every claim should be backed by evidence from the code.
Verdict Consistency Rules
The summary verdict must be consistent with the findings in the body. Follow these rules:
The verdict must reflect your most severe finding. If you have any ⚠️ findings, the verdict cannot be "LGTM." Use "Needs Human Review" or "Needs Changes" instead. Only use "LGTM" when all findings are ✅ or 💡 and you are confident the change is correct and complete.
When uncertain, always escalate to human review. If you are unsure whether a concern is valid, whether the approach is sufficient, or whether you have enough context to judge, the verdict must be "Needs Human Review" — not LGTM. Your job is to surface concerns for human judgment, not to give approval when uncertain. A false LGTM is far worse than an unnecessary escalation.
Separate code correctness from approach completeness. A change can be correct code that is an incomplete approach. If you believe the code is right for what it does but the approach is insufficient (e.g., treats symptoms without investigating root cause, silently masks errors that should be diagnosed, fixes one instance but not others), the verdict must reflect the gap — do not let "the code itself looks fine" collapse into LGTM.
Classify each ⚠️ and ❌ finding as merge-blocking or advisory. Before writing your summary, decide for each finding: "Would I be comfortable if this merged as-is?" If any answer is "no," the verdict must be "Needs Changes." If any answer is "I'm not sure," the verdict must be "Needs Human Review."
Devil's advocate check before finalizing. Re-read all your ⚠️ findings. For each one, ask: does this represent an unresolved concern about the approach, scope, or risk of masking deeper issues? If so, the verdict must reflect that tension. Do not default to optimism because the diff is small or the code is obviously correct at a syntactic level.
Holistic PR Assessment
Before reviewing individual lines of code, evaluate the PR as a whole. Consider whether the change is justified, whether it takes the right approach, and whether it will be a net positive for the codebase.
Motivation & Justification
Every PR must articulate what problem it solves and why. Don't accept vague or absent motivation. Ask "What's the rationale?" and block progress until the contributor provides a clear answer.
"I am not sure why is this needed. ... It's not immediately obvious whether this happens only for the bridge comparison tests or whether it can happen for real-life scenarios too."
Challenge every addition with "Do we need this?" New code, APIs, abstractions, and flags must justify their existence. If an addition can be avoided without sacrificing correctness or meaningful capability, it should be.
"I don't think we should take this change, at all. A change which makes the VS runner see the same assets as the CLI runner, sure. But random extra hacking on the side, no."
Demand real-world use cases and customer scenarios. Hypothetical benefits are insufficient motivation for expanding API surface area or adding features. Require evidence that real users need this.
"It is not clear to me whether you can hit a real-world scenario on 32-bit platforms where it makes a difference."
Evidence & Data
Require measurable performance data before accepting optimization PRs. Demand BenchmarkDotNet results or equivalent proof — never accept performance claims at face value.
"Can you please share a benchmark using BenchmarkDotNet against public System.Text.Json APIs that demonstrates the improvement?"
Distinguish real performance wins from micro-benchmark noise. Trivial benchmarks with predictable inputs overstate gains from jump tables, branch elimination, and similar tricks. Require evidence from realistic, varied inputs.
"Try to benchmark it with an input that varies randomly. Jump tables are great for trivial micro-benchmarks, but they are less great for real world code."
Investigate and explain regressions before merging. Even if a PR shows a net improvement, regressions in specific scenarios must be understood and explicitly addressed — not hand-waved.
"Could you please inspect the regressions on why exactly it's an improvement there?"
Approach & Alternatives
Check whether the PR solves the right problem at the right layer. Look for whether it addresses root cause or applies a band-aid. Prefer fixing the actual source of an issue over adding workarounds to production code.
"The offset behind
Flags.IndexMaskshould always be correct. Instead of checking that the index is in range in all its usages, we should fix the root cause where the offset wasn't computed/updated correctly."When a PR takes a fundamentally wrong approach, redirect early. Don't iterate on implementation details of a flawed design. Push back on the overall direction before the contributor invests more time.
"I'm still hesitating whether separating FEATURE_HW_INTRINSICS from SIMD and MASKED_HW_INTRINSICS is the right approach ... An alternative would be to handle them like #113689 and fix the value numbering."
Ask "Why not just X?" — always prefer the simplest solution. When a PR uses a complex approach, challenge it with the simplest alternative that could work. The burden of proof is on the complex solution.
"Wouldn't it be simpler to just do a regular mono stackwalk when we need to record and raise a sample?"
Cost-Benefit & Complexity
Explicitly weigh whether the change is a net positive. A performance trade-off that shifts costs around is not automatically beneficial. Demand clarity that the change is a win in the typical configuration, not just in a narrow scenario.
"It is a performance trade-off. You will shift the costs around. It is not clear to me whether it would be a win at the end in the typical configuration."
Reject overengineering — complexity is a first-class cost. Unnecessary abstraction, extra indirections, and elaborate solutions for marginal gains are actively rejected.
"This optimization smells funny. It seems overly complicated for little win. Is this path hot? Can we instead store the home directory?"
Every addition creates a maintenance obligation. Long-term maintenance cost outweighs short-term convenience. Code that is hard to maintain, increases surface area, or creates technical debt needs stronger justification.
"The primary goal of this project is to minimize our long-term maintenance costs. Building multiple optimizing code generators would go against that goal."
Scope & Focus
Require large or mixed PRs to be split into focused changes. Each PR should address one concern. Mixed concerns make review harder and increase regression risk.
"I think I'm going to break this into two pieces, even though that's more work."
Defer tangential improvements to follow-up PRs. Police scope creep by asking contributors to separate concerns. Even good ideas should wait if they're not part of the PR's core purpose.
"Should probably be a separate PR."
Risk & Compatibility
Flag breaking changes and require formal process. Any behavioral change that could affect downstream consumers needs documentation, API review, and explicit approval — even when the change improves the codebase internally.
"Introduce the new API in this PR. Remove the old check in another PR, mark it as breaking change and document it (as any other breaking change)."
Assess regression risk proportional to the change's blast radius. High-risk changes to stable code need proportionally higher value and more thorough validation.
"I wanted to backport this change to .NET 10, potentially .NET 9, and wouldn't want to introduce any risky changes."
Codebase Fit & History
Ensure new code matches existing patterns and conventions. Deviations from established patterns create confusion and inconsistency. If a rename or restructuring is warranted, do it uniformly in a dedicated PR — not piecemeal.
"This change is inconsistent with the rest of the global pointers. If we want to consider renaming these, I think we should do it in a separate PR and apply it consistently to all global pointers."
Check whether a similar approach has been tried and rejected before. If a prior attempt didn't work, require a clear explanation of what's different this time.
"If it's not worthwhile, especially if it was previously tried and it wasn't obviously beneficial, then we should close the issue."
Correctness & Safety
Error Handling & Assertions
Use
Debug.Assertfor internal invariants, not exceptions. For internal-only callers, assert assumptions rather than throwingArgumentException. PreferDebug.Assert(value != null)over the null-forgiving operator (!)."Since there are no public callers, this should be an assert, not an ArgumentException." — bartonjs
Use
throwfor reachable error paths,UnreachableExceptionfor exhaustive switches. When a code path might be hit at runtime, throw an exception rather than asserting. Usethrow new UnreachableException()for default cases in exhaustive switches. UsePlatformNotSupportedException(notNotSupportedException) for platform gaps. In native code, use_ASSERTE(!"message")."We prefer throw rather than asserts so it is more apparent if some scenario makes it here." — VSadov
Include actionable details in exception messages. Use
nameoffor parameter names. Include the unsupported type or unexpected value. Never throw empty exceptions."You should add some message here:
throw new ArgumentException($\"Unknown ArrayFunctionType: {functionType}.\", nameof(method));" — jkoritzinskyInitialize output parameters in all code paths. When a method has
outparameters or pointer outputs (bytesWritten,numLocals), ensure they are initialized to a defined value in all error paths."Clear numLocals here (or at start of the method) so that it is initialized in all error cases?" — jkotas
Handle OOM with exceptions or fail-fast, never asserts. Use
ThrowOutOfMemoryorEEPOLICY_HANDLE_FATAL_ERROR, not asserts. In interpreter loops, usenothrow newand check for null."OutOfMemory handling should not be done by asserts. It should throw exception; or if exception is not viable, fail with fail fast." — jkotas
Use
ThrowIfhelpers over manual checks. UseArgumentOutOfRangeException.ThrowIfNegative,ObjectDisposedException.ThrowIf, etc. instead of manual if-then-throw patterns."This if condition should not be necessary, as it's going to be checked by ThrowIfNegative." — stephentoub
Challenge exception swallowing that masks unexpected errors. When a PR adds try/catch blocks that silently discard exceptions (
catch { continue; },catch { return null; }), question whether the exception represents a truly expected, recoverable condition or an unexpected error signaling a deeper problem (race conditions, memory corruption, build environment issues). Silently catching exceptions that "shouldn't happen" hides root causes and makes debugging harder. The default disposition should be to let unexpected exceptions propagate or fail fast so the real issue gets investigated."Why do we want to mask this error? ... Our general strategy in AOT compilers is to fail the compilation when the input is malformed. It is expected that the malformed input can and will cause the compiler to crash or fail." — jkotas
Thread Safety
Use
VolatileorInterlockedfor cross-thread field access. Fields written on one thread and read on another must useVolatile<T>,Volatile.Read/Write, orInterlocked. The??=operator is not thread-safe.Nullable<T>is not safe for caching (two-field struct tears). Do not use shared mutable arrays without synchronization."field ??= is not thread-safe." — jkotas; "Nullable<int> is a struct with two fields. This pattern has a race condition caused by tearing." — jkotas
Use
TickCount64for timeout calculations. UseEnvironment.TickCount64(long) instead ofEnvironment.TickCount(int) to avoid integer overflow."Should this use long and
Environment.TickCount64to avoid integer overflow issues?" — jkotas
Security
Guard integer arithmetic against overflow. Guard size computations involving multiplication (e.g.,
newCapacity * sizeof(T)) against integer overflow. Use patterns correct by construction."We have a lot of scars from integer overflow security bugs... This change is switching from code that follows best practices to a potentially vulnerable pattern." — jkotas
Clean sensitive cryptographic data after use. Always clear key material with
CryptographicOperations.ZeroMemory. When usingPinAndClearbut copying to another buffer, clear the original too. Use non-short-circuit operators (|) in verification code to prevent timing leaks."This is using PinAndClear to avoid the GC making a copy... but it's itself making a copy and not clearing the original." — bartonjs
Don't proactively send credentials without opt-in. Never send authentication credentials (especially Basic auth) before receiving a challenge.
"This is problematic and we will have difficulty with security group as it especially with basic AUTH leaks the credentials." — wfurt
Limit
stackallocto ~1KB and validate size. Don't stackalloc based on user-controlled or large input sizes. Move stackalloc to just before usage, not before early returns."We typically limit stackallocs to ~1K." — stephentoub
Correctness Patterns
Fix root cause, not symptoms or workarounds. Investigate and fix the root cause rather than adding workarounds or suppressing warnings. Revert broken commits before layering fixes.
"Let's try to investigate the root cause instead of taking this fix as-is since there could be other issues/AVs related to the mangling of the list." — jkotas
Prefer safe code over unsafe micro-optimizations. Do not introduce
Unsafe.As,Unsafe.AsRef, or raw pointers without demonstrable performance need. Prefer Span-based APIs. If performance is the issue, prefer fixing the JIT."I do not want to introduce unsafe code for things like this. If it's material, the cast should be elided by the JIT." — stephentoub
Use
Unsafe.BitCastfor same-size type punning. PreferUnsafe.BitCast<TFrom, TTo>overUnsafe.As<TFrom, TTo>for type punning between value types of the same size."Unsafe.BitCast is more correct (avoids undeclared misaligned access) and less dangerous than Unsafe.As here." — jkotas
Delete dead code and unnecessary wrappers. Remove dead code, unnecessary wrappers, obsolete fields, and unused variables when encountered or when the only caller changes.
"Unnecessary wrapper", "Dead code that I happen to notice", "This is the only use of m_canBeRuntimeImpl. It can be deleted." — jkotas
Handle
SafeHandle.IsInvalidbeforeDispose. CheckIsInvalid(not null) on returned SafeHandles. Get the exception before callingDispose, since Dispose might clear the error state."
if (handle.IsInvalid) { Exception ex = Interop.Crypto.CreateOpenSslCryptographicException(); handle.Dispose(); throw ex; }" — vcsjonesSeal classes when
Equalsuses exact type matching. If a class implementsEqualswithGetType()comparison, seal the class to prevent subtle inheritance bugs."Is there a reason why this Equals implementation is exact type match only even though ContextHolder isn't sealed? I'd prefer to block this class of failure by sealing the class." — kg
Use
Environment.ProcessPathandAppContext.BaseDirectory. Use these instead ofProcess.GetCurrentProcess().MainModule?.FileNameandAssembly.Locationfor NativeAOT/single-file compatibility."Process.GetCurrentProcess().MainModule?.FileName should be same as Environment.ProcessPath." — jkotas
File name casing must match csproj references exactly. Linux is case-sensitive. New source files must be listed in the
.csprojif other files in that folder are explicitly listed."The build is failing because Linux is a case-sensitive file system and your csproj file and the file name differ by case." — vcsjones
Prefer correct-by-construction designs. Prefer designs that are correct by construction (e.g., scanning IL) over manually maintained parallel data structures. A missed optimization is better than silent bad codegen.
"I'd go with the correct-by-construction approach if it's an option." — MichalStrehovsky
Allocate on the correct loader allocator for collectibility. When allocating runtime data structures for generic instantiations, use the correct loader allocator accounting for collectibility of type arguments.
"Consider MethodInNonCollectibleAssembly<CollectibleType>(): This method instantiation should be allocated on collectible loader allocator. As written, it will have a memory leak." — jkotas
Backport targeted fixes, not refactorings. When backporting to servicing branches, create small targeted fixes. Backporting large refactorings introduces unnecessary risk.
"If we need the fix in .NET 10 for Android, we should do a small targeted fix that just adds the few lines under Android ifdef." — jkotas
JIT-Specific Correctness
JIT lowering must not double-lower nodes. Never call
LowerNodeon an already-lowered node. Return newly created nodes for the caller to lower. Constant folding belongs in import/morph, not lowering."Lower is not supposed to be called twice on the same node generally." — EgorBo
Mark collectible ALC test methods
NoInlining. Methods that touch collectible assembly load contexts must be[MethodImpl(MethodImplOptions.NoInlining)]to prevent the JIT from keeping references alive."This needs to be marked as no-inlining too. It is valid for JIT to inline this method and leave a reference to collectible assembly load context in a local." — jkotas
Performance & Allocations
Measurement & Evidence
Performance changes require benchmark evidence. Include BenchmarkDotNet or EgorBot numbers before merging. Validate with real-world scenarios, not just microbenchmarks.
"Performance related changes without numbers have high probability of being performance regressions in practice." — jkotas
Justify binary size increases with real-world measurements. Changes that increase binary size require measured wall-clock improvements on real-world apps, not just instruction counts.
"I would like to see number for a Blazor app (total size / bytes saved by this change)." — jkotas
Avoid premature optimization with object pools and caches. Do not introduce global caches or object pools without evidence they are needed. Prefer making the underlying operation faster.
"This pool looks like a premature optimization." — jkotas
Allocation Avoidance
Avoid closures and allocations in hot paths. When a lambda captures locals creating a closure, consider using a static delegate with a state parameter (value tuple). Avoid string concatenation; use span-based operations.
"Since this is capturing
dataandcontextin the closure it's allocating a closure for every call. The usual fix is to make the callback take a TState, and just pass through a value-tuple." — bartonjsPre-allocate collections when size is known. Pass capacity to
Dictionary,HashSet,Listconstructors when the expected count is available."We can pre-allocate the dictionary of the right size." — jkotas
Structs in dictionaries need
IEquatable<T>andGetHashCode. Without these, the runtime falls back to boxing allocations for equality comparison."If a struct doesn't override equality comparison logic, the runtime will end up using a fallback that boxes the value." — MihaZupan
Avoid Pinned Object Heap for non-permanent objects. POH is never compacted and effectively gen2. Only use for objects surviving as long as the process.
"We avoid the POH for objects that may have a shorter lifespan, typically only using it for objects that will survive as long as the process does." — stephentoub
Suppress
ExecutionContextflow for infrastructure timers. When allocatingTimeror similar background infrastructure, suppress EC flow to avoid capturing unrelatedAsyncLocals that leak memory."We'll want to suppress the ExecutionContext during the timer allocation to avoid capturing unrelated asynclocals." — MihaZupan
Code Structure for Performance
Place cheap checks before expensive operations. Order conditionals so cheapest/most-common checks come first. Move expensive work after early-exit checks.
"These checks are cached cheap bit tests. We should do them first, and run the more expensive IL header decoder only when the modes do not match." — jkotas
Allocate resources lazily where possible. Allocate expensive resources on first use, not during initialization. Avoid forcing type initialization during startup.
"We try to do things lazily where possible since it is good for performance." — jkotas; "Do not force initialization during runtime startup just to make cDAC work. Startup is our number one perf problem." — jkotas
Extract throw helpers into
[DoesNotReturn]methods. Move throwing logic from error paths into separate static local functions or helper methods to allow the JIT to inline the success path."Please move the body of this
if (throwOnFailure)block to a separate [DoesNotReturn] throwing static local function." — stephentoubAvoid O(n²) patterns in collections and hot paths. Watch for linear scans inside loops, repeated
RemoveAtin loops. UseRemoveAll, single-pass restructuring, or appropriate data structures."Since RemoveParsedValue performs a linear scan, this change makes the setter have a quadratic worst-case complexity." — MihaZupan
Cache repeated accessor calls in locals. Store the result of repeated property/getter calls in a local variable.
"Maybe read it out once into local to reduce number of calls to m_type_data_get_type?" — lateralusX
Separate hot data from rarely-used data in runtime structures. Keep frequently accessed data inline; move rarely-used data (GCInfo, DebugInfo) to separate structures.
"The code header structure is intentionally designed to separate hot and rarely used data." — jkotas
Compute constant data at compile time, not execution time. In interpreter and similar hot paths, pre-compute metadata lookups and type checks during the compilation phase.
"This computation is constant and should be done at compile time." — BrzVlad
Consider scalability, not just throughput. Evaluate whether data structures, caches, and locking strategies will hold up at high cardinality or under concurrent load. Watch for unbounded collection growth, lock contention that worsens with core count, and O(1) assumptions that break at scale.
Specific API Choices
Use
AppContext.TryGetSwitchwith a static readonly property. Cache AppContext switches instatic bool Prop { get; } = AppContext.TryGetSwitch(...)so the JIT can dead-code-eliminate unreachable paths."
private static bool SwitchEnabled { get; } = AppContext.TryGetSwitch(..., out bool enabled) && enabled;This makes it readonly and lets the JIT delete the unreachable code paths." — MihaZupanDo not cache
typeofexpressions in .NET Core.typeof(...)is JITed into a constant; caching it is a de-optimization. Similarly, don't storeArrayPool.Sharedin variables—it breaks devirtualization."Caching typeof(...) is de-optimization in .NET Core. typeof(...) is JITed into a constant." — jkotas
Use
CollectionsMarshalfor large value-type dictionary lookups. UseGetValueRefOrAddDefaultorGetValueRefOrNullRefto avoid copying large structs. UseValueListBuilderon hot paths."You can use CollectionsMarshal.GetValueRefOrAddDefault here... Avoids copy of the large EventMetadata struct." — jkotas
Use
sizeofinstead ofMarshal.SizeOffor blittable structs.sizeofis more correct and significantly faster when no marshalling is involved."It is more correct and a lot faster to use sizeof instead of Marshal.SizeOf." — jkotas
Use the idiomatic
(uint)index >= (uint)lengthbounds check. The JIT recognizes this pattern and optimizes it. Slice spans before iterating to avoid per-element bounds checks."The JIT recognizes the idiomatic pattern and optimizes it down where it is safe already." — tannergooding
Source generators must be properly incremental. Do not store Roslyn symbols (
ISymbol,Compilation) in incremental pipeline steps. Output must be deterministic with Ordinal-sorted lists."Make your generators properly incremental or don't ship them at all, because the alternative is that you'll murder the IDE." — Sergio0694
Avoid LINQ and records in low-level compiler codebases. In CG2/ILC and AOT tools, use direct loops instead of LINQ and readonly structs instead of records. Use concrete types over interfaces in private code.
"I'd avoid
using System.Linqto steer clear of perf traps." — MichalStrehovskyUse
ValueListBuilderfor dynamic array building in BCL. UseValueListBuilder<T>(with pooling) orArrayBuilder<T>. Use stackalloc for small sizes, array pool when too large."ValueListBuilder is the centralized type for building arrays in BCL." — huoyaoyuan
API Design & Contracts
New public APIs require approved proposals before PR submission. All new API surface must go through API review. PRs adding unapproved APIs will be closed. The implementation must match exactly what was approved.
"We do not accept PRs for unapproved APIs." — jkotas
Use
internalfor new APIs pending API review. If the API is needed immediately for implementation, mark itinternaland file a review request separately."This needs to go through API Review for it to be public. You should use internal for now." — jozkee
Parameter names must match between ref and src. Renaming a public API parameter (including case changes) is a breaking change affecting named arguments and late-bound scenarios.
"The approved API calls the second parameter value, not result. The mismatch is also why the build
…(truncated)