To be clear, Dict
is quite amazingly fast when used with types which are cheap to hash; it’s a great simple and robust default. Replacing it in the code above was interesting but I’d say it’s a questionable performance hack unless you really need the speed. You might find the following thread interesting:
The speedup here was relatively minor, but if you have a large dataset and you’re limited by memory bandwidth it can make a huge difference to use smaller width integers. Another related reason to use narrow types would be if your inner loops are naturally data-parallel. In that case the compiler’s SIMD vectorizer might kick in, giving you a speed boost depending on how many elements can fit into your SIMD register at once.
IIRC I got a small benefit out of removing the recursion once I’d optimized some of the other things. I didn’t check in the final version though.
Using sizehint!
to avoid rehashing the Dict
helped quite a lot. You can sizehint!
the arrays as well but that made less of a difference (hence being commented out).
So actually this is the part which you really want to optimize I’d suggest giving LightGraphs.DiGraph
a go by feeding it an edge list approximately like the one I’ve built above. I peeked at the light graphs internals and it doesn’t seem to lay out the data quite how I was imagining (ie, in a few big contiguous arrays). Perhaps there’s a different “immutable graph” type you could use which does that. I don’t know a lot about that part of the ecosystem mind you.