# Understanding allocs/constant propagation issue?

**URL:** https://discourse.julialang.org/t/understanding-allocs-constant-propagation-issue/92474
**Category:** General Usage
**Created:** [January 4, 2023, 12:16am UTC](https://discourse.julialang.org/t/understanding-allocs-constant-propagation-issue/92474 "2023-01-04T00:16:03Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Egwene\_al\_Vere](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@Egwene\_al\_Vere](https://discourse.julialang.org/u/Egwene_al_Vere)
#### Post date: [January 4, 2023, 12:16am UTC](https://discourse.julialang.org/t/understanding-allocs-constant-propagation-issue/92474/1 "2023-01-04T00:16:03Z")

</div>

Consider the example,

```julia
using LinearAlgebra, BenchmarkTools

function comu_f!(r, a, b, p, s)
    mul!(r, a, b, s, p)
    mul!(r, b, a, -s, true)
    return nothing
end

m1, m2, m3 = [rand(ComplexF64, 10, 10) for _ in 1:3]

function txx(r, a, b)
    p = false
    s = 2.0
    comu_f!(r, a, b, p, s)
end

function txx2(r, a, b)
    p = false
    s = 2.0
    mul!(r, a, b, s, p)
    mul!(r, b, a, -s, true)
    nothing
end

```

(In real use, p and s would change, and is in a inner loop so I’d like to reduce allocations.)

`@btime txx($m1, $m2, $m3)`  
shows

> 757.120 ns (1 allocation: 32 bytes)

while `@btime txx2($m1, $m2, $m3)` shows

> 745.260 ns (0 allocations: 0 bytes)

Since `txx2` just copy-paste the code, why does `txx` allocates while the mul! call should not? Thanks!

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [January 4, 2023, 12:36am UTC](https://discourse.julialang.org/t/understanding-allocs-constant-propagation-issue/92474/2 "2023-01-04T00:36:15Z")

</div>

I don’t think the constants get propagated through to the function call. In general (someone correct me if I’m wrong), you only really can rely on type information to ensure proper constant propagation through functions. An example to fix the performance:

```julia
function comu_f2!(r,a,b,::Val{p},::Val{s}) where {p,s}
    mul!(r, a, b, s, p)
    mul!(r, b, a, -s, true)
    nothing
end

```

Using the `Val` lets you encode values into the type itself to be available at compile time.

You can change the function to

```julia
function txx3(r, a, b)                                       
    p = false
    s = 2.0
    comu_f2!(r, a, b, Val(p), Val(s))
end

```

For me, this gives basically the same performance as `txx2`.

I think the reason for doing this is to make sure that the `comu_f2` function can be reused for the same types (i.e. be general) without having to recompile for different constants every time. I am sure there is a macro which lets you inline the function and have the constant propagation but I’m not sure.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 4, 2023, 12:44am UTC](https://discourse.julialang.org/t/understanding-allocs-constant-propagation-issue/92474/3 "2023-01-04T00:44:14Z")

</div>

> [@jmair](#):
>
> I am sure there is a macro which lets you inline the function

```julia
@inline function ...

```

May work, but constant propagation is not guaranteed in any case.

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [January 4, 2023, 12:49am UTC](https://discourse.julialang.org/t/understanding-allocs-constant-propagation-issue/92474/4 "2023-01-04T00:49:59Z")

</div>

I did try this but it didn’t work with the constant propagation.

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [January 4, 2023, 1:30am UTC](https://discourse.julialang.org/t/understanding-allocs-constant-propagation-issue/92474/5 "2023-01-04T01:30:12Z")

</div>

Try this?

```julia
Base.@constprop :aggressive function comu_f!(r, a, b, p, s) ...

```

---

<div class="post-metadata">

### Author: ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)
#### Post date: [January 4, 2023, 7:57am UTC](https://discourse.julialang.org/t/understanding-allocs-constant-propagation-issue/92474/6 "2023-01-04T07:57:34Z")

</div>

> [@jmair](#):
>
> Using the `Val` lets you encode values into the type itself to be available at compile time.

I think this only helps if the dispatch values are compile time constants, like you said.

OP mentioned that in his real application `p, s` should change too. I think I read somewhere that this will then also involve dynamic dispatch (due to the type `Val{p}` being dynamic then) and, thus, will also gain overhead from that.  
Or can const-prop eliminate that too?

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [January 4, 2023, 8:06am UTC](https://discourse.julialang.org/t/understanding-allocs-constant-propagation-issue/92474/7 "2023-01-04T08:06:56Z")

</div>

This works great, I hadn’t seen this macro before - thanks!

---

<div class="post-metadata">

### Author: ![Egwene\_al\_Vere](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@Egwene\_al\_Vere](https://discourse.julialang.org/u/Egwene_al_Vere)
#### Post date: [January 4, 2023, 2:30pm UTC](https://discourse.julialang.org/t/understanding-allocs-constant-propagation-issue/92474/8 "2023-01-04T14:30:17Z")

</div>

Thanks! The `Base.@constprop :aggressive` macro seems to work, nice to know this exists!
