Skip to content

Do not claim that structs are on the stack #4561

Closed

Description

@svick

The new Operator article says:

Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap.

Depending on how you understand it, this claim could be considered true, if you consider that value types are created on the stack, but then can be copied to the heap.

But I think it's still too confusing and too close to the common misconception of "value types are stored on the stack, reference types on the heap" and that the wording should be changed.

(But I'm not sure what should it be changed to, which is why I'm creating an issue and not a PR.)

Activity

  1. BillWagner commented on Mar 7, 2018

    @BillWagner
    Member

    I'd like to start a discussion on this issue.

    First, this is much larger than a single topic. A search of the repo for the phrase "on the stack" produces more than 100 occurrences. Some are correct, in that they refer to a Stack collection. Most refer to the common inaccurate phrase that value types are "on the stack".

    @ericlippert wrote a few great blog posts explaining that the relevant fact for value types is they are copied by value, not that they are stored on the stack. 1 2

    The recent additions of in, readonly struct and ref struct declarations make the copy semantics much more relevant than the storage implementation.

    I propose that we remove any description of stack storage from all documentation on value types in the language guide(s) and the .NET guide, and replace that explanation with a correct discussion on the copy semantics.

  2. changed the title [-]The new operator article should not claim that structs are created on the stack[/-] [+]Do not claim that structs are on the stack[/+] on Mar 7, 2018
  3. d0pare commented on Mar 7, 2018

    @d0pare

    I would suggest to add clear note that value types are not restricted to stack storage instead of removing all descriptions about stack storage.

  4. jskeet commented on Mar 7, 2018

    @jskeet

    While I'm always nervous about disagreeing with Bill, I'd argue that ref struct makes the stack/heap distinction more relevant than ever before. The rules around what you can do with a ref struct are designed to avoid it escaping the stack. (The implementation details of captured variables etc then definitely leak into the language.)

    I think it would make sense to start with the "copy by value" part, but then include material about heap vs stack in later material.

  5. ericlippert commented on Mar 7, 2018

    @ericlippert

    This is indeed a tricky topic with over a decade of misinformation and confusing documentation muddying the waters. So indeed, first, do no further harm!

    The situation is further complicated by the fact that most people still reason from the perspective that coroutines or closures don't even exist, even though they've been in C# for well over a decade.

    If I had the opportunity to do it all over again knowing what I know now, I would emphasize the following key points:


    • Many methods in C# are coroutines. That is, upon activation they can subsequently return, hang (run forever), throw, or suspend. (That is: yield or await) A method which never possibly suspends we can call a "normal method". A coroutine which suspends can be resumed.

    • Variables are storage locations that store values. Values are either references or instances of value types.

    • "Temporary" variables are special unnamed variables used to hold values created during expression evaluation.

    • Variables have a lifetime that is either long or short. A variable with short lifetime is one where we know at compile time that it will never be used after the return/throw of the method that created it, or across a suspension of the coroutine that created it.

    • Variables not known to have short lifetimes must conservatively be given long lifetimes.

    • An aliasing relationship -- that is, two names for the same storage location -- can be imposed by ref/out/in. Establishing an alias must never cause the aliasing variable to live longer than the aliased variable.

    • The transitive closure of references in long-lived variables lives arbitrarily long; this imposes significant runtime costs because a garbage collector must work out which long-lived variables are dead, and collect their storage. However, the total amount of storage available is limited only by address space available to the process.

    • No such runtime costs are imposed by short-lived variables. However, an extremely small amount of memory -- typically only a million bytes -- is reserved for short-lived variables; if this pool is exhausted then the program will fail catastrophically with no opportunity to recover.


    You'll note that nowhere in there did I say anything about heap, stack or registers; all of those are implementation details. But everything follows logically from these principles. A closed-over local variable can be accessed after the method which declared the local returns, and therefore it is on the long-term pool. And so on.

    However, at this point, decades in, it is probably too late to go refactoring all of the documentation from scratch.

  6. BillWagner commented on Mar 8, 2018

    @BillWagner
    Member

    @jskeet This is a great discussion.

    Consider this explanation of ref struct:

    A ref struct must have a known short lifetime (to use Eric's term above). It follows that ref struct types can only be local variables of methods that are not coroutines, or members of another ref struct. They cannot be members of a class or a struct. They cannot be return values (see note below). They cannot be captured by lambda expressions. They cannot have lifetimes that span await in async methods, or yield return in iterator methods.

    Note: a ref struct can be returned by ref when that ref struct was passed into the method by ref.

    I think that "on the stack" has been a colloquialism for "variable known to have a short lifetime".

  7. jskeet commented on Mar 8, 2018

    @jskeet

    I'd say that "on the stack" is more "the usual implementation of variables known to have a short lifetime" - which ties in with Eric's description. On the other hand, by the time we start talking about the amount of memory available for short-lived variables, we're into implementation details anyway.

    I think there's more to using stack/heap than just the age of C# and the amount of existing documentation - there's also the expectations coming from developers in other languages, who look for familiar concepts. If we could start the whole process of describing all programming languages from scratch, Eric's description might be ideal.

    Some quick points to note about ref structs though (having been exploring them more just this week):

    • They can (currently, at least) be used in iterator methods, so long as they're only used "between" yield return statements (rather than usage potentially straddling a yield return statements)
    • I'd hope that the same would be true for async at some point, even though it appears not to be yet
    • They can be declared in lambda expressions, but can't be captured from an enclosing scope by lambda expressions

    Let me know if you'd like examples of all of these for further checks as to whether they're the expected behavior or not. It wouldn't be entirely unusual for me to have wandered into corner cases...

  8. BillWagner commented on Mar 8, 2018

    @BillWagner
    Member

    Both of us missed the full list, which is here as a proposal in the csharplang repo

    To your bullet list:

    • They can (currently, at least) be used in iterator methods, so long as they're only used "between" yield return statements (rather than usage potentially straddling a yield return statements)

    The third bullet item in the proposal indicates that ref structs should be legal in async methods, as long as they are not in scope at the await expression.

    • They can be declared in lambda expressions, but can't be captured from an enclosing scope by lambda expressions

    Yes, I updated my comment above.

  9. jskeet commented on Mar 8, 2018

    @jskeet

    I didn't miss the proposal - it's just it's inaccurate, in my experience :) Sample code:

    public ref struct RefLikeStruct
    {
        public void Method() => Console.WriteLine("Hi");
    }
    
    public class Test
    {
        public static IEnumerable<int> IteratorMethod()
        {
            yield return 0;
            // Rules say this should be invalid: foo is in scope
            // for both yield return statements. It compiles for me.
            var foo = new RefLikeStruct();
            foo.Method();
            yield return 1;
        }
    
        public static async Task AsyncMethod()
        {
            await Task.Delay(1000);
            {
                // Rules suggest this should be valid: foo is not in
                // scope at any await expression. It doesn't compile for me.
                var foo = new RefLikeStruct();
                foo.Method();
            }
            await Task.Delay(1000);
        }
    }

    The error message for the last part is unhelpful too:

    Parameters or locals of type 'RefLikeStruct' cannot be declared in async methods or lambda expressions.

    The part about lambda expressions is definitely not true for local variables...

    I've been assuming that the C# team is on the case in terms of tightening up the spec language, error messages, and actual error cases. If you think I've discovered areas they should be aware of, I'm happy to file issues.

  10. BillWagner commented on Mar 8, 2018

    @BillWagner
    Member

    ping @VSadov @jcouv Should Jon file issues based on the above?

  11. VSadov commented on Mar 8, 2018

    @VSadov
    Member

    I think allowing ref structs "between yields" in iterators is a bug.
    We catch the violation when it is time to capture, so it is safe, but capturing is an implementation detail.

    FWIW which and how locals are captured depends on the precision of flow analysis. It gets refined with almost every major release. It may also be reasonable to capture all locals in Debug - so that you would not lose the values when method is resumed after await/yield.

    Currently the language spec says unconditionally that ref struct locals are not allowed in iterators/async.
    In the future this may be relaxed to exclude blocks that do not contain yield/await.
    And that would have effect of requiring that locals in that block are never captured, which is ok, even considering the Debug scenario.

    I have logged a bug on that - dotnet/roslyn#25350

  12. VSadov commented on Mar 8, 2018

    @VSadov
    Member

    Re: "structs on the stack"

    Structs vs. classes distinction is mostly defined in terms of value/reference semantics. Structs certainly do not need to be on stack.

    The lifetime of variables (not just locals) is tied to scoping. As long as variable can be accessed, it must exist. Therefore, for example, when a variable is accessible from a nested method which may outlive the outer method where the local is formally defined, then such local cannot be implemented as a slot in the method frame and must be "captured", but that is an implementation detail.

    Note that this does not prohibit situations when underlying storage lives longer then needed. That could be wasteful, but otherwise noone would observe such difference through regular means provided in the language.
    (platform services like reflection and unsafe code should be ignored here since those specifically allow to glance at implementation details and we are talking about semantics)

    The problem with ref structs (and ref locals/parameters) is that "stack only" for these is a limitation of the platform which does not permit ref fields - primarily to make it simpler for GC.

    In a way the stack-only "leaks" the constraints of the underlying platform and that is why such details as capturing have an impact.

  13. 9 remaining items

  14. modified the milestones: Sprint 134 (4/7/2018 - 4/27/2018), on May 21, 2018
  15. added
    won't fixIssues that were closed as part of automated backlog grooming
    on Jan 25, 2021
  16. dotnet-bot commented on Jan 25, 2021

    @dotnet-bot
    Contributor

    This issue has been closed as part of the issue backlog grooming process outlined in #22351.

    That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the @dotnet/docs team for visibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions