Page 1 of 1

[1.1.38] spill_item_stack causes heavy lag

Posted: Sun Sep 05, 2021 7:57 am
by -DeadlyKitten
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.

Re: [1.1.38] spill_item_stack causes heavy lag

Posted: Sun Sep 05, 2021 8:32 am
by Silari
This isn't really a bug, as the command is doing exactly what it's supposed to and doing it fairly quick. The reason the second one takes longer is because it has to restart it's search every time the command is run again, so it's going over the area nearest the player 1000 times, the next area after that 999 times, and so on. The fact it only takes 8 times longer to run the command 1000 times rapidly is pretty dang impressive.

I'd say this'd be more fitting as a modding interface request, to allow the 'items' parameter of spill_item_stack to either be an ItemStackIdentification or a table of ItemStackIdentification. Then anyone who needs to spill a bunch of different items can gather them together into a table first and make just one call, which means it wouldn't have to repeatedly search the closest areas - it could just continue from the current search point with the next item stack. Also wouldn't be breaking for current usage of the function.

Re: [1.1.38] spill_item_stack causes heavy lag

Posted: Sun Sep 05, 2021 10:47 am
by Klonan
Yes indeed, this isn't really a bug, just the fact of how the functions and Lua API work
-DeadlyKitten wrote: Sun Sep 05, 2021 7:57 am 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.
So you can open a modding request for something faster for this use case,
I can imagine something like spill inventory, or as Silari said, spill stack to take a table of item stacks

Other than that I am moving this to Not a bug