Best approach for runtime dispatching inside a hot loop (heterogeneous tree structure)

Let me just mention the following: if you decide to use if statements, the compiler has an optimization to recognize the following pattern

   if key == 1 
     ...
   elseif key == 2
     ...
   elseif key == 3
     ...
   else
   ...
   end

and transforms it to a jump-table in the machine-language code so that the running time is independent of the number of cases. Unfortunately, this optimization is broken on some platforms in 0.6 (i.e., erroneous machine-code is emitted) because of an LLVM bug, but in an earlier discourse discussion Yichao Yu posted instructions how to patch your 0.6 for this particular bug.