• We have updated our Community Code of Conduct. Please read through the new rules for the forum that are an integral part of Paradox Interactive’s User Agreement.

Stellaris Dev Diary #181 : Threading and Loading Times

Hello everyone, this is The French Paradox speaking!

On behalf of the whole Stellaris team, we hope you've had a good summer vacation, with current circumstances and all!

We're all back to work, although not at the office yet. It is going to be a very exciting autumn and winter with a lot of interesting news! We are incredibly excited to be able to share the news with you over the coming weeks and months!

Today I open the first look at the upcoming 2.8 release with some of the technical stuff that we programmers have been working on over summer. The rest of the team will reveal more about the upcoming content and features in the following diaries.

Without further ado, let's talk about threads!

Threads? What threads?

There is a running joke that says fans are always wondering which one will come first: Victoria III or a PDS game using more than one thread.

image (26).png

Don't lie, I know that's how some of you think our big decision meetings go

I’m afraid I’ll have to dispel the myth (again): all PDS games in production today use threads, from EU4 to CK3. Even Stellaris! To better explain the meme and where it comes from, we have to go through a little history. I’m told you guys like history.

For a long time, the software industry relied on “Moore’s Law”, which states that a CPU built in two years will be roughly twice as efficient as one today.
This was especially true in the 90s, when CPUs went from 50 MHz to 1GHz in the span of a decade. The trend continued until 2005 when we reached up to 3.8GHz. And then the clock speed stopped growing. In the 15 years since, the frequency of CPUs has stayed roughly the same.
As it turns out, the laws of physics make it quite inefficient to increase speeds beyond 3-4 GHz. So instead manufacturers went in another direction and started “splitting” their CPUs into several cores and hardware threads. This is why today you’ll look at how many cores your CPU has and won’t spend much time checking the frequency. Moore’s Law is still valid, but, to put it in strategy terms, the CPU industry reached a soft cap while trying to play tall so they changed the meta and started playing wide.

This shift profoundly changed the software industry, as writing code that will run faster on a CPU with a higher speed is trivial: most code will naturally do just that. But making usage of threads and cores is another story. Programs do not magically “split” their work in 2, 4 or 8 to be able to run on several cores simultaneously, it’s up to us programmers to design around that.

Threading nowhere faster

Which brings us back to our games and a concern we keep reading on the forums: “is the game using threads?”. The answer is yes, of course! In fact, we use them so much that we had a critical issue a few releases back where the game would not start on machines with 2 cores or less.

But I suspect the real question is : “are you making efficient usage of threads?”. Then the answer is “it depends”. As I mentioned previously, making efficient use of more cores is a much more complex issue than making use of more clock cycles. In our case, there are two main challenges to overcome when distributing work among threads: sequencing and ordering.

Sequencing issues occur when 2 computations running simultaneously need to access the same data. For example let’s say we are computing the production of 2 pops: a Prikki-Ti and a Blorg. They both access the current energy stockpile, add their energy production to it and write the value back. Depending on the sequence, they could both read the initial value (say 100), add their production (say 12 and 3, the Blorg was having a bad day) and write back. Ideally we want to end up with 115 (100 + 12 + 3). But potentially both would read 100, then compute and overwrite each other ending up with 112 or 103.
The simple way around it is to introduce locks: the Prikki-Ti would “lock” the energy value until it’s done with its computation and has written the new value back, then the Blog would take its turn and add his own. While this solves the problem, it introduces a greater one: the actions are now sequential again, and the benefit of doing them on concurrent threads has been lost. Worse, due to the cost of locking, unlocking and synchronizing, the whole thing will likely take longer than if we simply computed both on the same thread in the first place.

The second issue is ordering, or “order dependency”. Meaning in some cases changing the order of operations changes the outcome. For example let’s say our previous Prikki-Ti and Blorg decide to resolve a dispute in a friendly manner. We know the combat system will process both combatants, but since there are potentially hundreds of combat actions happening, we don’t know which one will happen first. And potentially on 2 different machines the order will differ. For example on the server the Prikki-Ti action will happen first, while on the client the Blorg will act first.

OOS.png

#BlorgShotFirst

On the server the Prikki-Ti action is resolved first, killing the Blorg. The Blorg action that comes after (possibly on another thread) is discarded as dead Blorgs can’t shoot (it’s a scientific fact). The client however distributed the computation in another way (maybe it has more cores than the server) and in his world the Blorg dispatched the Prikki-Ti first, which in turn couldn’t fight back. Then both players get the dreaded “Player is Out of Sync” popup as their realities have diverged.

There are, of course, ways to solve the problem, but they usually require redoing the design in a way that satisfies both constraints. For example in our first case each thread could store the production output of each pop to add to each empire, and then those could be consolidated at the end. In the same fashion our 2 duelists problem could be solved by recording damage immediately, but applying the effects in another phase to eliminate the need for a deterministic order.

As you can imagine, it is much easier to design something with threading in mind rather than retrofitting an existing system for it. If you don’t believe me just look at how much time is spent retrofitting your fleets, I’ll wait.

The good news

This is all nice and good, but what’s in it for you in the next patch, concretely? Well you will be happy to hear that I used some time to apply this to one of the oldest bits of our engine: the files and assets loading system.

For the longest time we have used a 3rd party software to handle this. While it saved us a lot of trouble, it has also turned out to be quite bad at threading. Up to the point that it was sometimes slower with more cores than less, most notably to the locking issues I mentioned before.
In conjunction with a few other optimizations, it has enabled us to drastically reduce the startup time of the game.
I could spend another thousand word explaining why, but I think this video will speak better:


This comparison was done on my home PC, which uses a venerable i7 2600K and an SSD drive. Both were “hot” startups (the game had been launched recently), but in my experiments I found that even on a “cold” start it makes a serious difference.

To achieve the best speedup, you will need to use the new beta DirectX11 rendering engine. Yes, you read correctly: the next patch will also offer an open beta which replaces the old DX9 renderer by a more recent DX11 version that was initially made by our friends at Tantalus for the console edition of Stellaris. While visually identical, using DX11 to render graphics enables a whole range of multi-threading optimizations that are hard or impossible to achieve with DX9. Playing with the old renderer will still net you some nice speedup on startup, the splash screen step should still be much faster, but you’re unlikely to see the progress bar “jump” as it does with DX11 when the game loads the models and textures.

Some of those optimizations have also been applied to newer versions of Clausewitz, and will be part of CK3 on release. Imperator should also benefit from it. It might be possible to also apply it to EU4 and HoI4, but so far my experiments with EU4 haven’t shown a huge speedup like it did for Stellaris and CK3.

If you want to read more technical details about the optimizations that were applied to speedup Stellaris, you can check out the article I recently published on my blog.

And with that I will leave you for now. This will likely be my last dev diary on Stellaris, as next month I will be moving teams to lead the HoI4 programmers. You can consider those optimizations my farewell gift.
This may have been a short time for me on Stellaris but don’t worry: even if I go, Jeff will still be there for you!
 
Last edited:
  • 145Like
  • 38Love
  • 24
  • 6
  • 5Haha
  • 4
Reactions:
Well this is nice, the dev diaries actually return before the end of August, I half-expected to have to wait all the way to October :p

I'm not sure Jeff would approve of going this much into technical detail, as us primitives aren't well-versed in quantum string theory ;)
... But it's a very nice way to start off a dev diary cycle, not with new features but with technical diaries - I hope there are more of these coming, concerning balance too! :)
 
  • 4
  • 2Like
Reactions:
We're all back to work, although not at the office yet. It is going to be a very exciting autumn and winter with a lot of interesting news! We are incredibly excited to be able to share the news with you over the coming weeks and months!

Well hopefully it contains fixes for the core problems of the game, like these:

  1. Pop growth, (automatic) migration, resettlement simulator lategame, pop micromanagement hell and gene modding problems, having to use an edict to fix pop micromanagement which is locked behind Galactic community and 3 resolutions.
  2. AI being as stupid as ever while Starnet AI, being developed by 1 modder with less tools than Paradox, made an AI fully capable of managing its economy with 0 economy cheats.
  3. Automation as in planet and habitat auto-build. Its completely broken and just as stupid as the AI. This feature will kill your economy instead of making lategame bearable. There are no working tools to have the AI reasonably automate the tedious micromanagement for you.
  4. Crisis AI is broken since 2.2 since the Crisis is too stupid to actually conquer the galaxy. Even worse, the 2.6 patch did focus on Crisis, but Paradox didn't care about fixing whats wrong. Instead they just changed how Crisis targets a player. No one asked for this - instead players asked Paradox to fix the Crisis AI. But Paradox didn't care.
  5. The game was never designed with colonizing 50, 75, 100+ planet and Habitats. Paradox had to add a x25 Crisis multiplier beacuse of this. (Of course it doesn't help at all because Crisis is broken, see 4.) Quest rewards and science from station was never adjusted for 2.2 science costs and science from station and rewards are quite close to useless in most instances.
  6. Balance is completely absent. As a quick example: Spiritualists are no match for Materialists, since 2.2. The very recent update was a huge slap in the face for Spiritualists and made them much worse since most Edicts are now permanent -rendering edict cost and edict duration bonuses from Spiritualist Ethic useless. (Makes you facepalm immediately if you think about it, but apparently Paradox did it anyway)

These issues are in for months. 2.6 didn't fix the Crisis. Some space-whale patch from 2.7 didn't fix anything either. If you want players to return, you will have to fix the game instead of throwing more content ontop of a broken foundation.
 
  • 14
  • 1Like
Reactions:
That command is definitely in the current release. You need to switch to observer mode and observe an AI nation to see it though.
OK, unless I have misunderstood something here the command might be in but doesn't actually do something. At least it doesn't display the nice tooltips we've seen in the dev diary:
1598535157016.png

The highligthed fleet is mine, so it's there because I send it out to conquer stuff before the AI took it over when I entered observer mode. Note that it is at full strenght and stronger then anything the opponent has to throw against it, yet it's new mission is not only to retreat into friendly territory...
1598535349853.png

...but to a starbase on the other end of my empire (no wormhole shortcuts that would make this starbase to be considered a nearby one)
This is the reason I stopped playing months ago, my AI allies are just as useless so there's no point in having a federation in the first place for me. So this is a bug that needs to be fixed before I would even consider getting another DLC but in the meantime I would be glad to even understand WHY the AI does this.
 
  • 4
  • 1Like
Reactions:
OK, unless I have misunderstood something here the command might be in but doesn't actually do something. At least it doesn't display the nice tooltips we've seen in the dev diary:
You need to observe a specific AI nation for the skull tooltips to show.

AI (and performance for that matter) are always things we keep in mind. The next patch will contain some tweaks on that topic, as the ones after that too. It's a recurring topic that we work on over time, it's unlikely that there will be the _one_ patch that solves everything at once.
 
  • 9
  • 4Like
  • 3
  • 1Haha
Reactions:
In regards to the AI topic; I feel there will *never* be a single, satisfactory AI. Make it too competent, and some players won't be able to stand up to it at all; which isn't fun. Don't make it competent enough, and everyone will always steamroll it (as we see currently.) I feel AI competence should vary based on the difficulty curve, rather than just using the difficulty setting to prop up the AI with more and more resources being pulled out their.... whatever the species has for removing waste matter.

I think an easy difficulty should have an easy AI, while higher difficulties have progressively more competent and challenging AI. I don't play with Starnet, since I genuinely can't beat the damn thing, and getting my ass handed to me and finding myself faced with a defeat string prior to 2250 is... not fun.
 
  • 1
Reactions:
Apparently I'm too bad at google searching for this. How do I do get to observe a specific AI nation? Is anyone here able to get the skull tooltips to show?
While in observer mode, control+click on a nation's flag to observe it. You can also use the observer outliner on the top right.
 
  • 12
  • 2Like
  • 2
Reactions:
it's unlikely that there will be the _one_ patch that solves everything at once
But for performance there was one patch that lowered it significantly and we're still dealing with it over 1,5 years later. It was a bold move to change that much of the game with Megacorp, it would also be a bold move to come back on some of these changes. If rebalancing the game arround lower pop numbers would mean a faster, easier and more substantial improvement to performance I would be all for it. Anyway I'm repeating myself. Thanks for answering my earlier question.
 
  • 7
  • 6
  • 1Like
Reactions:
it has enabled us to drastically reduce the startup time of the game.
Fine, based on the video, Stellaris will start roughly half a real-time-minute faster. Maybe even a whole real-time-minute in some cases ?

I promise that I will be more impressed if the game itself would run faster during its actual play-time so that I wouldn't waste a zillion of real-time-HOURS due to ingame-performance-issues.

Have I missed something, but ( altough 3 whole months have already passed again ) this is all for now, right ? So, let me guess that the 3 whole months were actually used for something else ... like new content ... for a new DLC and its update ?
 
Last edited:
  • 14
  • 5
Reactions:
While in observer mode, control+click on a nation's flag to observe it. You can also use the observer outliner on the top right.
THANK YOU!

OK, so my main fleet is running towards the other end of my space to merge with the single transporter there. OK, that is still dumb but it's not randomly dumb. I abandoned this game months ago and finally have an answer.
 
  • 1Like
  • 1
Reactions:
That command is definitely in the current release. You need to switch to observer mode and observe an AI nation to see it though.
matt most of us are done with the base game and playing mods now. im assuming the modders are very bad at optimizing and just love creating. i was wondering if you could make an auto threading script that applies to mods. currently i have over 50 mods installed, but i assume most people are playing with gigastructures or star trek.

it would be cool to see higher interaction between the devteam and modders giving them access to performance metrics. some dos and donts