Escape Analysis: Elide an array allocation in BitConverter.GetBytes pattern

Open
#113,236 12 comments 4 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

Assessment

This issue has not been assessed yet.

Description

area-CodeGen-coreclr

JIT should be able to eliminate GC allocation in this snippet:

void Test(int value, Span<byte> dest) => 
    BitConverter.GetBytes(value).AsSpan(0, 3).CopyTo(dest);

This pattern could be used as a safe alternative to "write N-bytes of a primitive to a span of bytes". Alternative zero-alloc patterns don't look pretty/involve unsafe

Sub-tasks:

cc @AndyAyersMS

Dominant language
C#
Stars
18.3k
Forks
5.6k
Avg merge
2d 19h
Merged PRs (30d)
589

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.