Proposal: Scoped Memory and Arena-Based Lifetime Management #120166
Replies: 3 comments 7 replies
-
While finalizers are non-deterministic, there're also rare.
Net 10 improves on object stack allocation, which provides at least a partial instance of this (and potentially cheaper for some usage patterns).
What does this mean? Just that it can't be returned? Or does it also prevent passing a value as a parameter to another method? The former is expected, the latter makes it much less useful. Note that C# doesn't currently have a full lifetime tracking system as would be required for even some of the earlier portions of this proposal. This is an enormous engineering effort. |
Beta Was this translation helpful? Give feedback.
-
This is both requires really complicated analysis (that is easy to break), and any attempt at this would expose internal method implementation details, where updating a library/dependency could cause calling code to fail to compile (or potentially worse, compile but fail at runtime). Probably, the requirement would have to be that
This would be an entirely new language, or at least involve a similar engineering effort. |
Beta Was this translation helpful? Give feedback.
-
This is impossible to implement at C# level at all, as well as other high-level languages. With binary distribution used instead of source distribution, the C# compiler has no view of any callee method. This makes escape analysis being totally broken at method boundary. Adding escape model to public API from another project is also impractical. There have been various discussions around this. Instead, we are doing escape analysis in JIT, which has whole view of inlined functions, which is most likely to be benefit from this. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation
The .NET runtime today relies on a generational garbage collector (GC). While GC is convenient, it introduces:
For high-performance, real-time, or embedded workloads, developers need predictable, deterministic memory management. This proposal introduces scoped arenas as a first-class memory model in C#, enabling applications to run with minimal GC pressure or even disable the GC entirely.
Language & Runtime Features
1. Scoped Blocks
Rules:
2. Scoped Methods
Rules:
3. Scoped Classes
Rules:
4. Allocation-Site Scoping
Allocation‑site scoping allows developers to force an object into the current arena at the point of allocation, even if the type itself was not designed with scoped in mind.
scoped var client = new HttpClient(); // allocated in arena
Rules:
5. Arena Flattening
Rules:
6. Opt-In Model
Scoped memory must be explicitly enabled in project settings:
Rules:
7. GC-Free Mode
If the entire codebase uses scoped lifetimes, the GC can be disabled:
Rules:
8. Allocator Strategies
Arenas require backing allocators. The runtime/compiler chooses the best per platform:
Rules:
Compiler & Runtime Responsibilities
Conclusion
Scoped arenas in .NET would be a paradigm shift, offering developers the choice between convenience and raw performance, with compiler-enforced safety and platform-optimized allocators.
Beta Was this translation helpful? Give feedback.
All reactions