02/10/2025 –, Amphithéâtre Jean-Baptiste Say Langue: English
Writing a loop to add up all the elements in an array is often featured in tutorials and training materials — it's just five lines of code, and can be remarkably generic and performant. Yet there are a surprising number of challenges in determining exactly how such a function should operate while ensuring consistent generic behaviors, numeric accuracy, high performance, and an extensible interface.
This talk will describe these challenges and some of their solutions, looking at both the specific designs of Julia's builtin reduction machinery (and how it may change) as well as general learnings about best practices when designing such highly generic, performance-critical, and extensible interfaces.
Base Julia's reduction machinery is largely the same as it was originally implemented over a decade ago, and it implements highly generic behaviors that apply across many different types of containers. It has many orthogonal features and switches. This includes:
- The three functions with differing associativities:
foldlvs.reducevs.foldr - Whether an
initwas provided or not — and if so, how thatinitshould be used - Whether
dimswas provided or not — and if so, if the first dimension is included - If the container is iterable or array-like — and if so, whether its indices are linear or cartesian
- If the container has zero, one, or more elements
- If there's a known reducing and/or mapping function being applied — and if it's ok to speculatively evaluate or memoize calls of the passed functions
In short: it's complicated! It's also highly performance sensitive. Everyone expects the sum function to be both fast and accurate, and while there are cases where those two criteria align, they can also be at adds with one another.
Julia's current documentation hasn't fully specified how many of these features should work, but a number of devs have been working over the past few years to more rigorously define what the behaviors should be. As consensuses around the design fundamentals have grown, it's become more clear what incremental changes are possible and how a completely reimagined implementation could work. Building out that reimagined implementation has not been trivial, and it brings together many learnings of best practice generic Julia programming.
Matt has been a part of the Julia community for over a decade and is the Director of Sales Engineering at JuliaHub.