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

[Analyzer]: Recommend usage of constant in stackalloc expressions #89478

Open
just-ero opened this issue Jul 25, 2023 · 2 comments
Open

[Analyzer]: Recommend usage of constant in stackalloc expressions #89478

just-ero opened this issue Jul 25, 2023 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Meta code-analyzer Marks an issue that suggests a Roslyn analyzer
Milestone

Comments

@just-ero
Copy link
Contributor

.NET 8 introduces some performance-related warnings. This made me think of another commonly discussed issue; using constant sizes in stackalloc expressions.

A dynamically-sized stackalloc array can potentially be worse for performance. An analyzer warning for such a situation could be beneficial.

Span<byte> data = len <= 128 ? stackalloc[len] : new byte[len]; // warning: Use constant size in `stackalloc` expression

// fix (have to use `.Slice(0, len)` when using the span)
Span<byte> data = len <= 128 ? stackalloc[128] : new byte[len];

Here are some relevant benchmark results: https://gist.github.com/just-ero/7ef8dd2b49fa5c1cf8d47bbbc6112bd6.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 25, 2023
@ghost
Copy link

ghost commented Jul 25, 2023

Tagging subscribers to this area: @dotnet/area-meta
See info in area-owners.md if you want to be subscribed.

Issue Details

.NET 8 introduces some performance-related warnings. This made me think of another commonly discussed issue; using constant sizes in stackalloc expressions.

A dynamically-sized stackalloc array can potentially be worse for performance. An analyzer warning for such a situation could be beneficial.

Span<byte> data = len <= 128 ? stackalloc[len] : new byte[len]; // warning: Use constant size in `stackalloc` expression

// fix (have to use `.Slice(0, len)` when using the span)
Span<byte> data = len <= 128 ? stackalloc[128] : new byte[len];

Here are some relevant benchmark results: https://gist.github.com/just-ero/7ef8dd2b49fa5c1cf8d47bbbc6112bd6.

Author: just-ero
Assignees: -
Labels:

area-Meta

Milestone: -

@EgorBo
Copy link
Member

EgorBo commented Jul 25, 2023

  1. What's the threshold when analyzer should not suggest that, e.g. should we promote len <= 4096 ? stackalloc[len]?
  2. In some cases it might increase overall stack usage when the actual len is small. On macOS all secondary threads have 512Kb of stack by default.
  3. In some cases non-constant stackalloc might be faster if the actual length is small, I was able to reproduce it:

image

  1. With SkipLocalsInit the performance difference might be similiar for sizes >= 32
  2. In theory, we could optimize this in JIT

@EgorBo EgorBo added code-analyzer Marks an issue that suggests a Roslyn analyzer api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jul 25, 2023
@ericstj ericstj removed the untriaged New issue has not been triaged by the area owner label Aug 7, 2023
@ericstj ericstj added this to the Future milestone Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Meta code-analyzer Marks an issue that suggests a Roslyn analyzer
Projects
None yet
Development

No branches or pull requests

3 participants