Stellaris

Stellaris

Evolving Hyperlanes Redux
 This topic has been pinned, so it's probably important
Draconas  [developer] 11 Oct, 2022 @ 1:28pm
Why does it cause lag spikes on large maps? Why You No Fix?
Because Can't Fix!

If I could, I would. I keep meaning to try a few things that may improve the situation, but the problem is they definitely are a lot of work, and only may improve performance and may make it worse or differently bad. So they are having to wait until I have time on my hands to burn.

(The big one if you feel like coding is spreading the create and destroy events out across the year rather than having them in a batch)

So this mod will probably never work well on huge galaxy's.

Firstly, please remember that modders have a limited set of tools to work with. I have the options available to me in the mod scripting, which are limited.

Secondly a chunk for this is based on my observations, is not gospel for what is actually happening, but 1. I have done several mods that screw with the galaxy now, and have a lot of observational data to back this up, and 2. I am a coder in real life so have a pretty good understanding the underlying mechanics - consider it like a good mechanic - I may not know exactly how your make and model of car works, but I know how a lot of cars work, and can suss out a lot of your car with that knowledge because that's how cars work.



Consider the following:

1. The Stellaris galaxy is a network of nodes (stars) connected by edges (hyperlanes)

2. The Stellaris galaxy network is static for the lifetime of the game (broadly true in vanilla, occasionally events change it, usually by adding a single system or a small cluster (L-Gate, and do you notice the lag spike when that appears? I certainly do?), but there are no major shifts)

3. Pathfinding through this network is a thing that has to happen a lot. e.g. A fleet needs to move from Star A to Star B. Same with "give me a star that is geographically close to star X". The game needs this to be very fast.

4. Finding the best path is a function of distance (mostly static, want the shortest) and of quality (frequently changing. E.g. system owners, presence of hyperlane blockers, presence of enemy fleets)

5. Path finding on a network is a well understood, but non trivial problem in computer science. There are many different strategies you can take.

6. Of the many strategies, we can break them down into 2: Ones where you know the distance / value function at any point in the network, and ones where you don't. The strategies where you know the distance at any point are an order of magnitude (e.g. 10x) faster than the ones where you don't.
For a comparison, imagine you are trying to see your friend in a far off city. In one instance you have a map showing you all the roads, intervening towns and the distances between them, in the other instance you don't and have to stop and ask locals at each town you enter how much closer you have gotten to your friend and where the next towns you could head to are.


Given all this, specifically points 2, 3 and 6 the developers made some choices.

Specifically they choose to pre-calculate that map soon after galaxy generation and store it in a massive lookup table somewhere. Broadly I am certain that in the background of your stellaris game is a massive lookup table that lists the distance between every star and every other star, both in raw display terms and through the hyperlane network. (and probably other data like what that path is). Computationally this is incredibly bad because its a problem that gets massively, massively more complex the more stars you add, it's a potentially O(n!) problem for the computer scientists. But n is fairly small (number of stars in the galaxy) and in vanilla it only has to do this once, and I suspect there are a collection of optimisations they can do to make it less bad.
Once it has that table it can then use it for all of the (many) pathfinding operations it needs to do, and only has to worry about dynamic changes that involve players and empires and fleets etc..., which is massively easier to calculate.
This is why on huge galaxy's you can notice a lag spike when you add a single system, but never see that on small of medium ones.

What this long winded explanation comes down to is, that this mod breaks a fundamental assumption that they built the game on: that the galaxy is basically static, so even though its very expensive to build the map of galactic distances, it's worth it because you only have to build it once and then it makes all your pathfinding very fast. And if you do have to rebuild the map, it's usually only because of a specific edge case that only happens once per game and is often a small change that you can optimise around so not have to rebuild everything every time.

Then this mod comes along and regularly makes massive changes to the hyperlane network, forcing the game to throw away its entire map and rebuild the whole thing from scratch. Now I happen to know that this gets done in a multi threaded fashion in the background, because I've managed to break it, but all of those background multi-threads have to finish before the turn tick can advance, because it has to know if it needs to repath ships, so the tick has to wait for them all to be done.

So that is why there will always be a lag spike on huge galaxy's. I will probably over time experiment with some ideas to try and make them less bad, but they will probably always exist and there is nothing I can do about them, it is the nature of how the game works.