# Annotating functions for analytic performance prediction

**URL:** <https://discourse.julialang.org/t/annotating-functions-for-analytic-performance-prediction/41485>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 16, 2020, 12:32am UTC](https://discourse.julialang.org/t/annotating-functions-for-analytic-performance-prediction/41485 "2020-06-16T00:32:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hros](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hros](https://discourse.julialang.org/u/hros)\
**Post date:** [June 16, 2020, 12:32am UTC](https://discourse.julialang.org/t/annotating-functions-for-analytic-performance-prediction/41485/1 "2020-06-16T00:32:11Z")

</div>

I would like to add function meta data and reflection to accomplish the following (I’ll use a simplistic scenario):

Say I building a big-data solutions (call it ‘g’) using a library of functions (‘f\_i’) )for performing: sort, set-intersection, disk-access, …  
Assume that the library has a few versions for each of these “primitive” functions, and that their performance depends on the data types, data size, data distribution, h/w (disk, network, cores) parameters, …

Knowing this dependence, I would like to annotate each of the primitive functions with their known “performance dependence” function, and using some sort of reflection on my function (‘g’) be able to select the library functions the optimize my performance criteria

This example might seem a bit contrived, but my actual use case (which is more complicated to describe) would greatly benefit from such a feature (hopefully without becoming a Julia parsing guru)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 16, 2020, 12:33am UTC](https://discourse.julialang.org/t/annotating-functions-for-analytic-performance-prediction/41485/2 "2020-06-16T00:33:36Z")

</div>

Maybe use a Dict?

```julia
julia> d = Dict(sin => :fast, cos => :slow)
Dict{Function,Symbol} with 2 entries:
  cos => :slow
  sin => :fast

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 16, 2020, 12:36am UTC](https://discourse.julialang.org/t/annotating-functions-for-analytic-performance-prediction/41485/3 "2020-06-16T00:36:56Z")

</div>

Actually, you can profitably use a simple version of traits here:

```julia
julia> struct Fast end

julia> struct Slow end

julia> run(f::Fast) = "fast!"

julia> run(f::Slow) = "slow!"

julia> d = Dict(sin => Fast(), cos => Slow())
Dict{Function,Any} with 2 entries:
  cos => Slow()
  sin => Fast()

julia> run(f) = run(d[f])
run (generic function with 3 methods)

julia> run(cos)
"slow!"

julia> run(sin)
"fast!"

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 16, 2020, 12:40am UTC](https://discourse.julialang.org/t/annotating-functions-for-analytic-performance-prediction/41485/4 "2020-06-16T00:40:01Z")

</div>

OK that wasn’t really traits.

```julia

julia> annotate(::typeof(sin)) = Fast()
annotate (generic function with 1 method)

julia> run(f) = run(annotate(f))
run (generic function with 3 methods)

```

---

<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:** [June 16, 2020, 1:31am UTC](https://discourse.julialang.org/t/annotating-functions-for-analytic-performance-prediction/41485/5 "2020-06-16T01:31:29Z")

</div>

I’ve thought about this for a bit. I think it would be really interesting to have an extension to multiple dispatch that allowed methods to be picked via a simple cost function. The details, of course would be really complicated to implement, but if successful, it would simplify lots of the sparse linear algebra generic fallbacks issues.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 16, 2020, 2:00pm UTC](https://discourse.julialang.org/t/annotating-functions-for-analytic-performance-prediction/41485/6 "2020-06-16T14:00:01Z")

</div>

> [@Oscar\_Smith](#):
>
> I think it would be really interesting to have an extension to multiple dispatch that allowed methods to be picked via a simple cost function.

I may not understand the specs fully, but I think that traits are the perfect solution here. See eg [StaticArrays.jl which uses a `Size` trait](https://github.com/JuliaArrays/StaticArrays.jl/blob/94ce5e25f4f79d7c11825e19fd5953bf134717c0/src/det.jl#L64) (or similar) in quite a few places to fall back to generic linear algebra methods for arrays above a certain size.

---

<div class="post-metadata">

**Author:** ![hros](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hros](https://discourse.julialang.org/u/hros)\
**Post date:** [June 17, 2020, 5:57pm UTC](https://discourse.julialang.org/t/annotating-functions-for-analytic-performance-prediction/41485/7 "2020-06-17T17:57:14Z")

</div>

Thanks for the suggestions  
I am looking for a dynamic analysis capability rather than automatic dispatching  
I would like take a function, and “reflect” on it (including loops and all…) and discover how many times each of the library functions is called and with what parameters, so that I can output both an estimate of expected resource usage as well as identify if the “contracts” of the library functions are met (e.g. maintaining a security parameter when calling a sequence of cryptographic primitives)
