After reading that issue I had some inspiration -
@inline _lazy(x) = x[1] # unwrap the tuple
@inline Broadcast.broadcasted(::typeof(_lazy), x) = (x,) # wrap the Broadcasted object in a tuple to avoid materializing
macro lazy(x)
return esc(:(_lazy(_lazy.($x))))
end
Which gives
julia> a = rand(10000) ; b = rand(10000) ;
julia> @btime sum(a .+ b)
10.896 μs (5 allocations: 78.27 KiB)
9902.652963666198
julia> @btime sum(@lazy a .+ b)
13.095 μs (3 allocations: 64 bytes)
9902.652963666245
julia> @btime any(a .+ b .> 1.9)
15.549 μs (7 allocations: 5.64 KiB)
true
julia> @btime any(@lazy a .+ b .> 1.9)
1.016 μs (4 allocations: 96 bytes)
true
So not materializing the array isn’t always faster (depends on the operation) but it does save on allocations.