Skip to content

Commit

Permalink
add more elaborate workflow in hot loop tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Oct 26, 2023
1 parent fc11519 commit 6049224
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/src/tutorials/hot_loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -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),))
```

0 comments on commit 6049224

Please sign in to comment.