[1.1.76] Enemy expansion candidate chunk value computation is slow

We are aware of them, but they have low priority. We have more important things to do. They go here in order not to take space in the main bug thread list.
Post Reply
Atraps003
Long Handed Inserter
Long Handed Inserter
Posts: 63
Joined: Thu Jan 04, 2018 6:34 am
Contact:

[1.1.76] Enemy expansion candidate chunk value computation is slow

Post by Atraps003 »

This computation is done even when it's not used for gameplay (peaceful and/or expansion disabled).


Here are a list of benchmarks with different expansion parameters.


Save 2.zip-
Enemy Bases Disabled

Performed 25000 updates in 43078.746 ms
avg: 1.723 ms, min: 0.014 ms, max: 22.377 ms
checksum: 1478576917
44.785 Goodbye



Save 3.zip-
Enemy Bases Enabled (600%/600% size/freq)
/c game.map_settings.enemy_expansion.max_expansion_distance = 0
/c game.map_settings.enemy_expansion.friendly_base_influence_radius = 0

Performed 25000 updates in 48195.971 ms
avg: 1.928 ms, min: 0.012 ms, max: 22.314 ms
checksum: 3101583768
51.380 Goodbye



Save 4.zip-
Enemy Bases Enabled (600%/600% size/freq)
/c game.map_settings.enemy_expansion.max_expansion_distance = 2
/c game.map_settings.enemy_expansion.friendly_base_influence_radius = 2

Performed 25000 updates in 48292.988 ms
avg: 1.932 ms, min: 0.021 ms, max: 23.419 ms
checksum: 474892596
51.511 Goodbye



Save 5.zip-
Enemy Bases Enabled (600%/600% size/freq)
/c game.map_settings.enemy_expansion.max_expansion_distance = 20
/c game.map_settings.enemy_expansion.friendly_base_influence_radius = 2

Performed 25000 updates in 114056.784 ms
avg: 4.562 ms, min: 0.017 ms, max: 58.503 ms
checksum: 2440450436
117.244 Goodbye



Save 6.zip-
Enemy Bases Enabled (600%/600% size/freq)
/c game.map_settings.enemy_expansion.max_expansion_distance = 25
/c game.map_settings.enemy_expansion.friendly_base_influence_radius = 8

Performed 25000 updates in 1149348.364 ms
avg: 45.974 ms, min: 0.017 ms, max: 928.929 ms
checksum: 2723993261
1152.541 Goodbye
Attachments
expansion-candidate-chunk-value-computation-slow.mp4
(40.38 MiB) Downloaded 49 times
6.zip
(3.24 MiB) Downloaded 54 times
5.zip
(3.22 MiB) Downloaded 37 times
4.zip
(3.24 MiB) Downloaded 35 times
3.zip
(3.24 MiB) Downloaded 34 times
2.zip
(3.23 MiB) Downloaded 48 times
factorio-current.log
(6.29 KiB) Downloaded 35 times

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2227
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [1.1.76] Enemy expansion candidate chunk value computation is slow

Post by boskid »

Primary place where i see the CPU being consumed at is computation of the "ChunkScore" by the ExpansionPlanner. By polluting and pollution spread you force a lot of chunks to be generated and when chunks are generated they notify ExpansionPlanner about chunk being generated. ExpansionPlanner tries to compute the chunk score and this operation is expensive due to settings you are throwing at it, mainly the `friendly_base_influence_radius` because for each chunk generated, that code will touch at most (2*F + 1)^2 chunks nearby and on each of those chunks it counts amount of spanwers by iterating the list of military targets (spawners are always military targets so its not done through entity searches). This operation of counting number of bases on a given chunk could be optimized by having a counter of enemy spawners on a particular chunk for a particular force but that would make sense only if in most cases there are multiple spawners on each chunk and multiple other military targets on each chunk (visiting all military targets on chunk also checks if they are a spawner). Having this data "cached" would be yet another place that could become inconsistent with real data and if possible i would try to avoid introducing such pieces of logic.

In general its hard to claim that a performance problems are bugs, there are multiple places where you could say "make it faster" and it will be marked as not being a bug, in this case it is really close to this one but there is one specific piece of code that could be optimized. I will keep this bug report open to see if i can think of a nice simple solution that would be easy to implement in 1.1.

Atraps003
Long Handed Inserter
Long Handed Inserter
Posts: 63
Joined: Thu Jan 04, 2018 6:34 am
Contact:

Re: [1.1.76] Enemy expansion candidate chunk value computation is slow

Post by Atraps003 »

Thanks for looking. Here is an example of chunk score being slow when spawners are destroyed instead of generated.
Attachments
spawners-destroyed.mp4
(59.24 MiB) Downloaded 39 times

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2227
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [1.1.76] Enemy expansion candidate chunk value computation is slow

Post by boskid »

I know, there are 6 reasons why a chunk score is recomputed: 1/ base built, 2/ base destroyed, 3/ chunk generated, 4/ tile changed, 5/ player building created and 6/ player building destroyed. Almost all of those are relatively common thing which is why i am not closing this bug report because it would be nice to improve the performance here if it has such a big impact.

Post Reply

Return to “Minor issues”