Skip to content

Recompilation and load time #169

Closed
Closed

Description

@oschulz

According to @time and @time_imports, DocStringExtensions seems to recompile on every load, resulting in comparatively long load time (seems to vary a lot, but I've seen up to 75 ms). Now that very low level packages like SpecialFunctions depend on DocStringExtensions, this is becoming more noticeable. Is this inherent (recompilation) to the way DocStringExtensions works, or can we do something about this?

Activity

  1. MichaelHatherly commented on Aug 13, 2024

    @MichaelHatherly
    Member

    Is this inherent (recompilation) to the way DocStringExtensions works, or can we do something about this?

    We should be able to do something about it.

  2. oschulz commented on Aug 13, 2024

    @oschulz
    Author

    We should be able to do something about it.

    Merci!

  3. MichaelHatherly commented on Aug 13, 2024

    @MichaelHatherly
    Member

    I thought perhaps it was some invalidations causing recompilation since

    julia> @time_imports using DocStringExtensions
                   ┌ 0.0 ms DocStringExtensions.__init__() 
         15.2 ms  DocStringExtensions 84.18% compilation time
    

    But SnoopCompile isn't giving me anything useful right now :)

    julia> using SnoopCompileCore
    
    julia> invs = @snoop_invalidations using DocStringExtensions
    Any[]
    
  4. oscardssmith commented on Nov 20, 2024

    @oscardssmith

    I'm seeing much worse than this in some cases:

    julia> @time @time_imports using OrdinaryDiffEqFIRK
                   ┌ 0.0 ms DocStringExtensions.__init__() 
        174.0 ms  DocStringExtensions 99.30% compilation time
    
  5. MichaelHatherly commented on Nov 20, 2024

    @MichaelHatherly
    Member

    Any invalidations showing up in that particular case that can be traced back to DocStringExtensions?

  6. MichaelHatherly commented on Mar 25, 2025

    @MichaelHatherly
    Member

    I spent a little more time looking at this one @oschulz @oscardssmith. Not sure this is actually being caused by anything that DocStringExtensions is doing...

    First I commented out

    __init__() = (hook!(template_hook); nothing)
    completely, and it resolved the compilation issue:

    julia> @time_imports using DocStringExtensions
          2.2 ms  DocStringExtensions
    

    That's not surprising I would think.

    So then I just swapped the body of the __init__ with a noop:

    __init__ = nothing # (hook!(template_hook); nothing)

    That was under the assumption that it was calling hook! that was causing the trouble. But that resulted in

    julia> @time_imports using DocStringExtensions
                   ┌ 0.0 ms DocStringExtensions.__init__()
        119.5 ms  DocStringExtensions 97.81% compilation time
    

    Which, for me, is no different to the timings of the original __init__:

    julia> @time_imports using DocStringExtensions
                   ┌ 0.0 ms DocStringExtensions.__init__()
        117.6 ms  DocStringExtensions 97.64% compilation time
    

    Bit weird. So then I just made a MWE package with just

    module InitTest
    
    __init__() = nothing
    
    end # module InitTest

    And that resulted in the exact same behaviour:

    __init__ commented out:

    julia> @time_imports using InitTest
          1.0 ms  InitTest
    

    __init__ uncommented:

    julia> @time_imports using InitTest
                   ┌ 0.0 ms InitTest.__init__()
        120.0 ms  InitTest 98.78% compilation time
    

    To me this doesn't look like anything to do with this package, but rather a Julia issue. I'd be grateful if anyone else is able to replicate the above behaviour.

  7. oschulz commented on Mar 25, 2025

    @oschulz
    Author

    Maybe we need one of the compiler experts on this ...

  8. oscardssmith commented on Mar 25, 2025

    @oscardssmith

    Gabriel is looking into this. Current suspicion is that it might be an issue with the timing.

  9. KristofferC commented on Mar 25, 2025

    @KristofferC
    Member

    Edit: What is written below seems to only be true on macOS.

    This regressed in 1.11.3:

    ❯ julia +1.11.3 --project -q
    julia> @time_imports using InitTest
                   ┌ 0.0 ms InitTest.__init__() 
         63.8 ms  InitTest 98.75% compilation time
    
    ❯ julia +1.11.2 --project -q
    julia> @time_imports using InitTest
                   ┌ 0.0 ms InitTest.__init__() 
          1.1 ms  InitTest
    

    Looks fixed on JuliaLang/julia#57871, probably due to this: JuliaLang/julia@56909d7.

  10. MichaelHatherly commented on Mar 25, 2025

    @MichaelHatherly
    Member

    Thanks @KristofferC.

  11. KristofferC commented on Mar 25, 2025

    @KristofferC
    Member

    I am still not sure this is completely resolved.

  12. archermarx commented on Oct 16, 2025

    @archermarx

    Still hitting this on v1.12 on WSL

  13. MichaelHatherly commented on Oct 12, 2026

    @MichaelHatherly
    Member

    Revisiting this with a bit of claude assistance this looks like this is a problem with the actual timing measurement. Claude:

    For each package with an __init__, run_module_init in base/loading.jl calls print_time_imports_report_init to print the ┌ … __init__() line, and that function calls printstyled(...; color = :light_black). The kwcall specialisation behind it isn't in the sysimage, so it compiles the first time it runs. The print happens inside the parent package's load timer, so that compile time is charged to whichever package with an __init__ loads first. DocStringExtensions sits low in many dependency trees, so it usually loads first and gets the blame.

    Some additions to the base Julia precompiles would likely solve it, or maybe moving the printing outside of the timing window.

    I built a custom sysimage to verify it with an additional precompile for Base.print_time_imports_report_init(Base) and Base.print_time_imports_report(Base) to the tty_script generator and I saw the following kinds of numbers:

    • stock: 10–18 ms, 30–47% compilation
    • custom: about 4 ms, no compilation reported

    @KristofferC, you know that code better, does the above correctly explain it?

  14. KristofferC commented on Oct 12, 2026

    @KristofferC
    Member

    It seems plausible at least and Claude is rarely wrong :)

  15. KristofferC commented on Oct 12, 2026

    @KristofferC
    Member

    Claude:

    Yes, that explanation is correct. The printing of the __init__() line happens inside the parent package's timing window, and the first print compiles printstyled for the stdout type, which gets charged to the first package with an __init__. The existing precompile for this (JuliaLang/julia#55729) ran with stdout redirected to devnull so it didn't cover TTY.

    Fix in JuliaLang/julia#63755: the init lines are now printed after the package's timing is taken (so it is correct regardless of output type), and the TTY specializations are precompiled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions