Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Utf8Array] AsSpanの最適化 #71

Merged
merged 1 commit into from
Nov 3, 2021
Merged

[Utf8Array] AsSpanの最適化 #71

merged 1 commit into from
Nov 3, 2021

Conversation

finphie
Copy link
Owner

@finphie finphie commented Nov 3, 2021

https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKGIAYACY8gOgCUBXAOwwEt8YLAMIR8AB14AbGFADKMgG68wMXAG4a9Jq049+ggJI8ZEMfKhKV6mpoDMTUgyEMA3jQYeGsbABMIXSQBPBmBAjBgAbQBdBgB9BA1qTwZ3T2J7IQAKACEwyJiEAEoGAF4APjiEUoYE1I86pns2GF8AeQDA2TFsLgAeUPCKgEFyTOLyysTkhvSGZraOrp7+vOHSMYa3JOTPWAAzBgVsKBrcav2GAFkYfGhAy+PcAAtsSRYAcRgMIagobECACLYDDYZp7GQwLgqTLxQpTHYeYgAdiuNzuDygz1ewm84XmPnaQSWXEyFwQuDQlRYABlIQBzDBPOENAC+DRmTRaBMW3T6Axgw1sY1cDWSRxOuF51S4MAA7nMuYTOryVoMYQhKXRKfEafTGcztgjkQxJT14R42Yb2YbGgqFkSVfyKtlRrweCaQVAMOMKjqhrhiZlcJ7veaUjbZvilcTVQKGNl1m6MB7jt7NqLdjADuLTucswwAKpcXDYcEsIY+Hyk/PXW5Qe6PF5vT7fX7/IEgsEQqEwdWFSmZLhJwqZDjD4Opg0IxEo2voxvYoS4mBRnk9asHcnahC6rgMplhy3TCOc+3K5ZO+NCpMpr3FLbTw7HE1SkoMGXy1cOi+rdWUidetuu77lO07GqaXCHjQLJAA=

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public class C {
    readonly byte[] _x;
    
    public C(Byte[] x) => _x = x;
    
    public ReadOnlySpan<byte> A1() => _x;
    
    public ReadOnlySpan<byte> A2()
    {
        ref var xs = ref MemoryMarshal.GetArrayDataReference(_x);
        return MemoryMarshal.CreateReadOnlySpan(ref xs, _x.Length);
    }
    
    public ReadOnlySpan<byte> A3() {
        var span = new ReadOnlySpan<byte>(_x, 0, _x.Length);
        return span;
    }
   
    
    public ReadOnlySpan<byte> B1(int start) => _x.AsSpan(start);
    
    public ReadOnlySpan<byte> B2(int start)
    {
        ref var xs = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_x), (nint)(uint)start);
        return MemoryMarshal.CreateReadOnlySpan(ref xs, _x.Length);
    }
    
    public ReadOnlySpan<byte> B3(int start) {
        var span = new ReadOnlySpan<byte>(_x, start, _x.Length);
        return span;
    }
}
C.A1()
    L0000: mov rax, [rcx+8]
    L0004: test rax, rax
    L0007: jne short L0010
    L0009: xor ecx, ecx
    L000b: xor r8d, r8d
    L000e: jmp short L0018
    L0010: lea rcx, [rax+0x10]
    L0014: mov r8d, [rax+8]
    L0018: mov [rdx], rcx
    L001b: mov [rdx+8], r8d
    L001f: mov rax, rdx
    L0022: ret

C.A2()
    L0000: mov rax, [rcx+8]
    L0004: mov rcx, rax
    L0007: cmp [rcx], ecx
    L0009: add rcx, 0x10
    L000d: mov eax, [rax+8]
    L0010: mov [rdx], rcx
    L0013: mov [rdx+8], eax
    L0016: mov rax, rdx
    L0019: ret

C.A3()
    L0000: sub rsp, 0x28
    L0004: mov rax, [rcx+8]
    L0008: mov rcx, rax
    L000b: mov eax, [rax+8]
    L000e: mov r8d, [rcx+8]
    L0012: mov r9d, eax
    L0015: cmp r8, r9
    L0018: jb short L002c
    L001a: add rcx, 0x10
    L001e: mov [rdx], rcx
    L0021: mov [rdx+8], eax
    L0024: mov rax, rdx
    L0027: add rsp, 0x28
    L002b: ret
    L002c: call 0x00007ffc8f6d5b28
    L0031: int3

C.B1(Int32)
    L0000: sub rsp, 0x28
    L0004: mov rax, [rcx+8]
    L0008: test rax, rax
    L000b: jne short L0019
    L000d: test r8d, r8d
    L0010: jne short L003e
    L0012: xor ecx, ecx
    L0014: xor r9d, r9d
    L0017: jmp short L002f
    L0019: mov r9d, [rax+8]
    L001d: cmp r9d, r8d
    L0020: jb short L003e
    L0022: add rax, 0x10
    L0026: sub r9d, r8d
    L0029: movsxd rcx, r8d
    L002c: add rcx, rax
    L002f: mov [rdx], rcx
    L0032: mov [rdx+8], r9d
    L0036: mov rax, rdx
    L0039: add rsp, 0x28
    L003d: ret
    L003e: call 0x00007ffc8f6d5b28
    L0043: int3

C.B2(Int32)
    L0000: mov rax, [rcx+8]
    L0004: mov rcx, rax
    L0007: cmp [rcx], ecx
    L0009: add rcx, 0x10
    L000d: mov eax, [rax+8]
    L0010: mov r8d, r8d
    L0013: add rcx, r8
    L0016: mov [rdx], rcx
    L0019: mov [rdx+8], eax
    L001c: mov rax, rdx
    L001f: ret

C.B3(Int32)
    L0000: sub rsp, 0x28
    L0004: mov rax, [rcx+8]
    L0008: mov rcx, rax
    L000b: mov eax, [rax+8]
    L000e: mov r9d, r8d
    L0011: mov r10d, eax
    L0014: add r9, r10
    L0017: mov r10d, [rcx+8]
    L001b: cmp r9, r10
    L001e: ja short L0038
    L0020: add rcx, 0x10
    L0024: movsxd r8, r8d
    L0027: add rcx, r8
    L002a: mov [rdx], rcx
    L002d: mov [rdx+8], eax
    L0030: mov rax, rdx
    L0033: add rsp, 0x28
    L0037: ret
    L0038: call 0x00007ffc8f6d5b28
    L003d: int3

@github-actions github-actions bot added the performance Performance related issue label Nov 3, 2021
@finphie finphie merged commit 6db1435 into main Nov 3, 2021
@finphie finphie deleted the performance/AsSpan branch November 3, 2021 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant