# Does adding dependencies increase compilation time?

**URL:** <https://discourse.julialang.org/t/does-adding-dependencies-increase-compilation-time/39766>\
**Category:** General Usage\
**Created:** [May 19, 2020, 5:41pm UTC](https://discourse.julialang.org/t/does-adding-dependencies-increase-compilation-time/39766 "2020-05-19T17:41:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrucePBon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brucepbon/32/11936_2.png) [@BrucePBon](https://discourse.julialang.org/u/BrucePBon)\
**Post date:** [May 19, 2020, 5:41pm UTC](https://discourse.julialang.org/t/does-adding-dependencies-increase-compilation-time/39766/1 "2020-05-19T17:41:31Z")

</div>

I am writing a Julia Package and I feel like compilation time of the package is longer than It was in the past. Does it have to do with the added dependencies?

Should I only import those functions that my package uses? Instead of `using PyPlot`, should I write `import PyPlot: imshow`.

I tried changing all `using`’s for `import`’s in my code and I ended up with conflicts between `Gadfly.plot` and `Plots.plot`. In these cases, the second imported function is just ignored.

I though that I could create a “minimal module” for each dependency, and import within these modules, to avoid conflicts in Main.

```nohighlight
module minGadfly
    import Gadfly: plot
end

module minPlots
    import Plots: plot
end

```

Does any of this make any sense? I wanted to ask before committing to it.

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [May 19, 2020, 6:06pm UTC](https://discourse.julialang.org/t/does-adding-dependencies-increase-compilation-time/39766/2 "2020-05-19T18:06:44Z")

</div>

Short answer: yes, adding dependencies will increase compilation time.

Unfortunately, trying to import only individual functions from a module won’t help. So doing `import PyPlot: imshow` won’t be any faster than `using PyPlot`. Folks with more knowledge of the compiler could perhaps give a more detailed answer but I believe it just boils down to the fact that a function in a given module might call other functions in that same module and it’s hard determine what exactly to compile when do `using Foo: bar`

However, it’s important to point out that I’m using the term “compile” pretty loosely above. The last paragraph is about what happens when you do `using Foo` or `import Foo`. That includes things like parsing of source code and lowering to an internal intermediate format but not the full pass through LLVM to generate machine code. When you actually call a function at runtime with concrete inputs, then julia can fully compile that function for the given input arguments. I don’t think that bit should be affected by adding extra dependencies.

On a final note, there’s no performance difference between `using` and `import`. The difference is just in how they bring names into scope.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 19, 2020, 6:45pm UTC](https://discourse.julialang.org/t/does-adding-dependencies-increase-compilation-time/39766/3 "2020-05-19T18:45:30Z")

</div>

> [@BrucePBon](#):
>
> I though that I could create a “minimal module” for each dependency, and import within these modules, to avoid conflicts in Main.

I don’t think there’s any reason to expect this to have a performance impact.

You might, however, find the new `@optlevel` macro in Julia 1.5 useful: [https://github.com/JuliaLang/julia/blob/master/HISTORY.md#new-language-features](https://github.com/JuliaLang/julia/blob/master/HISTORY.md#new-language-features)

---

<div class="post-metadata">

**Author:** ![BrucePBon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brucepbon/32/11936_2.png) [@BrucePBon](https://discourse.julialang.org/u/BrucePBon)\
**Post date:** [May 20, 2020, 10:20am UTC](https://discourse.julialang.org/t/does-adding-dependencies-increase-compilation-time/39766/4 "2020-05-20T10:20:46Z")

</div>

Thanks for the answer! I don’t know that much about the compilation details of importing. However, I have tested that doing `import Gadfly: plot` does not suffice for running `Gadfly.plot( Coord.Cartesian( xmin=0, xmax=1 ), more_arguments... )`, because `Gadfly.Coord` needs to be imported explicitly. The compiler does not do it by itself.

Therefore, it seems that the compiler does not necessarily try to resolve classes that are used by the imported function. Your point about functions in a module calling other functions still stands, I don’t know how the compiler deals with this.

---

<div class="post-metadata">

**Author:** ![BrucePBon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brucepbon/32/11936_2.png) [@BrucePBon](https://discourse.julialang.org/u/BrucePBon)\
**Post date:** [May 20, 2020, 10:22am UTC](https://discourse.julialang.org/t/does-adding-dependencies-increase-compilation-time/39766/5 "2020-05-20T10:22:14Z")

</div>

Thanks for the answer and the reference to @optlevel. I am still in Julia1.3 though, it’s a hard to keep up with the version updates 😅

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [May 20, 2020, 3:49pm UTC](https://discourse.julialang.org/t/does-adding-dependencies-increase-compilation-time/39766/6 "2020-05-20T15:49:18Z")

</div>

> [@BrucePBon](#):
>
> I have tested that doing `import Gadfly: plot` does not suffice for running `Gadfly.plot( Coord.Cartesian( xmin=0, xmax=1 ), more_arguments... )` , because `Gadfly.Coord` needs to be imported explicitly. The compiler does not do it by itself.

This is just a matter of `Gadfly.Coord` not being brought into scope. It’s orthogonal to how much work is being done by the `using` and `import` statements. I don’t have Gadfly installed right now but here’s an experiment you can do to demonstrate this:

```julia
@time import Gadfly: plot
@time import Gadfly.Coord: Cartesian

```

then restart julia and run the same two lines in reverse order, you should find that both times, the first line to run takes about the same amount of time and is much slower than the second.

---

<div class="post-metadata">

**Author:** ![BrucePBon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brucepbon/32/11936_2.png) [@BrucePBon](https://discourse.julialang.org/u/BrucePBon)\
**Post date:** [May 20, 2020, 4:26pm UTC](https://discourse.julialang.org/t/does-adding-dependencies-increase-compilation-time/39766/7 "2020-05-20T16:26:37Z")

</div>

I see, I have a better idea now of what goes on under the hood ( and what doesn’t ). thanks

the experiment conforms to your expectations, by the way: 10s for the first import and 1s for the second, regardless of the imported members.
