Solution for redefinition of functions being used in other functions?

question

#1

Hi,
I’ve searched around the old mailing lists, and I understand that this is a well-known issue, and that I think it is solved? for v 0.6, but for those still at 0.5, is there any solution/hack/advice?

For example, working with IJulia, I just want to plot a function

f(x) = tan(x) 
plot(f,-π,π)

then change the definition of f(x) and plot again. But plot will keep the old definition of f, despite f itself having changed. At present what I do is restart the IJulia kernel, but obviously this is very annoying if the notebook is anything longer than a toy example, because it takes a long time to restart it again.

Is there anything I can do to avoid restarting the kernel?

Thanks,
Ángel


#2

Check this FAQ entry and overall thread.


#3

It appears the change is too big to be backported to 0.5.1, so you have basically two options:

  1. use latest master,
  2. as a workaround for 0.5, organize your workflow into modules so that related functions recompile together.
    https://github.com/JuliaLang/julia/issues/265

#4

There is a simple solution: use anonymous functions:

f(x) = tan(x) 
plot(x -> f(x), -π, π)

f(x) = sin(x)
plot(x -> f(x), -π, π)

This works because each time you use an anonymous function, it creates a new function:

julia> f(x) = 3x
f (generic function with 1 method)

julia> x -> f(x)
(::#15) (generic function with 1 method)

julia> x -> f(x)
(::#17) (generic function with 1 method)

#5

Hi,

There is a simple solution: use anonymous functions:

f(x) = tan(x)
plot(x -> f(x), -π, π)

f(x) = sin(x)
plot(x -> f(x), -π, π)

this is a great workaround, thanks a lot!

This works because each time you use an anonymous function, it
creates a new function:

julia> f(x) = 3x
f (generic function with 1 method)

julia> x -> f(x)
(::#15) (generic function with 1 method)

julia> x -> f(x)
(::#17) (generic function with 1 method)

I assume unused anonymous functions will be cleaned at some point by the
GC?

Thanks a lot,
Ángel de Vicente


#6

Anything unreferenced becomes “garbage” and gets “recycled” at some point by the Garbage Collector. Creating anonymous functions all the time isn’t ideal (even though Julia has drastically decreased their cost). But it’s faster beyond any measure than restarting the kernel, and it’s fine to happen at the pace of a human hitting a button. After all, human time is more valuable than computer time (as long as the computer usage doesn’t come with a high cost itself).


#7

Can you provide a link to the part of the codebase where Julia calculates when an anonymous function has become unreferenced?


#8

I rarely follow the development of Julia at code-level. My closest memory on the matter is this conversation and things linked from there, which are a long reading, as anonymous functions have been an important and frequent topic, and their implementation has been a field for competitive approaches. I would be very interested in a more informed answer, as an unrecycled closure could be referencing other objects and affect performance.


#9

I had heard once, I believe it was from Jeff, that JITed code was not GCed (at least currently) in Julia.
Maybe after @vtjnash fixes #265 in v0.6, it will be possible to reclaim some of that memory.


#10

Thanks for the input. You are most probably right about both the “code” part and the “not yet” part. Similar situation with the unused Symbols. Of course those aren’t the kind of things people lose their sleep over (some other languages don’t even bother to get them right). And they are not design neglects postponed for the distant future, they are conscious intermediate choices planned to have their implementation fixed on later development stages, after more fundamental parts of the language get crystallized. It is the “data” part which causes the headaches and I haven’t yet run in a case where GC let me down on Julia 5.0. The developers have done a great job.

Still it’s sad that some of the involved people opt to treat such information like a privilege for the ones who have the time to read through tones of mixed low-level discussion (not to mention information hidden in unsearchable videos and papers). It can make the experience of people new to the language quite a challenge. Hopefully, the new discussion platform will help the rest of the community to gradually properly document things of any level in an organized way. Thanks @angelv for bringing that one in attention. By the way, I’ve included @dpsanders’ suggestion in the respective FAQ.