Skip to content

stackalloc docs should discuss performance benefit of constant size #28823

@JamesNK

Description

@JamesNK

See Twitter discussion here: https://twitter.com/EgorBo/status/1508069816275513344

tldr:

Current:

const int MaxStackLimit = 1024;
Span<byte> buffer = inputLength <= MaxStackLimit ? stackalloc byte[inputLength] : new byte[inputLength];

Slightly faster:

const int MaxStackLimit = 1024;
Span<byte> buffer = inputLength <= MaxStackLimit ? stackalloc byte[MaxStackLimit].Slice(0, inputLength) : new byte[inputLength];

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions