Skip to content

Proposal: Allow to allocate objects on the stack #240

@ygc369

Description

@ygc369

To reduce GC, C# should provide mechanisms to allocate objects on the stack.

  1. Escape Analysis. This is the most important thing. We can get "free" performance improvement through it without any code change.
  2. Syntax to force to allocate on the stack.
    (from http://xoofx.com/blog/2015/10/08/stackalloc-for-class-with-roslyn-and-coreclr/)

Escape analysis in many case is impossible to perform. Allocate a class and pass it to a virtual method (for which you know only the type at the callsite, unless allocated in the same method), and you won’t have any way to determine whether stackalloc is safe or not.

So besides escape analysis, we want to have syntax to guarantee some objects allocated on the stack. If the stack-allocated object has finalizer, then its finalizer should be executed once its last reference goes out of scope, not need wait until GC finishes.

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