# How does constant propagation impact latency with a Val-based dispatch?

**URL:** <https://discourse.julialang.org/t/how-does-constant-propagation-impact-latency-with-a-val-based-dispatch/114545>\
**Category:** Performance\
**Tags:** compilation, constant-propagation\
**Created:** [May 22, 2024, 5:49am UTC](https://discourse.julialang.org/t/how-does-constant-propagation-impact-latency-with-a-val-based-dispatch/114545 "2024-05-22T05:49:57Z")\
**Posts on this page:** 1\
**Page:** 1

<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:** [May 22, 2024, 5:49am UTC](https://discourse.julialang.org/t/how-does-constant-propagation-impact-latency-with-a-val-based-dispatch/114545/1 "2024-05-22T05:49:57Z")

</div>

The example here is schematic, and isn’t really an MWE.

Suppose I have a set of functions like

```julia
f() = f('A')
Base.@constprop :aggressive f(a, args...) = g(Val(a == 'A'), args...)
Base.@constprop :aggressive g(::Val{true}, args...) = 1
Base.@constprop :aggressive g(::Val{false}, args...) = 2

```

In this case, `code_typed` for `f()` detects that `g(::Val{false})` is unreachable, and inlines `g(::Val{true})`, to produce

```julia
julia> @code_typed f()
CodeInfo(
1 ─ return 1
) => Int64

```

However, the first call still seems to propagate constants into `g(::Val{false})`. In my use case, propagating constants into `g(::Val{false})` is necessary for type-inference within the method, but this adds quite a bit to the latency of `f()`. I would love it if the method was entirely ignored in this call, as it is not being compiled subsequently.

Simplifying the method definition of `g(::Val{false}, args...)` appears to reduce TTFX for `f()`, which I had not expected, as this method is not being used subsequently in the call.

The actual use case that I’m looking at is in matmul, where the latency in `generic_matmul!`

> <https://github.com/JuliaLang/julia/blob/263928f9ad450a28d601c3f185040987e0bc45d5/stdlib/LinearAlgebra/src/matmul.jl#L372-L418>

may be reduced considerably if the last call

> <https://github.com/JuliaLang/julia/blob/263928f9ad450a28d601c3f185040987e0bc45d5/stdlib/LinearAlgebra/src/matmul.jl#L417>

on line 417 were absent. This line is not reachable in many common cases, but still contributes to the latency of matrix multiplication. The constant-propagation into `wrap` appears to lead to this latency, and removing the aggressive constprop annotations reduces TTFX. Ideally, I would only want this constprop to happen if the line is reachable.

The connection with a `Val`-based dispatch is that I was trying to hide this line behind a `Val(all(map(in(('N', 'T', 'C')), (tA_uc, tB_uc)))`, which may be evaluated at compile-time, but doesn’t seem to impact latency.
