narc0tiq / YARM

Yet another variant of the Resource Monitor for Factorio
https://mods.factorio.com/mod/YARM
MIT License
54 stars 42 forks source link

Cache ore entities to reduce calls to `surface.find_entity` #98

Closed narc0tiq closed 5 years ago

narc0tiq commented 5 years ago

Instead of having the sites retain coordinates, which automatically brings in the need to do a LuaSurface::find_entity call whenever the site wants to recalculate itself, let's have a central ore tracker that feeds out indexes that the sites can retain instead.

This gives some advantages:

And some disadvantages:


Original content below:

Every tick, we do up to 100 calls to LuaSurface::find_entity for each site that is actively being recounted, and for each force that has that site under active observation. We do these calls to avoid increasing save/load times by storing the actual entities (as we used to do before #26).

However, the call itself probably (!) takes extra time as it crosses the Lua-C++ barrier, whereas if we had the entities stored in a Lua table, we might be able to reduce the access time and thus the on_tick footprint of the mod.

NB: if implemented, this change must be backed up by some actual data; at the very least, set the time-between-site-updates fairly low and track some sites with lots of entities, then check the debug info for whether YARM's tick timing changes with/without the change.

narc0tiq commented 5 years ago

The cache does make a difference: cache before & after

But it also is not multiplayer-safe... maybe store it in global, I guess.

narc0tiq commented 5 years ago

NB: with the ore tracker as finally designed/developed, we went to a much smoother tick time of 1-1.2ms with the same sites as shown in the image above. However, it also impacted save load time by a few seconds (the example image has sites totalling ~62,000 entities).