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
1
u/smdowney WG21, Text/Unicode SG, optional<T&> Aug 14 '26
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.