# Are temporaries created in one line functions that return the result of a broadcasted function?

**URL:** <https://discourse.julialang.org/t/are-temporaries-created-in-one-line-functions-that-return-the-result-of-a-broadcasted-function/91701>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 15, 2022, 1:09pm UTC](https://discourse.julialang.org/t/are-temporaries-created-in-one-line-functions-that-return-the-result-of-a-broadcasted-function/91701 "2022-12-15T13:09:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [December 15, 2022, 1:09pm UTC](https://discourse.julialang.org/t/are-temporaries-created-in-one-line-functions-that-return-the-result-of-a-broadcasted-function/91701/1 "2022-12-15T13:09:50Z")

</div>

Suppose I have

```julia
fm_residuals(pc, F) = signed_dist.(pc,Ref(F))

```

and

```julia
tst .= fm_residuals(pc,F)

```

Will a temporary be created for the signed distances in the scope of `fm_residuals` or will the result be directly written into `tst` at the scope of the caller? Does the behavior depend on whether `fm_residuals` is inlined?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 15, 2022, 1:49pm UTC](https://discourse.julialang.org/t/are-temporaries-created-in-one-line-functions-that-return-the-result-of-a-broadcasted-function/91701/2 "2022-12-15T13:49:46Z")

</div>

Yes, a temporary will likely be created. The proper way to use broadcasting in this way is

```julia
tst .= signed_dist.(pc,Ref(F))

```

so that the two broadcast operations will fuse and avoid creating the temporary array.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 15, 2022, 1:49pm UTC](https://discourse.julialang.org/t/are-temporaries-created-in-one-line-functions-that-return-the-result-of-a-broadcasted-function/91701/3 "2022-12-15T13:49:47Z")

</div>

> [@prittjam](#):
>
> Will a temporary be created for the signed distances in the scope of `fm_residuals`

Yes.

> [@prittjam](#):
>
> will the result be directly written into `tst` at the scope of the caller?

No.

> [@prittjam](#):
>
> Does the behavior depend on whether `fm_residuals` is inlined?

No.

Function calls break loop fusion in broadcasting.

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [December 15, 2022, 1:51pm UTC](https://discourse.julialang.org/t/are-temporaries-created-in-one-line-functions-that-return-the-result-of-a-broadcasted-function/91701/4 "2022-12-15T13:51:34Z")

</div>

I thought that inlining might fuse them again. But apparently not.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 15, 2022, 1:53pm UTC](https://discourse.julialang.org/t/are-temporaries-created-in-one-line-functions-that-return-the-result-of-a-broadcasted-function/91701/5 "2022-12-15T13:53:04Z")

</div>

Broadcasting is, more or less, a syntactic transform (without the need for a macro, as it’s built into the language). Semantically, the call to `fm_residuals` has to complete doing its thing before the result can be used, so the loops can’t be fused. There is no automatic lazy evaluation of some sort going on there - julia is not a lazy language.

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [December 15, 2022, 2:07pm UTC](https://discourse.julialang.org/t/are-temporaries-created-in-one-line-functions-that-return-the-result-of-a-broadcasted-function/91701/6 "2022-12-15T14:07:43Z")

</div>

So for broadcasting, it seems that you cannot compose small functions efficiently; e.g.,

```julia
cost = sum(loss.(residual.(x,Ref(model))))

```

will not fuse the loss and residual functions. Should these be composed into one function so broadcasting is done efficiently? This disallows some flexibility in composition.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 15, 2022, 2:52pm UTC](https://discourse.julialang.org/t/are-temporaries-created-in-one-line-functions-that-return-the-result-of-a-broadcasted-function/91701/7 "2022-12-15T14:52:27Z")

</div>

> [@prittjam](#):
>
> will not fuse the loss and residual functions. Should these be composed into one function so broadcasting is done efficiently?

No, that is fused, since the broadcast is on the same syntactic level. This would not be

```julia
g(x, model) = residual.(x, Ref(model))
cost = sum(loss.(g(x, model))

```

because the call to `g` has to complete before `loss` can be broadcast over the result.

What I mean with “function calls break loop fusion” is that broadcasting cannot “look into” what a function that is called is doing and fuse loops with any broadcasts that may happen inside of that function. Broadcasting can only “see” what is immediately around it.

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [December 15, 2022, 2:55pm UTC](https://discourse.julialang.org/t/are-temporaries-created-in-one-line-functions-that-return-the-result-of-a-broadcasted-function/91701/8 "2022-12-15T14:55:44Z")

</div>

Thanks for the explanation. It’s clear now.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 15, 2022, 8:09pm UTC](https://discourse.julialang.org/t/are-temporaries-created-in-one-line-functions-that-return-the-result-of-a-broadcasted-function/91701/9 "2022-12-15T20:09:46Z")

</div>

> [@prittjam](#):
>
> `cost = sum(loss.(residual.(x,Ref(model))))`

Note that this allocates one array, you can call the method of `sum` that takes a function as the first argument to avoid the allocation completely. This is often applicable when you’re toimg reductions, like a sum. There is no need to allocate an arrays just to reduce it to a scalar.

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [December 15, 2022, 9:30pm UTC](https://discourse.julialang.org/t/are-temporaries-created-in-one-line-functions-that-return-the-result-of-a-broadcasted-function/91701/10 "2022-12-15T21:30:56Z")

</div>

Yes, I overlooked that allocation! Thanks.
