Language: Julia · Versions: Julia 1.11.2
The problem
I have a tight numerical inner loop inside a function that passes an anonymous closure x -> x * scale to sum. Later in the same function (even in a dead if false branch or a post-processing step), scale is reassigned (scale = scale * 2.0). As soon as that reassignment exists anywhere in the function body, @code_warntype shows scale::Core.Box, SIMD vectorization disappears, and the inner loop allocates millions of heap boxes!
Minimal example
function compute_sum(data::Vector{Float64}, flag::Bool)
scale = 2.5
# Closure captures `scale`:
total = sum(x -> x * scale, data)
if flag
scale = 1.0 # Reassigning `scale` forces Julia's lowering pass to wrap `scale` in `Core.Box`!
end
return total + scale
end
using InteractiveUtils
@code_warntype compute_sum([1.0, 2.0, 3.0], false)
Expected
compute_sum compiles x -> x * scale with an unboxed Float64 capture and zero heap allocations.
What happens instead
Because Julia's AST lowering (before type inference runs!) sees that scale is both captured by a closure and assigned more than once in the outer scope, it boxes scale into an untyped Core.Box (Any), making x * scale type-unstable (Any) inside the hot loop!
Why does Julia's Core.Box lowering happen before type inference, and beyond let scale = scale blocks, when can Ref(scale) or typed boxes solve mutable captures without losing type stability in Julia 1.11?
# Fix 1: a fresh binding that is only assigned once, used by the closure
function compute_sum(data::Vector{Float64}, flag::Bool)
scale = 2.5
total = let s = scale
sum(x -> x * s, data)
end
if flag
scale = 1.0 # this binding is no longer captured, so no Box
end
return total + scale
end
# Fix 2: a typed mutable cell when the closure really must see later writes
function compute_sum_ref(data::Vector{Float64}, flag::Bool)
scale = Ref(2.5) # Base.RefValue{Float64}; `scale` itself is assigned once
total = sum(x -> x * scale[], data)
flag && (scale[] = 1.0)
return total + scale[]
end
# Fix 3: keep the Box but give it a declared type
function compute_sum_ann(data::Vector{Float64}, flag::Bool)
scale::Float64 = 2.5 # reads get a typeassert, so x * scale infers as Float64
total = sum(x -> x * scale, data)
flag && (scale = 1.0)
return total + scale
end
Why boxing happens before inference
Julia closures capture variables, not values. A closure is lowered into a struct with one field for each captured variable. If the variable is ever reassigned, the closure and the outer function have to share one mutable cell, because the closure could be stored somewhere and called after the reassignment. Lowering only sees syntax. It can't tell that sum won't keep the closure, so it has to choose between a plain field and a shared cell before any types are known. When it chooses the cell, it uses Core.Box, whose contents field is untyped (Any). That's why one assignment anywhere in the body is enough. In your example it's actually needed: the assignment comes after the closure is created. This is the long-standing issue #15276, and the Julia manual covers it in Performance Tips › Performance of captured variable. I'm not aware of anything in 1.11 that changes it. A lowering rewrite (JuliaLowering.jl) is in progress upstream, but I wouldn't count on it for 1.11.
One correction about the cost. There's only one Core.Box. The millions of allocations come from each x * scale being a dynamic dispatch on Any that returns a boxed Float64.
When to use each fix
let/ a new name: best when the closure only needs the current value. The capture is a concretely typed field, with no allocation and no loss of SIMD. FastClosures.jl's@closuredoes this rewrite for you.Ref: best when you need shared mutable state that's still type-stable.RefValue{Float64}is a concrete type, soscale[]infers asFloat64. That costs at most one allocation per call, not one per element. This is the user-level "typed box".Core.Boxitself can't be given a type parameter.- Type annotation: the Box (and its single allocation) stays, but every read is asserted to be
Float64, so the loop body is type-stable again. The manual recommends this too.
I haven't run these, so this is reasoning from the lowering rules and the docs. To check it, run @code_warntype on the closure itself (@code_warntype (x -> x * s)(1.0) inside the let won't show it, so call it via code_warntype(f, (Float64,)) on the captured closure) and @allocated compute_sum(data, false) after warm-up. Fixes 1 and 2 should report 0 or 16 bytes, not something proportional to length(data).