Legend

Class
Struct
Enum
Interface
Delegate
Constructor
Method
Property
Event
Field

Static method: T Run<T>(WorkNode<T>)

Declaration

public static T Run<T>(
    WorkNode<T> node
)

Summary

Runs the specified work on a custom call stack, which is not limited to 1 MB as the standard call stack is. See remarks for details.

Generic type parameters

T Type of result to compute.

Parameters

WorkNode<T>node Work to be executed.

Returns

The result of the computation.

Remarks

CustomCallStack.Run<T>(WorkNode<T>) expects a delegate that is structured in such a way that it:

  • always knows at what state in the computation it is each time it is called;
  • returns a WorkStep<T>.Call each time it requires some other value to be computed, and then expects that resulting value to come in through the parameter next time it is called;
  • returns a WorkStep<T>.Return when it is done. At this point the delegate is not called again.
  • The delegate may return null to indicate the same as a WorkStep<T>.Return containing a default(T) value.

CustomCallStack works as follows:

  • The first time the specified delegate is called, its parameter receives default(T).
  • If the value returned by the delegate is a WorkStep<T>.Return, the work is done and the result is returned.
  • If the value returned is a WorkStep<T>.Call, this is treated similarly to a method call. The old delegate is pushed on a stack and the new delegate is executed according to the same rules until it returns a WorkStep<T>.Return. Once it does so, the original delegate is popped from the stack and then called with the result passed into its parameter.

There are deliberately no safeguards in this algorithm; it will allow you to grow the stack indefinitely and not generate an equivalent to the StackOverflowException. This means that if your delegates always return a WorkStep<T>.Call, they will consume memory rampantly and never finish.