# How to Bypass the World Age Problem

**URL:** <https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012>\
**Category:** General Usage\
**Tags:** world\_age\
**Created:** [November 11, 2017, 9:34pm UTC](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012 "2017-11-11T21:34:54Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [November 12, 2017, 8:05pm UTC](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012/21 "2017-11-12T20:05:04Z")

</div>

> [@yuyichao](#):
>
> Invokelatest is exactly what you were using before implicitly and is what you should use now.

Before now I was on Julia v0.5. The problem did not exist, so I don’t understand what you mean by this statement. The reality is that I _cannot_ use it because my code will be become so slow that it will be useless.

To be specific, if I translate the GIST linked above to v0.5 (basically just remove `invokelatest` in the last benchmark) then the runtime matches that of the reference.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 12, 2017, 8:24pm UTC](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012/22 "2017-11-12T20:24:08Z")

</div>

> [@cortner](#):
>
> The problem did not exist, so I don’t understand what you mean by this statement

The problem didn’t exist because the invokelastest was implicit and unpredictable.

> [@cortner](#):
>
> The reality is that I cannot use it because my code will be become so slow that it will be useless.
> 
> To be specific, if I translate the GIST linked above to v0.5 (basically just remove invokelatest in the last benchmark) then the runtime matches that of the reference.

I’ll repeat that `invokelastest` will never add overhead that’s not present with dynamic dispatch on 0.5 and the reason you can call that function on 0.5 at all is due to dynamic dispatch. If you do see a large overhead, you are not using it correctly.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [November 12, 2017, 8:42pm UTC](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012/23 "2017-11-12T20:42:03Z")

</div>

> [@yuyichao](#):
>
> I’ll repeat that invokelastest will never add overhead that’s not present with dynamic dispatch on 0.5 and the reason you can call that function on 0.5 at all is due to dynamic dispatch. If you do see a large overhead, you are not using it correctly.

I get that there is something I am not doing correctly. This is exactly what I am trying to find out in this thread. How do I fix the performance in the following code? (this is more involved than the first example, because it is much closer to my actual use case)

```Julia
using BenchmarkTools
using Calculus: differentiate
function Base.diff(ex::Expr)
   ex_d = differentiate(ex, :r)
   return eval( :( (r->$ex, r->$ex_d)) )
end
type AnalyticPotential{F0,F1}
   f::F0
   f_d::F1
end
function innertest(p::AnalyticPotential, N)
   s0 = 0.0
   s1 = 0.0
   for n = 1:N
      x = rand()
      s0 += p.f(x)
      s1 += p.f_d(x)
   end
end
Nruns = 1_000_000
a = 1.234
r0 = 0.9
function test(N)
   f, df = diff(:( exp(- $(a/r0) * r + $a) ) )
   p = AnalyticPotential( r->Base.invokelatest(f, r), r -> Base.invokelatest(df, r) )
   innertest(p, 10)
   @btime innertest($p, $N)
end
test(Nruns)

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 12, 2017, 10:03pm UTC](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012/24 "2017-11-12T22:03:49Z")

</div>

> [@cortner](#):
>
> AnalyticPotential( r-\>Base.invokelatest(f, r), r -\> Base.invokelatest(df, r) )

`Base.invokelatest(AnalyticPotential, f, df)`

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 12, 2017, 10:05pm UTC](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012/25 "2017-11-12T22:05:22Z")

</div>

Actually no, I didn’t realize the later ones is where you need it so

> [@cortner](#):
>
> innertest(p, 10)  
> @btime innertest($p, $N)

```julia
    Base.invokelatest(innertest, p, 10)
    @btime Base.invokelatest(innertest, $p, $N)

```

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [November 12, 2017, 10:32pm UTC](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012/26 "2017-11-12T22:32:16Z")

</div>

Well, that was embarrassingly easy. Somehow, this didn’t occur to me at all. Thank you for your patience.

So I should understand `invokelatest` as “please use dynamic dispatch for this call”?

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [November 12, 2017, 10:33pm UTC](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012/27 "2017-11-12T22:33:47Z")

</div>

would it be feasible to create a macro that applies this to an entire block of code?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 12, 2017, 10:39pm UTC](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012/28 "2017-11-12T22:39:06Z")

</div>

> [@cortner](#):
>
> So I should understand invokelatest as “please use dynamic dispatch for this call”?

Yes.

> [@cortner](#):
>
> would it be feasible to create a macro that applies this to an entire block of code?

It should be pretty easy to do…

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [November 13, 2017, 6:49pm UTC](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012/29 "2017-11-13T18:49:21Z")

</div>

FWIW inreimplemenred the functionality using macros and it works equally well if not better and invokelatest is ninlinger needed. Thank you for making that point.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 19, 2017, 3:47pm UTC](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012/30 "2017-11-19T15:47:27Z")

</div>

> [@cortner](#):
>
> So I should understand invokelatest as “please use dynamic dispatch for this call”?

Not exactly. Julia always uses dynamic dispatch when calling methods. But which “world” – i.e. global snapshot of available methods – is determined statically although the method within that world is chosen dynamically. So what `invokelatest` does is to choose a world dynamically rather than statically, using the latest world.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [November 19, 2017, 9:58pm UTC](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012/31 "2017-11-19T21:58:02Z")

</div>

Thank you for clarifying. It is a bit strange initially but one probably gets used to it over time.

[Previous page](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012.md?page=1)
