# Boolean short circuit "fusion"

**URL:** <https://discourse.julialang.org/t/boolean-short-circuit-fusion/19641>\
**Category:** General Usage\
**Created:** [January 14, 2019, 10:33pm UTC](https://discourse.julialang.org/t/boolean-short-circuit-fusion/19641 "2019-01-14T22:33:10Z")\
**Posts on this page:** 1\
**Showing post:** 20

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [January 15, 2019, 11:20pm UTC](https://discourse.julialang.org/t/boolean-short-circuit-fusion/19641/20 "2019-01-15T23:20:49Z")

</div>

After reading that issue I had some inspiration -

```julia
@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
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.

---

_[View the full topic](https://discourse.julialang.org/t/boolean-short-circuit-fusion/19641)._
