@waranator: What I previously stated remains valid -- many tracked entities lead to some performance issues regardless of tracking method -- but there's a possible fix that would require changing the way the mod works somewhat.
Before we get to that, though: there is no method for implementing on-save or on-load behaviour, otherwise I would like to do exactly what you said: retain only coordinates of the ore entities and link to the entities as needed -- not even at load time, but on first query, and retain the references thereafter. That would give the best performance envelope, with simple lists of numbers (coordinates) being very fast to load out of script.dat, and world.find_entities_filtered being restricted to a few calls that then don't need to be repeated until the next time the map is loaded.
There are basically two ways to do ore tracking, and a third method that would work even better, but will be more difficult to implement. In order:
- you can keep track of entity coordinates (very fast saving/loading, they're just two lists of numbers) and do
find_entities_filtered
to acquire an entity reference when you need to ask it how much ore it has left (slow runtime, you have to do a lot of find_entities_filtered
and it adds up). You can't make a transitory cache (one not persisted in global
) because that would lead to (rare, but guaranteed) desyncs.
- you can keep track of actual entity references (slow saving/loading, Factorio itself has to save a permanent identifier and then reconnect the entity reference on load), and just ask the entity how much ore it has whenever you're interested (very fast runtime, the entities are already right there). There's no opportunity to speed things up here aside from a periodic pruning of tracked entities, which would occasionally slow down the runtime but would allow removing unnecessary tracked entities from the savegame.
- as a hybrid, you can place some (YARM defined) mining drill entities and set them to count connected ore (one entity per contiguous ore patch); you'd still save the entity references, but there would be orders of magnitude fewer of them (tens instead of tens of thousands), and Factorio itself does the ore counting very quickly (in C++) so the runtime would be more than acceptably fast. The disadvantage is that there must be these entities that get placed in the world and that must be managed (and which must never actually mine any ore).
Actually, on that note: we could have YARM tag ordinary mining drills directly -- then there's no need for us to define a mining drill entity, nor to stop it from doing its intended job. This would remove a lot of complexity from the code, but would force players to manually update their tracked sites.
I'd like to actually implement one of the hybrid approaches, but either of them is non-trivial effort and most of my time has been taken by IRL stuff -- until I get the opportunity to work on YARM again, you'll have to live with the save/load performance being scuttled in favor of runtime performance. Of course, you're welcome to fork the code and maybe pull request the changes -- it's open source precisely so that someone else can take over when I can't.