Skip to content

Using the range operator will result in significant performance loss #66

Closed

Description

@HMBSbige

Jit

using System;

public class X
{
    public void A(ref Span<byte> span, int i)
    {
        span = span[i..];
    }

    public void B(ref Span<byte> span, int i)
    {
        span = span.Slice(i);
    }
}
; Core CLR 8.0.23.53103 on x64

X..ctor()
    L0000: ret

X.A(System.Span`1<Byte> ByRef, Int32)
    L0000: sub rsp, 0x28
    L0004: mov rax, rdx
    L0007: mov eax, [rax+8]
    L000a: mov ecx, eax
    L000c: sub ecx, r8d
    L000f: mov r8d, r8d
    L0012: mov r10d, ecx
    L0015: add r10, r8
    L0018: cmp r10, rax
    L001b: ja short L002b
    L001d: add r8, [rdx]
    L0020: mov [rdx], r8
    L0023: mov [rdx+8], ecx
    L0026: add rsp, 0x28
    L002a: ret
    L002b: mov rax, 0x7ffb10a5ead8
    L0035: call qword ptr [rax]
    L0037: int3

X.B(System.Span`1<Byte> ByRef, Int32)
    L0000: sub rsp, 0x28
    L0004: mov eax, [rdx+8]
    L0007: cmp r8d, eax
    L000a: ja short L0020
    L000c: mov ecx, r8d
    L000f: add rcx, [rdx]
    L0012: sub eax, r8d
    L0015: mov [rdx], rcx
    L0018: mov [rdx+8], eax
    L001b: add rsp, 0x28
    L001f: ret
    L0020: mov rax, 0x7ffb10a5ead8
    L002a: call qword ptr [rax]
    L002c: int3

So just ignore IDE0057

Activity

  1. added this to the Performance milestone on Jan 11, 2024
  2. Repository owner locked and limited conversation to collaborators on Oct 22, 2025
  3. converted this issue into a discussion #96 on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions