[0.17.41] Performance issues with get_craftable_count

Bugs that are actually features.
Post Reply
Morcam
Burner Inserter
Burner Inserter
Posts: 5
Joined: Fri May 17, 2019 4:40 am
Contact:

[0.17.41] Performance issues with get_craftable_count

Post by Morcam »

Hi,

While investigating some performance issues in the Handyhands mod (https://mods.factorio.com/mod/HandyHand ... 000c4ed671), I discovered that the player.get_craftable_count API seems to be taking a lot of time to run when mods with lots of recipes are installed, but it doesn't make a lot of sense as to why. Specifically, this code:

Code: Select all

script.on_event(
    defines.events.on_tick,
    function(event)
        local p = game.players[1]
	local craftableCount = p.get_craftable_count("pipe")
    end
)
Takes around 2-3ms to run when a full set of Pyanodon's mods are installed. More complex recipes or more checks per frame can easily drive the costs to 50ms or more, which obviously causes stuttering. When you think about it, this doesn't make a lot of sense - the recipe for a pipe has not changed at all due to the mods - it's still just 1 iron plate, and there aren't really any recipes for handcrafting iron plates. Since Pyanodon's mods (and most other big mods) add large iron plate refining recipe chains, it's probably wandering through those uncraftable recipe chains and collecting the ingredients? Or is it just an unavoidable problem with a lot of recipes?

So a couple things:
1. Is the get_craftable_count API call looking through un-hand-craftable recipes? Can it not do that?
2. Presumably the inventory does things a bit differently, since it doesn't take several seconds to load. Can we get access to whatever it's doing to get recipe counts in a mod?

kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: [0.17.41] Performance issues with get_craftable_count

Post by kovarex »

The more recipes the more it needs to check for the dependency chains.

Morcam
Burner Inserter
Burner Inserter
Posts: 5
Joined: Fri May 17, 2019 4:40 am
Contact:

Re: [0.17.41] Performance issues with get_craftable_count

Post by Morcam »

I'm saying that there are no more applicable recipes. A pipe is made of an iron plate. The mods add no handcraftable recipes for iron plates. So there is no chain.

The inventory menu must do things differently, since it doesn't take 10 seconds to open, which it would if I ran get_craftable_count on all the recipes in it.

kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: [0.17.41] Performance issues with get_craftable_count

Post by kovarex »

There is an operation, where it prepares the meta-information for the calculation, which is normally shared for all the things shown in the crafting menu. This is why it doesn't take that long.

User avatar
raiguard
Factorio Staff
Factorio Staff
Posts: 451
Joined: Wed Dec 13, 2017 8:29 pm
Contact:

Re: [0.17.41] Performance issues with get_craftable_count

Post by raiguard »

So is there nothing that can be done to improve this performance? In my Quick Item Search mod, I only show recipes that you can actually craft. When you have a very tiny search query (such as "b") and lots of mods enabled, checking each item to see if the player can craft it can take upwards of 280 milliseconds! It's a huge pain in an otherwise performance-friendly mod; getting the craftable counts takes over 300 times longer than the rest of the mod's logic combined...

Image
Don't forget, you're here forever.

Post Reply

Return to “Not a bug”