Persistant arrays (or parameters) to avoid re-allocation

Any approach which tries to automatically make the data persistent will introduce a bunch of “fun” opportunities for error, like thread safety (what happens if two threads try to call this function at the same time?) and debugging (how can you test the contents of the persistent data if it’s totally hidden?).

By far my favorite way to handle this situation is:

  1. Create a Workspace struct holding the relevant pre-allocated stuff
  2. Change the function signature to `foo!(workspace::Workspace, x::Vector)
  3. Create a convenience function foo(x) = foo(Workspace(x), x)

Then in performance-critical code, you can pre-allocate the Workspace and keep it around, calling foo!(workspace, x) every time. But for testing or simple interactive usage, you can call foo(x) which will construct a temporary Workspace for you.

This avoids any need for metaprogramming, makes thread-safety easy, and lets you test the contents of the Workspace after the fact if necessary.

12 Likes