3 ms·
No, performance would probably be worse. For one, std::function is not just for lambdas. It uses type-erasure and dynamic allocation to hold any sort of callabl
by slavik81 9y ago
No, performance would probably be worse. For one, std::function is not just for lambdas. It uses type-erasure and dynamic allocation to hold any sort of callable object/function. It's a generic type with a complex implementation and I wouldn't assume it would be optimized away.
Additionally, when using Either the lambdas are in the caller and are invoked almost immediately by map. The scope is small and they do not enter into any other compilation unit. If you pass them into the called function, the compiler is only going to be able to optimize them if the called function can be inlined. Even then, the optimizer will have to be smarter, because the error handler will travel through a much larger scope and a type conversion.
- AstralStorm 9y agoEither inserts a conditional and union dereference. Unions are pretty terrible on their own to optimize, even with strict aliasing. The pointer dereference and call overhead is not big and error handling code is supposed to be cold anyway.