Skip to content

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

@EgorBo

Description

@EgorBo

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

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions