Some simple methods fail to inline

Open
#119,510 3 comments 0 reactions 1 assignee View on GitHub

@EgorBo is already working on this.

Since Apr 17, 2026.

Assessment

This issue has not been assessed yet.

Description

area-CodeGen-coreclr JIT-Inlining

I’ve noticed that some very simple generic methods fail to inline, even though they seem like good candidates. In the example below, SizeOf<T> and AlignedSizeOf<T> are not inlined by the JIT.

While the SizeOf<T> example could potentially be rewritten with a throw helper, it seems like the JIT should be able to do better here without requiring such workarounds.

static uint AlignedSizeOfByte() => AlignedSizeOf<byte>();

static uint AlignedSizeOf<T>() where T : struct
{
    uint size = SizeOf<T>();
    if (size == 1 || size == 2)
    {
        return size;
    }

    return (uint)((size + 3) & (~3));
}

static unsafe uint SizeOf<T>() where T : struct
{
    if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
        throw new ArgumentException("Argument_NeedStructWithNoRefs");

    return (uint)sizeof(T);
}

godbolt.org

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.