Instantiation of recursive generic types slows down exponentially the deeper they are nested. Why?

后端 未结 4 725
独厮守ぢ
独厮守ぢ 2020-12-13 04:11

Note: I may have chosen the wrong word in the title; perhaps I\'m really talking about polynomial growth here. See the benchmark

4条回答
  •  既然无缘
    2020-12-13 04:38

    Accessing a new type causes the runtime to recompile it from IL to native code (x86 etc). The runtime also optimizes the code, which will also produce different results for value types and reference types.

    And List clearly will be optimized differently than List>.

    Thus also EmptyStack and NonEmptyStack> and so on will be handled as completely different types and will all be 'recompiled' and optimized. (As far as I know!)

    By nesting further layers the complexity of the resulting type grows and the optimization takes longer.

    So adding one layer takes 1 step to recompile and optimize, the next layer takes 2 steps plus the first step (or so) and the 3rd layer takes 1 + 2 + 3 steps etc.

提交回复
热议问题