# Difference between \`Base.@assume\_effects :foldable\` and \`Base.@constprop :aggressive\`

**URL:** https://discourse.julialang.org/t/difference-between-base-assume-effects-foldable-and-base-constprop-aggressive/118846
**Category:** General Usage
**Tags:** constant-propagation, effects
**Created:** [August 31, 2024, 2:38pm UTC](https://discourse.julialang.org/t/difference-between-base-assume-effects-foldable-and-base-constprop-aggressive/118846 "2024-08-31T14:38:13Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [August 31, 2024, 2:38pm UTC](https://discourse.julialang.org/t/difference-between-base-assume-effects-foldable-and-base-constprop-aggressive/118846/1 "2024-08-31T14:38:13Z")

</div>

What’s the difference between `Base.@assume_effects :foldable` and `Base.@constprop :aggressive`?

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [August 31, 2024, 2:49pm UTC](https://discourse.julialang.org/t/difference-between-base-assume-effects-foldable-and-base-constprop-aggressive/118846/2 "2024-08-31T14:49:11Z")

</div>

I am in no way an expert of the details of what they actually mean in terms of compiler optimization so this may not be a helpful answer, but at least on the axis of dangerousness of misuse I believe the former is a _promise_ to the compiler (misuse can cause bugs and UB) whereas the latter is a _suggestion_ (misuse can cause suboptimal performance or compile times)

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [August 31, 2024, 2:53pm UTC](https://discourse.julialang.org/t/difference-between-base-assume-effects-foldable-and-base-constprop-aggressive/118846/3 "2024-08-31T14:53:16Z")

</div>

Another notable difference is that I’ve literally never seen a performance problem that was successfully solved with `@costprop :aggressive`. I’m sure examples exist, but I’ve never seen one.

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [August 31, 2024, 3:12pm UTC](https://discourse.julialang.org/t/difference-between-base-assume-effects-foldable-and-base-constprop-aggressive/118846/4 "2024-08-31T15:12:06Z")

</div>

For context, my application involves recursive functions that should compile away (the value of a function call is determined by the types of the arguments). I’ll give `Base.@constprop :aggressive` a try and see if it works. (At least I’m not using `Base.@assume_effects :total` anymore. 😂)

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [August 31, 2024, 3:28pm UTC](https://discourse.julialang.org/t/difference-between-base-assume-effects-foldable-and-base-constprop-aggressive/118846/5 "2024-08-31T15:28:02Z")

</div>

The problem (as far as I understand) with `@costprop :aggressive` is that it basically just tells the compiler to try a little harder to figure out if costant propagation is legal/viable somewhere.

But it’s almost never the case that just trying a little harder will solve the problem. Usually, there’s a deeper problem causing the compiler to fail to figure out if it can constprop, or it’s going to require an order of magnitude more work to analyze, not just a little more work.

Even worse though, even if the macro did end up helping you cross some threshold so that constprop succeeds, any small changes to the compiler, or the wider compilation context of the function in question could push you back into the regime where the compiler gives up.

So it’s just generally a really brittle and unreliable tool (maybe even useless. If people know of any examples where it actually helped, I’d be quite curious to know)

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [August 31, 2024, 3:39pm UTC](https://discourse.julialang.org/t/difference-between-base-assume-effects-foldable-and-base-constprop-aggressive/118846/6 "2024-08-31T15:39:59Z")

</div>

> [@CameronBieganek](#):
>
> What’s the difference between `Base.@assume_effects :foldable` and `Base.@constprop :aggressive`?

The `foldable` effect convinces the compiler that constant folding is safe (among other things, as explained in the documentation). `@constprop :aggressive` suggests to the compiler to try a bit harder for constant propagation.

NB: constant folding and constant propagation are not the same thing. AFAIK constant folding is when all arguments of a call are constant, so the entire value of the call may be computed at compile time. Constant propagation propagates the value of only _some_ arguments, because not all of them are known at compile time.

> [@CameronBieganek](#):
>
> my application involves recursive functions

FTR, while constant folding works fine for recursion (once the `foldable` effect is known), constant propagation is disabled for recursion, see:

> <https://github.com/JuliaLang/julia/issues/55100>
>
> You can do some really cool things if you can opt in to recursive constant propa…gation: see \[this gist\](https://gist.github.com/olynch/4d8c63f1ccf3c6b31a71191c7f943c71). In that gist, I run Core.Compiler.eval on a new definition of \`abstract\_call\_method\` that simply disables the recursion check. Obviously, this is not a good idea to do globally, but if I could do something like \`@constprop :aggressive\_recursive\` to enable this behavior, that would be awesome.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [August 31, 2024, 3:41pm UTC](https://discourse.julialang.org/t/difference-between-base-assume-effects-foldable-and-base-constprop-aggressive/118846/7 "2024-08-31T15:41:08Z")

</div>

> [@Mason](#):
>
> If people know of any examples where it actually helped, I’d be quite curious to know)

There were PRs to LinearAlgebra by @jishnub where it was useful.

[https://github.com/JuliaLang/julia/pulls?q=is%3Apr+aggressive+constprop+is%3Amerged+](https://github.com/JuliaLang/julia/pulls?q=is%3Apr+aggressive+constprop+is%3Amerged+)

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [August 31, 2024, 5:21pm UTC](https://discourse.julialang.org/t/difference-between-base-assume-effects-foldable-and-base-constprop-aggressive/118846/8 "2024-08-31T17:21:17Z")

</div>

In general, constant propagation is quite useful in eliminating branches, and may help with type-stability as a consequence.
