There are some temperature update flags you could look at to avoid checking most map tile blocks. You could also keep track of dig job completions to find mined tiles.
The temperature update might help, but since I don't want to run this every tick, maybe check the actual temperature instead? "Warm stone" is 10075, so if the temperature is >= 10075 then that's pretty good evidence of magma.
Ideally, I could spawn a persistent structure for each mineral type (there are 24 distinct special minerals, but they won't all exist on the same map) that contains:
- Mineral type (if a tile doesn't match, it's reverted to a layer stone because it was mined out)
- Linked list of specks (they are all single-tile clusters)
- When to run the check (allows staggering the checks)
- Script to execute if speck is warm
Though unlikely to happen in a real game, the structure should deconstruct itself gracefully if the last speck is removed from the list.
The initial scan that sets up the structures will be in charge of spacing out the checks over N ticks. Not sure if last two digits of the time or last byte of the time (255 slices) would be more appropriate. I think updating this once per load is reasonable.
I'm not aware of any way to keep a persistent list in a script, so actually building it would need to wait for my new machine to create a DFHack plug-in.