spill_item_stack() causes heavy lag
Posted: Mon Sep 13, 2021 5:42 am
the command spill_item_stack() causes unnecessarily heavy lag due to needing to find where to drop repeatedly.
this is shown in the following example
on my machine the following code takes 2 seconds.
/c player = game.player
for i=1,1 do
player.surface.spill_item_stack({0, 0}, {name="iron-plate", count=100000}, true, player.force, true)
end
while this code takes 16 seconds.
/c player = game.player
for i=1,1000 do
player.surface.spill_item_stack({0, 0}, {name="iron-plate", count=100}, true, player.force, true)
end
both have identical results but the 2nd is divided into stacks of 100
this is important due to the most common use case looking something like this, going through an inventory and spilling everything on the same location.
for i=1,#inventory do
if inventory.valid_for_read then
player.surface.spill_item_stack(player.position, inventory, true, player.force, true)
inventory.count = 0
end
end
(code from https://mods.factorio.com/mod/muppet_streamer)
this is far closer to the slow case and so takes a long time.
my request would be to either allow spill_item_stack() to take in a list of stacks or to cache the searched area for the duration of the tick (only a single tick is likely necessary)
the list of stacks should be the same format as an inventory so coping the reference of the inventory directly into the command works
this is shown in the following example
on my machine the following code takes 2 seconds.
/c player = game.player
for i=1,1 do
player.surface.spill_item_stack({0, 0}, {name="iron-plate", count=100000}, true, player.force, true)
end
while this code takes 16 seconds.
/c player = game.player
for i=1,1000 do
player.surface.spill_item_stack({0, 0}, {name="iron-plate", count=100}, true, player.force, true)
end
both have identical results but the 2nd is divided into stacks of 100
this is important due to the most common use case looking something like this, going through an inventory and spilling everything on the same location.
for i=1,#inventory do
if inventory.valid_for_read then
player.surface.spill_item_stack(player.position, inventory, true, player.force, true)
inventory.count = 0
end
end
(code from https://mods.factorio.com/mod/muppet_streamer)
this is far closer to the slow case and so takes a long time.
my request would be to either allow spill_item_stack() to take in a list of stacks or to cache the searched area for the duration of the tick (only a single tick is likely necessary)
the list of stacks should be the same format as an inventory so coping the reference of the inventory directly into the command works