The type loading in the runtime is a recursive algorithm that consumes quite significant amount of stack space per type hierarchy level. Moreover, the stack consumption gets amplified if an exception is thrown when attempting to load one of the intermediate or the base type due to the behavior of native EH on Windows. On each level, there is EX_TRY with EX_HOOK block that does some state maintenance and then rethrows. That again grows the stack since native EH on Windows doesn't unwind the stack until a catch block completes without exceptions.
A first party has hit a problem when a 57 level trivial type hierarchy consumed more than 1.5MB of stack space, triggering stack overflow. Just the loading of that type hierarchy without an exception consumes ~500kB of memory.
The amplification due to the native EH can be mitigated by replacing the EX_HOOK by EX_CATCH and rethrowing after the catch (for Exception derived exceptions).
However, it seems it would make sense to remove the recursion if possible.
The type loading in the runtime is a recursive algorithm that consumes quite significant amount of stack space per type hierarchy level. Moreover, the stack consumption gets amplified if an exception is thrown when attempting to load one of the intermediate or the base type due to the behavior of native EH on Windows. On each level, there is
EX_TRYwithEX_HOOKblock that does some state maintenance and then rethrows. That again grows the stack since native EH on Windows doesn't unwind the stack until a catch block completes without exceptions.A first party has hit a problem when a 57 level trivial type hierarchy consumed more than 1.5MB of stack space, triggering stack overflow. Just the loading of that type hierarchy without an exception consumes ~500kB of memory.
The amplification due to the native EH can be mitigated by replacing the
EX_HOOKbyEX_CATCHand rethrowing after the catch (forExceptionderived exceptions).However, it seems it would make sense to remove the recursion if possible.