Skip to content

Can we improve ROS<byte>.ToArray from RVA static? #68103

@stephentoub

Description

@stephentoub

When doing new byte[] { const, const, const, ... }, the JIT generates code that calls the new array helper and then emits an unrolled loop to copy the data into the array. When doing ((ReadOnlySpan<byte>)new byte[] { const, const, const, ... }).ToArray(), the copy is handled via ToArray's call to Buffer.MemMove. Is it feasible to do better?

using System;
public class C
{
    public byte[] Implicit() => new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
    public byte[] Explicit() => ((ReadOnlySpan<byte>)new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }).ToArray();
}

SharpLab

; Core CLR 6.0.222.6406 on amd64

C.Implicit()
    L0000: sub rsp, 0x28
    L0004: vzeroupper
    L0007: mov rcx, 0x7ffa6ca70a90
    L0011: mov edx, 0x10
    L0016: call 0x00007ffacc4fb1b0
    L001b: mov rdx, 0x221bb0a0894
    L0025: vmovupd xmm0, [rdx]
    L0029: vmovupd [rax+0x10], xmm0
    L002e: add rsp, 0x28
    L0032: ret

C.Explicit()
    L0000: push rsi
    L0001: sub rsp, 0x20
    L0005: mov edx, 0x10
    L000a: mov rcx, 0x7ffa6ca70a90
    L0014: call 0x00007ffacc4fb1b0
    L0019: mov rsi, rax
    L001c: lea rcx, [rsi+0x10]
    L0020: mov rdx, 0x221bb0a0894
    L002a: mov r8d, 0x10
    L0030: call 0x00007ffa6c97e808
    L0035: mov rax, rsi
    L0038: add rsp, 0x20
    L003c: pop rsi
    L003d: ret

category:cq
theme:optimization
skill-level:beginner
cost:small
impact:small

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions