diff --git a/docs/src/tutorials/hot_loop.md b/docs/src/tutorials/hot_loop.md index 0321e6e..8544e97 100644 --- a/docs/src/tutorials/hot_loop.md +++ b/docs/src/tutorials/hot_loop.md @@ -45,3 +45,33 @@ allocs = check_allocs(run_almost_forever!, (Vector{Float64},)); @test isempty(allocs) ``` + +## More complicated initialization +In practice, a function may need to perform several distinct allocations upfront, including potentially allocating objects of potentially complicated types, like closures etc. In situations like this, the following pattern may be useful: +```julia +struct Workspace + ... # All you need to run the hot loop +end + +function setup() + # Allocate and initialize the workspace + return workspace +end + +function run!(workspace::Workspace) + ... # The hot loop +end + +function run() + workspace = setup() + run!(workspace) +end +``` + +Where `workspace` is either a custom struct designed to serve as a workspace for the hot loop, or simply a tuple of all the objects required. + +The benefit of breaking the function up into two parts which are called from a third, is that we may now create the workspace object individually, and use it to compute the type of the arguments to the `run!` function that we are interested in analyzing: +```julia +workspace = setup() +allocs = check_allocs(run!, (typeof(workspace),)) +``` \ No newline at end of file