Refined Storage

Refined Storage

115M Downloads

CraftingTree recursion calling MutableResourceListImpl.copy on nested crafts causing lag

Closed this issue ยท 1 comments

commented

Describe your enhancement

On the server I am running, I noticed a lot of lag coming from some of the refined storage exporters with autocrafting upgrades. I ran observable sampler over it and found that the exporters call AutocraftingNetworkComponentImpl.getMaxAmountSync, which leads to the CraftingTree object simulating the craft to see how many it can craft. At every crafting level, the CraftingTree calls child() for all it's children and creates a copy of the current Crafting state to simulate the child crafts over. Following the sampler I found that the calls to MutableResourceListImpl.copy make up a majority of the CPU time.

MutableResourceListImpl.copy calls the it's add() method on each entry which in turns attempts to get the objects from it's entries. This causes many hashes to be created for objects despite them definitely not being contained inside the crafting state at this point. As the crafting state starts empty, every object will be put into the craftingState for the first time. This calls the addNew method anyway as it's the first time it sees this entry.

This is a screenshot from the profiler. This contains only one branch of the tree, but they all terminate the same way, with the MutableResourceListImpl.copy being most of the CPU time:

Image

Can I suggest changing this .add call in MutableResourceListImpl.copy to directly call addNew instead? It seems it is eventually calling addNew anyway so it should only reduce the number of hashMap.get calls. Obviously I'm not an expert so please let me know if I'm missing anything.

Extra: Ideally copying the hashMap could be done in a different way entirely. I'm not super familiar with java but in rust for instance it would be possible to instead to copy the memory directly vs adding each new entry one a time. Maybe it would be possible in java using serialization/deserialization to copy the object? Since the copy should be identical, anything to avoid creating the hashes again would go a long way.

commented

Mostly fixed by ea8fce7.