Skip to content

Precompiling a function which spawns a Task does not precompile the Task's function #58818

Open
@NHDaly

Description

@NHDaly

Precompiling a function which spawns a Task does not precompile the Task's function.

Normally, precompiling a function will also compile all statically reachable functions it could call. However, since the function itself doesn't invoke the anonymous function it creates for a Task it spawns, that function is not compiled.

Consider this small MRE ran with --trace-compile=stderr:

julia> bar() = println("HI")
bar (generic function with 1 method)

julia> foo() = Threads.@spawn bar()
foo (generic function with 1 method)

julia> precompile(foo, ())
#=    6.6 =# precompile(Tuple{typeof(Main.foo)})
true

# Note that this _could_ have also compiled the lambda #foo##4#foo##5 which calls 
# bar(), but it did not.

# Now, actually _calling_ the function, which causes the Task to get scheduled,
# does precompile that lambda when the Task is first executed:

julia> foo()
#=    4.6 =# precompile(Tuple{Main.var"#foo##4#foo##5"})
HI
Task (done) @0x00000001662fb2d0

This makes it difficult to precompile such functions during the Package Precompilation step, without actually running a snoop workload.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler:precompilationPrecompilation of modulesmultithreadingBase.Threads and related functionalitytrimmingIssues with trimming functionality or PR's relevant to its performance/functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions