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

Constant stackalloc can be optimized by the JIT. #569

Merged
merged 38 commits into from
Aug 3, 2021

Conversation

ycrumeyrolle
Copy link
Collaborator

dotnet/runtime#54186 (comment)

For example:

    private int Variable(int length) {
        Span<byte> array = stackalloc byte[length];
        return array[0] + length;
    }

    L0000: push ebp
    L0001: mov ebp, esp
    L0003: sub esp, 8
    L0006: xor eax, eax
    L0008: mov [ebp-8], eax
    L000b: mov [ebp-4], eax
    L000e: mov [ebp-4], esp
    L0011: mov dword ptr [ebp-8], 0xe96c2ce5
    L0018: mov eax, edx
    L001a: mov ecx, eax
    L001c: test ecx, ecx
    L001e: je short L002e
    L0020: add ecx, 3
    L0023: shr ecx, 2
    L0026: push 0
    L0028: dec ecx
    L0029: jne short L0026
    L002b: lea ecx, [esp]
    L002e: mov [ebp-4], esp
    L0031: test eax, eax
    L0033: jl short L0052
    L0035: cmp edx, 0
    L0038: jbe short L0058
    L003a: movzx eax, byte ptr [ecx]
    L003d: add eax, edx
    L003f: cmp dword ptr [ebp-8], 0xe96c2ce5
    L0046: je short L004d
    L0048: call 0x71e32050
    L004d: lea esp, [ebp]
    L0050: pop ebp
    L0051: ret
    L0052: call System.ThrowHelper.ThrowArgumentOutOfRangeException()
    L0057: int3
    L0058: call 0x71e31070
    L005d: int3
    private const int ConstantLength = 32;   
    private int Constant(int length) {
       Span<byte> array = stackalloc byte[ConstantLength];
        return array[0]+length;
    }

    L0000: sub esp, 0x24
    L0003: vxorps xmm4, xmm4, xmm4
    L0007: vmovdqu [esp], xmm4
    L000c: vmovdqu [esp+0x10], xmm4
    L0012: xor eax, eax
    L0014: mov [esp+0x20], eax
    L0018: mov dword ptr [esp+0x20], 0xe96c2ce5
    L0020: lea eax, [esp]
    L0023: movzx eax, byte ptr [eax]
    L0026: add eax, edx
    L0028: cmp dword ptr [esp+0x20], 0xe96c2ce5
    L0030: je short L0037
    L0032: call 0x71e32050
    L0037: add esp, 0x24
    L003a: ret

@ycrumeyrolle ycrumeyrolle merged commit 8f7d759 into master Aug 3, 2021
@ycrumeyrolle ycrumeyrolle deleted the stackalloc-constant branch August 3, 2021 15:18
ycrumeyrolle added a commit that referenced this pull request Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant