# Small functions - best practices

**URL:** <https://discourse.julialang.org/t/small-functions-best-practices/54860>\
**Category:** New to Julia\
**Tags:** question, compilation, function\
**Created:** [February 8, 2021, 7:08pm UTC](https://discourse.julialang.org/t/small-functions-best-practices/54860 "2021-02-08T19:08:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matematija](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matematija/32/21820_2.png) [@Matematija](https://discourse.julialang.org/u/Matematija)\
**Post date:** [February 8, 2021, 7:08pm UTC](https://discourse.julialang.org/t/small-functions-best-practices/54860/1 "2021-02-08T19:08:01Z")

</div>

Hi all. First-time poster here.

I am writing some scientific code and would like to avoid unnecessary allocations. Specifically, I have a bunch of small functions (essentially one-liners) manipulating Arrays (representing tensors). This is all inner-loop stuff and I want it to be as lean as possible. As a super-simplified example:

```julia
linear(x::Vector{Float64}) = x'*W .+ b

```

My questions are:

1. From a performance standpoint, does it make sense to define

```julia
linear!(x::Vector{Float64}, res::Vector{Float64}) = (res .=x'*W .+ b)

```

or can I count on the compiler to save me from copy/pasting code by inlining the function or performing some type of return value optimization? Or, does it make sense to `@inline` this type of functions myself?

1. What are useful macros/tools for checking this kind of stuff? `@code_llvm`? Some thing from `BenchmarkTools.jl`? There’s a bunch of stuff out there but as a relative beginner and having no experience with reading lower-level code, I don’t know what/where to look for.

Thanks!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 8, 2021, 7:11pm UTC](https://discourse.julialang.org/t/small-functions-best-practices/54860/2 "2021-02-08T19:11:54Z")

</div>

> [@Matematija](#):
>
> can I count on the compiler to save me from copy/pasting code by inlining the function or performing some type of return value optimization? Or, does it make sense to `@inline` this type of functions myself?

if you read what the `?@inline` says:

> Give a hint to the compiler that this function is worth inlining.
> 
> Small functions typically do not need the @inline annotation, as the compiler does it automatically.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 8, 2021, 7:31pm UTC](https://discourse.julialang.org/t/small-functions-best-practices/54860/3 "2021-02-08T19:31:35Z")

</div>

The second will be slightly faster, but it is probably worth noting that `linear!(x, res) = (res .=x'*W .+ b)` will have exactly the same performance. Also, if `W` and `b` are non-const global variables, that will absolutely kill performance.

---

<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:** [February 8, 2021, 7:35pm UTC](https://discourse.julialang.org/t/small-functions-best-practices/54860/4 "2021-02-08T19:35:22Z")

</div>

To perform multiplication inplace and avoid an unnecessary copy, you need to call `mul!`, broadcasting `.=` is unfortunately not enough.

---

<div class="post-metadata">

**Author:** ![Matematija](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matematija/32/21820_2.png) [@Matematija](https://discourse.julialang.org/u/Matematija)\
**Post date:** [February 8, 2021, 7:55pm UTC](https://discourse.julialang.org/t/small-functions-best-practices/54860/5 "2021-02-08T19:55:29Z")

</div>

Didn’t know about `mul!`, that’s neat, thanks.

The fact that broadcasting is not enough wasn’t obvious to me. And that’s why I added the second question in the original post - what’s the easiest way to catch this kind of stuff?

---

<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:** [February 8, 2021, 8:04pm UTC](https://discourse.julialang.org/t/small-functions-best-practices/54860/6 "2021-02-08T20:04:46Z")

</div>

`*` is a binary operator that performs the multiplication, its a regular function call. Only dotted operations fuse under broadcast. I’m afraid I don’t know any other method of catching such allocations other than measuring them explicitly using, e.g. `@time`.  
You can also use `Meta.@lower` to see what an expression lowers to, I have a feeling it might reveal the temporary array, if not, `@code_typed` might
