runtime: [API Proposal]: Infinite stack

Background and motivation

I suggest adding a class where you can specify the stack size for the executing method.

API Proposal

 class StackExecute
{
    public  void Run(Action action,float sizeStack);
}

API Usage

int count = 0;
StackExecute execute = new();
execute.Run(()=>{
    TestMethod();
    },2000_000);    
void TestMethod()
{
    count++;
    if(count < 1000_000)
        TestMethod();
}

Alternative Designs

No response

Risks

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 23 (15 by maintainers)

Most upvoted comments

Strange code: a task is started, and then once it’s completed, GetAwaiter().GetResult() is called on it, despite the fact that the task has already been completed for a while.

It’s queuing a task and then blocking to wait for it to complete; the task is not guaranteed to have been completed when GetResult is called and generally won’t have been. But even if it was, the GetResult here also serves to propagate any exception that may have occurred, and in other overloads, to actually return any result.

Regardless, thanks for the suggestion, and I’m closing this issue; as multiple folks have stated, it’s not practical to add the suggested API currently and does not add meaningful enough benefit to warrant the massive infrastructural changes that would be necessary to support linked stacks.

Well, I thought ContinueWith is executed after the first task is completed?

Yes, that’s the case.

The external .GetAwaiter().GetResult() will be for the task started with ContinueWith(), not the original task started by Task.Run().

@Mr0N if you are writing your own recursive algorithm (as opposed to running third-party code with a huge stack), Recursion’t will likely prove useful to you.

…I would like something like a class where I allocate a gigabyte, and once it finishes its work, I can destroy it. I imagine the implementation would create another stack in the same thread that can be closed after the method completes.

It sounds a little like what you might want is actually an Arena allocator

Here are a few:

Is it possible to allocate a new stack, then hook up a continuation thunk that would handle returning to the old one

At the hardware level, yes. But most OS don’t provide such functionality because it is atypical and unnecessary. There is next to zero practical benefit to having stacks that can grow infinitely and many very real downsides ranging from perf to security and beyond.

The OS cares about where the stack is because its responsible for all memory on the system, it has to manage and share that memory between all processes, understand what is actually in use, what has been allocated but not yet committed, etc. It has to be able to “clean” up that memory when the process terminates (regardless of how it terminated).

This is very much a case where you will rarely hit the stack limit in practice, even though the typical stack size is already “small” on most platforms (it’s 1MB on Windows, 8MB on Linux, 512KB on MacOS – for 64-bit). It is a case where the physical hardware often has specialized handling and presumes stacks will be relatively small and returns will not happen “rapidly” (where it often uses an internal portion of the register file to do mirroring of stack spills, where it tracks a limited number of return addresses so that returns are faster, and other optimizations). Where approaching the upper limits of the already “small” stack sizes can hurt performance, where there are often trivial and far more performant workarounds for scenarios where deep recursion is expected, etc.

Something being technically possible does not necessarily mean it is good to implement or support in practice, which is why most OS do not provide such built-in support (at least not in a way that makes it trivial to use/support).

If you are doing an operation that needs 1GB of stack space, the overhead of creating a new thread and Joining it will be negligible.

Creating a new thread to execute a method is not very pleasant.

It’s what is fundamentally required for this to work and has almost identical syntax to what you’ve proposed above.

I would like something like a class where I allocate a gigabyte, and once it finishes its work, I can destroy it.

This would not be recommended as it would fail on most computer systems, especially mobile devices or laptops. It would likely cause thrashing and other issues as well, making your code many times slower, cause UI or other delays, etc.

I imagine the implementation would create another stack in the same thread that can be closed after the method completes.

Most OS do not allow this, once a thread begins execution its stack size is fixed for the lifetime of the thread.

Can I have a float.Epsilon stack size please?

Not sure what to pass in? PositiveInfinity to the rescue!

I like this api