# Dispatching by values is very slow - Feature or Problem?

**URL:** <https://discourse.julialang.org/t/dispatching-by-values-is-very-slow-feature-or-problem/65586>\
**Category:** Performance\
**Tags:** dispatch\
**Created:** [July 31, 2021, 8:33am UTC](https://discourse.julialang.org/t/dispatching-by-values-is-very-slow-feature-or-problem/65586 "2021-07-31T08:33:27Z")\
**Posts on this page:** 1\
**Showing post:** 7

<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:** [July 31, 2021, 12:01pm UTC](https://discourse.julialang.org/t/dispatching-by-values-is-very-slow-feature-or-problem/65586/7 "2021-07-31T12:01:40Z")

</div>

> [@viraltux](#):
>
> `BenchmarkTools` is not really there when it comes to compare functions

that’s literally the only purpose it serves… (comparing 2 versions of function is same idea)

> [@viraltux](#):
>
> why dispatching by value slow things down so much and if in general we should avoid it when performance is critical.

there’s really a ton of discussion on this, and the answer is yes, don’t use value as types but also don’t be afraid to write branch if you need to, sometimes constant folding gives you a ride:

> [@Dispatch on Value allocating](https://discourse.julialang.org/t/dispatch-on-value-allocating/26337/9):
>
> Yes, it generally will. julia\> f() = ntuple(x -\> x + 5, Val(2)) f (generic function with 2 methods) julia\> @code\_llvm f() ; @ REPL[45]:1 within `f' define void @julia_f_12684([2 x i64]* noalias nocapture sret) { top: %1 = bitcast [2 x i64]* %0 to i8* call void @llvm.memcpy.p0i8.p0i8.i64(i8* %1, i8* bitcast ([2 x i64]* @0 to i8*), i64 16, i32 8, i1 false) ret void } julia\> @code_native f() .section __TEXT,__ text,regular,pure_instructions ; ┌ @ REPL[49]:1 within `f' …

> [@Dispatch on argument values rather than (just) types?](https://discourse.julialang.org/t/dispatch-on-argument-values-rather-than-just-types/7974/4):
>
> In general, type computations in Julia are made for things that should be done at compilation time. If you cannot do the calculation at compilation time, then you will get performance hits by using the dispatch system (but if you can do things at compilation time, then you will get performance gains!). So it’s a tool to be used wisely. In this case, it sounds like you want to dispatch on runtime values. This means you are in the case where you will get performance losses. From what I measured …

---

_[View the full topic](https://discourse.julialang.org/t/dispatching-by-values-is-very-slow-feature-or-problem/65586)._
