# Help reducing compilation time

**URL:** <https://discourse.julialang.org/t/help-reducing-compilation-time/77823>\
**Category:** General Usage\
**Created:** [March 13, 2022, 2:05pm UTC](https://discourse.julialang.org/t/help-reducing-compilation-time/77823 "2022-03-13T14:05:04Z")\
**Posts on this page:** 9\
**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:** [March 13, 2022, 2:05pm UTC](https://discourse.julialang.org/t/help-reducing-compilation-time/77823/1 "2022-03-13T14:05:04Z")

</div>

Hi!

I have the following function:

```julia
function textview(
    lines::Vector{T},
    view::NTuple{4, Int};
    active_highlight::String = _CSI * "30;43m",
    active_match::Int = 0,
    frozen_columns_at_beginning::Int = 0,
    frozen_lines_at_beginning::Int = 0,
    highlight::String = _CSI * "7m",
    maximum_number_of_columns::Int = -1,
    maximum_number_of_lines::Int = -1,
    parse_decorations_before_view::Bool = false,
    search_matches::Union{Nothing, Dict{Int, Vector{Tuple{Int, Int}}}} = nothing,
) where T<:AbstractString

```

I saw that the first call is taking 0.2s due to precompilation. I am trying to reduce it. I have already used SnoopCompile but I had minor gains, the precompilation is always happening when I first call the function. However, if I changed the signature to:

```julia
function textview(
    lines::Vector{String},
    view::NTuple{4, Int};
    active_highlight::String = _CSI * "30;43m",
    active_match::Int = 0,
    frozen_columns_at_beginning::Int = 0,
    frozen_lines_at_beginning::Int = 0,
    highlight::String = _CSI * "7m",
    maximum_number_of_columns::Int = -1,
    maximum_number_of_lines::Int = -1,
    parse_decorations_before_view::Bool = false,
    search_matches::Union{Nothing, Dict{Int, Vector{Tuple{Int, Int}}}} = nothing,
)

```

Then the first call take less than 10ms, meaning that the precompilation happened during the package build process.

Question: how can I do this using the first signature? I really need that first approach because it must work with `Vector{SubString{String}}`.

The information from SnoopCompile regarding inference time is:

```julia
julia> print_tree(tinf)
InferenceTimingNode: 0.192785/0.193579 on Core.Compiler.Timings.ROOT() with 1 direct children
└─ InferenceTimingNode: 0.000794/0.000794 on write(::IOBuffer, ::String, ::String, ::Vararg{String
}) with 0 direct children

```

---

<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:** [March 13, 2022, 2:44pm UTC](https://discourse.julialang.org/t/help-reducing-compilation-time/77823/2 "2022-03-13T14:44:33Z")

</div>

I reduced the problem to this line!

```julia
        write(line_buf, line)

```

If I change it to:

```julia
        write(line_buf, string(line))

```

Everything is MUCH faster. I just cannot tell why this is happening.

I solved by adding the following to the package:

```julia
        let
            io = IOBuffer()
            write(io, SubString("test", 1, 4))
        end

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 13, 2022, 7:03pm UTC](https://discourse.julialang.org/t/help-reducing-compilation-time/77823/3 "2022-03-13T19:03:12Z")

</div>

> [@Ronis\_BR](#):
>
> I just cannot tell why this is happening.

Shot in the dark: maybe JET.jl can tell you about type instabilities. And IIRC SnoopCompile has some support for JET.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:** [March 13, 2022, 7:21pm UTC](https://discourse.julialang.org/t/help-reducing-compilation-time/77823/4 "2022-03-13T19:21:48Z")

</div>

Thanks for the tip! I will search about it 🙂

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [March 13, 2022, 7:48pm UTC](https://discourse.julialang.org/t/help-reducing-compilation-time/77823/5 "2022-03-13T19:48:36Z")

</div>

The concrete typing of the callee seems excessive. The more likely problem, as suggested by @goerch, is an inference problem in the caller.

SnoopCompile does support JET, but it also allows analysis of inference problems in it’s own right. It is both more and less comprehensive than JET: the differences are clarified [in the first Note on this page](https://timholy.github.io/SnoopCompile.jl/stable/snoopi_deep_analysis/#inferrability). The short version is that JET is reproducible but only finds the first inference failure; SnoopCompile has to be run in a fresh session but finds all inference failures.

---

<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:** [March 13, 2022, 8:37pm UTC](https://discourse.julialang.org/t/help-reducing-compilation-time/77823/6 "2022-03-13T20:37:52Z")

</div>

Good! I will try to analyze this. Thanks you very much for the information @tim.holy !

---

<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:** [March 13, 2022, 8:49pm UTC](https://discourse.julialang.org/t/help-reducing-compilation-time/77823/7 "2022-03-13T20:49:49Z")

</div>

@goerch and @tim.holy ,

Thank you very much for pointing out JET.jl! Using it, I found I had this in a part of the code:

```julia
        code_i = tryparse(Int, tokens[i], base = 10)

```

which can return `nothing`. Hence, I add:

```julia
        code_i = tryparse(Int, tokens[i], base = 10)
        isnothing(code_i) && continue

```

and now everything is working beautifully 🙂

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 13, 2022, 8:56pm UTC](https://discourse.julialang.org/t/help-reducing-compilation-time/77823/8 "2022-03-13T20:56:09Z")

</div>

> [@Ronis\_BR](#):
>
> Thank you very much for pointing out JET.jl!

As usual thanks should go out to @aviatesk, likewise!

---

<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:** [March 13, 2022, 8:58pm UTC](https://discourse.julialang.org/t/help-reducing-compilation-time/77823/9 "2022-03-13T20:58:53Z")

</div>

Thanks @aviatesk for this amazing tool! 🙂
