rdeits / MeshCat.jl

WebGL-based 3D visualizer in Julia
MIT License
233 stars 42 forks source link

Add PrecompileTools.jl dependency and method to close server #250

Closed ferrolho closed 6 months ago

ferrolho commented 8 months ago

What?

This PR is for adding PrecompileTools.jl as a project dependency. It also adds a new method, close_server!, that allows us to close previously-opened servers (along with some minor refactoring to allow it).

Why?

Because downstream packages that use MeshCat.jl (e.g., TORA.jl) may open a Visualiser during pre-compilation, and from Julia 1.10 onwards, any opened plots/servers/streams need to be closed, otherwise precompile will halt for forever.

Reduce TTFX

Using PrecompileTools.jl also allows us to "exercise" parts of MeshCat.jl during pre-compilation in order to reduce the time to first execution (TTFX) in a Julia session. The trade-off for this change is an increased pre-compilation time, proportional to the amount of work done in the "exercising" of the package.

For example, the src/precompile.jl I'm proposing currently looks like this:

import PrecompileTools

PrecompileTools.@compile_workload begin
    vis = Visualizer()
    close_server!(vis.core)
end

Here's what it changes:

Before

julia> @time using MeshCat
  0.597913 seconds (793.05 k allocations: 56.442 MiB, 3.40% gc time, 2.87% compilation time: 44% of which was recompilation)

julia> @time vis = Visualizer()
[ Info: Listening on: 127.0.0.1:8700, thread id: 1
┌ Info: MeshCat server started. You can open the visualizer by visiting the following URL in your browser:
└ http://127.0.0.1:8700
  0.314570 seconds (1.03 M allocations: 71.585 MiB, 2.92% gc time, 99.75% compilation time: <1% of which was recompilation)
MeshCat Visualizer with path /meshcat at http://127.0.0.1:8700

After

julia> @time using MeshCat
  0.600892 seconds (809.84 k allocations: 57.327 MiB, 2.98% gc time, 2.79% compilation time: 44% of which was recompilation)

julia> @time vis = Visualizer()
[ Info: Listening on: 127.0.0.1:8700, thread id: 1
┌ Info: MeshCat server started. You can open the visualizer by visiting the following URL in your browser:
└ http://127.0.0.1:8700
  0.006109 seconds (3.84 k allocations: 267.945 KiB, 91.38% compilation time: 56% of which was recompilation)
MeshCat Visualizer with path /meshcat at http://127.0.0.1:8700
ferrolho commented 8 months ago

Converted PR to a draft because there is something wrong, and the tests are not succeeding locally for some reason (even though GitHub CI did not complain).

ferrolho commented 8 months ago

Converted PR to a draft because there is something wrong, and the tests are not succeeding locally for some reason (even though GitHub CI did not complain).

The issue has been fixed, and the tests are passing locally. Marking PR as ready for review.

The only thing I'm not particularly happy about — and which I was trying to avoid in my first commit — is that now the CoreVisualizer struct has become a mutable struct.

ferrolho commented 6 months ago

@rdeits, can I merge this and tag v0.16.1?