# Help to reduce very long compilation time

**URL:** <https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052>\
**Category:** General Usage\
**Created:** [February 1, 2020, 1:16am UTC](https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052 "2020-02-01T01:16:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 1, 2020, 1:16am UTC](https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052/1 "2020-02-01T01:16:09Z")

</div>

Hi!

I am building a package to create text user interfaces in Julia using ncurses: [TextUserInterfaces.jl](https://github.com/ronisbr/TextUserInterfaces.jl)

Now, I am facing a very, very slow compilation time. For example, the following code just initialize NCurses, create a window with a label, and destroys everything:

```julia
using TextUserInterfaces

# Create the TUI.
function test()
    # Initialize the TUI.
    init_tui("/usr/local/Cellar/ncurses/6.1/lib")
    NCurses.noecho()

    win = create_window(border = true, title = " WINDOW ",
                        anchor_left = (rootwin, :left, 0),
                        anchor_top = (rootwin, :top, 0),
                        anchor_right = (rootwin, :right, 0),
                        anchor_bottom = (rootwin, :middle, 0))

    create_widget(Val{:label}, win, text = "Top right",
                  anchor_right = (win, :right, 0),
                  anchor_top = (win, :top, 0))

    refresh_all_windows()
    NCurses.update_panels()
    NCurses.doupdate()

    destroy_tui()
end

test()

```

Take a look at the difference when I run it for the first and second times:

```julia
julia> @time include("teste6.jl")
  1.167181 seconds (3.41 M allocations: 172.807 MiB, 3.10% gc time)

julia> @time include("teste6.jl")
  0.024663 seconds (19.38 k allocations: 890.259 KiB)

```

For a very complex TUI, with lots of widgets, this time is almost prohibitive (taking more than 5s to show the TUI). Is there anything I can do to debug/reduce it?

EDIT: Just an additional information, putting the function inside a module did not solve the problem.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 1, 2020, 1:55am UTC](https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052/2 "2020-02-01T01:55:15Z")

</div>

Well, running julia with `-O0 --compile=min` reduce this A LOT:

```julia
julia> @time include("teste6.jl")
  0.377436 seconds (861.08 k allocations: 48.172 MiB, 1.64% gc time)

julia> @time include("teste6.jl")
  0.007598 seconds (2.80 k allocations: 116.903 KiB)

```

So I guess I am seeing the compile latency. Right?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [February 1, 2020, 3:20am UTC](https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052/3 "2020-02-01T03:20:52Z")

</div>

> [@Ronis\_BR](#):
>
> I am building a package to create text user interfaces in Julia using ncurses: [TextUserInterfaces.jl](https://github.com/ronisbr/TextUserInterfaces.jl)

Unrelated to your question, note that we have [`Ncurses_jll`](https://github.com/JuliaBinaryWrappers/Ncurses_jll.jl) which provides the library for all platforms.

---

<div class="post-metadata">

**Author:** ![Jakub\_Wronowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_wronowski/32/204030_2.png) [@Jakub\_Wronowski](https://discourse.julialang.org/u/Jakub_Wronowski)\
**Post date:** [February 1, 2020, 12:21pm UTC](https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052/4 "2020-02-01T12:21:25Z")

</div>

I had a look on your sources and you are using `Integer` abstract type a lot. Maybe it would compile faster if you used `Int` or `T<:Integer`?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 1, 2020, 12:34pm UTC](https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052/5 "2020-02-01T12:34:49Z")

</div>

> [@giordano](#):
>
> Unrelated to your question, note that we have [`Ncurses_jll`](https://github.com/JuliaBinaryWrappers/Ncurses_jll.jl) which provides the library for all platforms.

It is amazing! It will solve some problems. However, its seems to have a bug and I cannot load ncurses in macOS:

> <https://github.com/JuliaPackaging/Yggdrasil/issues/455>
>
> Hi!
> 
> For some reason, I cannot initialize Ncurses using \`Ncurses\_jll\` in macOS….
> 
> \`\`\`julia
> julia\> using Ncurses\_jll
> 
> julia\> ccall( (:initscr, libncurses), Cvoid, ())
> Error opening terminal: xterm-256color.
> \`\`\`
> 
> After that, julia crashes. Passing the env. option \`TERM=xterm\` did not help also.
> 
> I can start Ncurses normally if I use the library that ships with macOS or the library in home-brew.

> [@Jakub\_Wronowski](#):
>
> I had a look on your sources and you are using `Integer` abstract type a lot. Maybe it would compile faster if you used `Int` or `T<:Integer` ?

Thanks for the tip!! It helped to reduce the compilation time, even though it is still high:

```julia
# After changing everything to Int
julia> @time include("teste6.jl")
  0.976859 seconds (3.18 M allocations: 159.402 MiB, 3.28% gc time)

```

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 1, 2020, 10:28pm UTC](https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052/6 "2020-02-01T22:28:18Z")

</div>

After your tips, TextUserInterfaces.jl is playing really nice with Ncurses\_jll.jl! Since I do not have a windows machine to test, can you please tell me if there is any change to make Ncurses\_jll works on Windows?

Moreover, Ncurses\_jll.jl is working perfectly when building a compiled TUI using PackageCompiler.jl. In this case, all that compilation time is gone and the interface is opened very fast.

My only question is: with this setup, if `~/.julia` is not present, then the artifacts related to Ncurses are downloaded. Is there a way to bundle those artifacts together with the other libraries to avoid this download?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [February 1, 2020, 10:42pm UTC](https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052/7 "2020-02-01T22:42:30Z")

</div>

> [@Ronis\_BR](#):
>
> After your tips, TextUserInterfaces.jl is playing really nice with Ncurses\_jll.jl!

🎉

> [@Ronis\_BR](#):
>
> Since I do not have a windows machine to test, can you please tell me if there is any change to make Ncurses\_jll works on Windows?

No idea, I don’t use Windows either 🙂 What’s the problem?

> [@Ronis\_BR](#):
>
> My only question is: with this setup, if `~/.julia` is not present, then the artifacts related to Ncurses are downloaded. Is there a way to bundle those artifacts together with the other libraries to avoid this download?

I’m not sure what you want to achieve, could you please elaborate?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 1, 2020, 10:45pm UTC](https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052/8 "2020-02-01T22:45:36Z")

</div>

> [@giordano](#):
>
> No idea, I don’t use Windows either 🙂 What’s the problem?

I have never been able to run Ncurses in Windows. If you said it is working, then I would create a partition here with Window to test 🙂

> [@giordano](#):
>
> I’m not sure what you want to achieve, could you please elaborate?

The ultimate idea of TextUserInterfaces.jl is to ship compiled TUIs to accomplish some tasks. In this case, it will be nice if everything is bundled in one single folder without requiring internet access. I can, of course, manually copy ncurses libraries to the directory created by PackageCompiler.jl. I am just wondering if there is an easier way.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [February 1, 2020, 10:55pm UTC](https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052/9 "2020-02-01T22:55:41Z")

</div>

> [@Ronis\_BR](#):
>
> I have never been able to run Ncurses in Windows. If you said it is working, then I would create a partition here with Window to test 🙂

I usually just test binary packages for macOS and Windows with Travis or AppVeyor and see what happens. I only use GNU/Linux on my machine, that’s the only system I have direct access to.

> [@Ronis\_BR](#):
>
> The ultimate idea of TextUserInterfaces.jl is to ship compiled TUIs to accomplish some tasks. In this case, it will be nice if everything is bundled in one single folder without requiring internet access. I can, of course, manually copy ncurses libraries to the directory created by PackageCompiler.jl. I am just wondering if there is an easier way.

Not really, because the binaries are different for each of the 13 (and counting) currently supported platforms. Unless you’re going to package 13 different versions of your project, that doesn’t help much.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 2, 2020, 8:48am UTC](https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052/10 "2020-02-02T08:48:04Z")

</div>

> [@Ronis\_BR](#):
>
> The ultimate idea of TextUserInterfaces.jl is to ship compiled TUIs to accomplish some tasks. In this case, it will be nice if everything is bundled in one single folder without requiring internet access.

That is exactly the point of the `create_app` function in PackageCompilerX ([https://kristofferc.github.io/PackageCompilerX.jl/dev/apps/](https://kristofferc.github.io/PackageCompilerX.jl/dev/apps/)). It you use the artifact system, it will automatically copy all artifacts and set up depot directory etc so you can just ship the whole thing to another machine.

You can see an example at [https://github.com/Gnimuc/Redux.jl/tree/master/examples/App](https://github.com/Gnimuc/Redux.jl/tree/master/examples/App) which uses CImGu.jl.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 2, 2020, 10:56pm UTC](https://discourse.julialang.org/t/help-to-reduce-very-long-compilation-time/34052/11 "2020-02-02T22:56:11Z")

</div>

Thanks!! I really did not know anything about the PackageCompilerX.jl until now. It seems very good.
