I have been reading a fair amount (a few references at end of post) about how to “correctly”, or better yet idiomatically, structure code in Julia. In general, there seems to be a strong sentiment that many submodules probably means the code could be better split into several packages.
My question is: wouldn’t breaking code into submodules help in terms of precompilation? (assuming that most of the submodules are not constantly updated).
I should note that I don’t fully understand all the intricacies of JIT compilation. It’s therefore quite possible that my assumptions below are incorrect and in that case cool! no submodules for me.
My sense is that the reason to use Julia is to leverage these sort of subtleties, which is where the performance gains are achieved, so I want to make sure I’m doing it right
Example
Say I have a package MyPkg
:
Module MyPkg
include("A.jl")
include("B.jl")
end
#contents of A.jl
foo_a(x) = x + 2
[...]
#contents of B.jl
foo_b(x) = 2*fooA(x)
[...]
Now let’t assume that:
-
fooA
(and the contents ofA.jl
is unlikely to change very frequently, while - I’m still tweaking
fooB
and might even want to add a few more things. - Finally, the contents of
A.jl
have no real meaning on their own (Silly example above aside, maybe they set up some specialized Types for the calculations coming inB.jl
, are utility functions that are tailored to the functions inB.jl
, etc.)
My current understanding is that in the construction above any change in B.jl
would require precompilation of MyPkg
(upon restarting the REPL), which would also necessarily include the contents of A.jl
.
I could restructured the code as follows:
Module MyPkg
include("A.jl")
include("B.jl")
end
#contents of A.jl
Module A
export fooA
fooA(x) = x + 2
[...]
end
#contents of B.jl
Module B
using .A
fooB(x) = 2*fooA(x)
[...]
end
If I now make a change to B.jl
wouldn’t only MyPkg
and B
need to be recompiled, while the already precompiled A
can be used as is?
Some references
Beyond the Julia documentation (specifically Modules and Code Loading) and the Pkg documentation, there are also several interesting discussions on this forum about the question (many quite recent):
15 posts - 5 participants