Event handler

If you only have a couple of known events, I think a sequence of if is a decent approach. If the computation inside state transition is relatively small and the transitions between events have some structure (not all states jump to arbitrary state), maybe you can squeeze out some performance with @goto.

If you have many unknown event types, “JIT” approach may be useful. See, e.g., Catwalk.jl: [ANN] Catwalk.jl - With dynamic dispatch to the moon! (an adaptive optimizer, aka JIT compiler)

If you know the possible concrete types of f[j] statically, you can use a manual union splitting hack like this. (This is a manual version of Catwalk.jl etc.). This is equivalent to plain ifs, but it can be useful for code organization.

2 Likes