JuliaLang / PrecompileTools.jl

Reduce time-to-first-execution of Julia code
MIT License
204 stars 11 forks source link

`@compile_workload` fails to compile some explicit toplevel calls #37

Open LilithHafner opened 5 months ago

LilithHafner commented 5 months ago

When I put setup in @compile_workload, it fails to compile later steps. I suspect this is due to inlining or otherwise treating multiple lines in the compile workflow step as a single operation.

Does not compile rand(::AliasTable)

PrecompileTools.@compile_workload begin
    at = AliasTable([1.0, 2.0])
    rand(at)
end

Does compile rand(::AliasTable)

at = AliasTable([1.0, 2.0])
PrecompileTools.@compile_workload begin
    rand(at)
end

Full example

x@fedora:~/.julia/dev/AliasTables$ cat src/AliasTables.jl 
module AliasTables

using Random
using PrecompileTools

export AliasTable

struct AliasTable{T}
    x::T
end
Base.rand(x::AliasTable) = rand(x.x)

at = AliasTable([1.0, 2.0])
PrecompileTools.@compile_workload begin
    rand(at)
end

end
x@fedora:~/.julia/dev/AliasTables$ julia --project -e '@time using AliasTables; @time at = AliasTable([1.0, 2.0]); @time rand(at)'
Precompiling AliasTables...
  1 dependency successfully precompiled in 1 seconds. 6 already precompiled.
  1.115552 seconds (599.80 k allocations: 35.161 MiB, 38.56% compilation time)
  0.000001 seconds (4 allocations: 112 bytes)
  0.000016 seconds (7 allocations: 320 bytes)
x@fedora:~/.julia/dev/AliasTables$ julia --project -e '@time using AliasTables; @time at = AliasTable([1.0, 2.0]); @time rand(at)'
  0.022336 seconds (37.92 k allocations: 2.467 MiB)
  0.000002 seconds (4 allocations: 112 bytes)
  0.000011 seconds (7 allocations: 320 bytes)
x@fedora:~/.julia/dev/AliasTables$ git checkout HEAD~
HEAD is now at 87e8d9c Broken
x@fedora:~/.julia/dev/AliasTables$ cat src/AliasTables.jl 
module AliasTables

using Random
using PrecompileTools

export AliasTable

struct AliasTable{T}
    x::T
end
Base.rand(x::AliasTable) = rand(x.x)

PrecompileTools.@compile_workload begin
    at = AliasTable([1.0, 2.0])
    rand(at)
end

end
x@fedora:~/.julia/dev/AliasTables$ julia --project -e '@time using AliasTables; @time at = AliasTable([1.0, 2.0]); @time rand(at)'
Precompiling AliasTables...
  1 dependency successfully precompiled in 1 seconds. 6 already precompiled.
  0.994406 seconds (598.94 k allocations: 35.113 MiB, 43.08% compilation time)
  0.000002 seconds (4 allocations: 112 bytes)
  0.002649 seconds (236 allocations: 11.422 KiB, 99.55% compilation time)
x@fedora:~/.julia/dev/AliasTables$ julia --project -e '@time using AliasTables; @time at = AliasTable([1.0, 2.0]); @time rand(at)'
  0.022320 seconds (37.92 k allocations: 2.467 MiB)
  0.000001 seconds (4 allocations: 112 bytes)
  0.002591 seconds (236 allocations: 11.422 KiB, 99.63% compilation time)

I ran into this example when attempting to use this package to set up precompilation for AliasTables.jl (https://github.com/LilithHafner/AliasTables.jl/pull/33)