Push Ifs Up and Fors Down
a year ago
- #programming
- #refactoring
- #optimization
- Push Ifs Up: Move conditional checks (ifs) to the caller to centralize control flow, reduce redundant checks, and simplify functions.
- Benefits of Pushing Ifs Up: Fewer overall checks, easier identification of dead branches, and centralized complex logic in one place.
- Dissolving Enum Refactor: A pattern where pulling up conditions reveals redundant or triplicated logic.
- Push Fors Down: Operate on batches of objects for performance gains, amortizing startup costs, and enabling vectorized processing.
- Performance Benefits: Batch processing can significantly improve performance, especially in hot paths handling many entities.
- Expressiveness: Batch operations can also enhance code expressiveness, as seen in libraries like jQuery.
- Composition: Combining 'push ifs up' and 'push fors down' can optimize both control flow and data processing.
- Example: FFT-based polynomial multiplication shows how batch processing can outperform individual evaluations.
- TigerBeetle Architecture: Uses batch processing in the data plane to amortize decision-making costs in the control plane.