This is still recursion, no? The lambda calls self(...). The condition for recursing is stored somewhat as a type, and I can see how a compiler could have an easier time inlining because of that, but the code is structured as recursion.
Trees are inherently recursive, so yes, it has to go somewhere. But it's not in the base data type anymore, the recursive positions are instead open template parameters.
The Fix hack wraps and unwraps one layer at a time, which is all you really need, and the callback algebra that processes a layer gets the evaluation of the recursive positions handed to it rather than calling it itself.
The inlining and inversion of control for the recursive call makes the compiler surprisingly happy. When I arrived at this I was hoping that it would not be worse and it would be an OK tradeoff against increased correctness. But I keep running into it being a benchmarked optimization, which I did not expect.
View all comments
Show parent comment
3
u/not-my-walrus Aug 14 '26
This is still recursion, no? The lambda calls
self(...). The condition for recursing is stored somewhat as a type, and I can see how a compiler could have an easier time inlining because of that, but the code is structured as recursion.