• 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:
Is this an out of season paradox summer vacation joke?
 
  • 8
  • 7Haha
  • 1Like
  • 1Love
Reactions:
Cheers for the DD Mat, and great work on the optimisations :) I'm sure you're looking at these things all the time, but if there's any chance of continued work on optimising the pace of the game during play? I enjoy the first 150 years immensely, but after that it's so slow that it's literally "read a book while you play" because the ratio of waiting : playing has got too high to maintain engagement (and a watched galaxy never boils.....).

My PC isn't the fastest (a teensy bit below recommended specs with the CPU, a Ryzen 5 1600 - GPU and RAM are all above, but I've got no doubt it's the CPU holding it back), but I can happily play HoI4 through to the 1950s, and EU4 through to the end, and CK2 up until the last hundred years or so (that also got bogged down hard towards the end, at least about a year ago), and Imperator Rome is also quick enough to maintain interest. Vicky 2, of course, runs very well :).

What's confused me particularly is that there's not a huge impact on galaxy size as to the speed and slowdown - I tried going small, and hit the same issues (it was slightly faster, but nowhere near as much as I'd have expected given the expected differences in terms of data processing requirements).

No biggy if not possible - I'll upgrade my PC in the next 12 months and get back into Stellaris then if it can't go any quicker - but from a hardware perspective it feels like the least accessible from a hardware perspective (ie, needs most expensive hardware) of any of PDS' games to enjoy.
 
  • 1Like
Reactions:
Who's Jeff?
 
  • 6
  • 2Like
Reactions:
I don't understand people on this forum.

First, they complain that there hasn't been any DD or official communication in months (rightly so I think). People said, if PDX can't show any new content yet, they could at least write about something boring or technical, just to show that they are working on the game and engage with the community.

Now we have a DD doing just that, Mat even sticked around answering questions (thank you @MatRopert ), and now people complain that the topic of this DD was not important for them and the devs should have worked on something else.

Also, keep in mind that Mat said that they are still in home office and I can say from experience that the complete lack of physical meetings and interactions with your colleagues over such a long period of time definitely takes a toll on productivity.

I too was complaining about the lack of communication, so if any devs read this, plaese don't be discouraged by the always unhappy and keep DDs like this coming, until the marketing people give you the "go" for the juicy stuff.
 
  • 26
  • 4Like
  • 2Haha
  • 1
Reactions:
My experience with modding another game suggests startup times would help out modders as well. Don't know if you can change files in this game while it's running, but if not, it's a lot of game startups to play and test the results of your mod changes and time saved there is appreciated.

It sounded from the Dev Diary that only Windows users with DX11 would see the benefit, but from the blog it seems like all platforms would benefit from at least the changes to the file loading library. Will be interesting to see how the overall set of changes translates to mac - though with my computer I spend more time loading save games than the game itself.
 
  • 3
Reactions:
I don't understand people on this forum.

First, they complain that there hasn't been any DD or official communication in months (rightly so I think). People said, if PDX can't show any new content yet, they could at least write about something boring or technical, just to show that they are working on the game and engage with the community.

Now we have a DD doing just that, Mat even sticked around answering questions (thank you @MatRopert ), and now people complain that the topic of this DD was not important for them and the devs should have worked on something else.

Also, keep in mind that Mat said that they are still in home office and I can say from experience that the complete lack of physical meetings and interactions with your colleagues over such a long period of time definitely takes a toll on productivity.

I too was complaining about the lack of communication, so if any devs read this, plaese don't be discouraged by the always unhappy and keep DDs like this coming, until the marketing people give you the "go" for the juicy stuff.
you forget that stellaris fans are the kind of people who can spend a hundred hours on a game and then call it unplayable.
 
Last edited:
  • 8
  • 3Haha
  • 2
  • 1Like
Reactions:
i am a bit curious regarding the production problem.
why do they even need to read the intial value ? just process the productions of the pops on the planet in order of the id of the pops and the planets are being calculated bei their id. or leave the planets out, the pops first of the whole empire in their id-order and then planets afterwards. this can be parallized. in the end each res is summed up and >>then<< the initial value is taken.
can there be negative values ? yeah, sure. but no problem, cause everything now effected cause of the negative values takes effect in the next cycle. but that is still all the pops of a planet, or the other method. this way its not getting rediculous to get the initial values for thousands of pops in a lategame and instead just one time per res, per empire, which should reduce it dramatically.

so, is this thought way to simple ??
 
  • 2
  • 1
Reactions:
I really like that there are resources invested into rather less prominent parts of the game that are still very important.

Really understand the people complaining about the bugs and odd ends that are there still in Stellaris, some things need at least a clean sweep, it's just little things but really a bunch of them, like the science ship curator technologies and/or the inability to upgrade / customize your own science ships, or constructors, that's just one bit. It doesn't make the game unfun or unplayable, but it's just annoying everytime you see this, salting the experience just that one bit, like taking 5% of your Stellaris happiness. These things are everywhere, and you notice them.
It's kind of a thankless, less flashy work, but would make many veterans happy, probably influencing them into recommending the game to other players.

That said thank you at PDX for the nice improvement, it will make many people glad, especially the mod lovers :)

All the best, Immanuel Can
 
  • 6
Reactions:
oh, Victoria III confirmed! :)
 
  • 3Like
Reactions:
Reading between the lines the article is wrote as though it was just something on the side he found, experimented with and found some wildly unexpected success. Rather than Paradox strategically invested some time/money. So nice one on that front. I hope may take it to the next level, if its good use for the better part of the game.
 
  • 8
  • 2Like
Reactions:
i am a bit curious regarding the production problem.
why do they even need to read the intial value ? just process the productions of the pops on the planet in order of the id of the pops and the planets are being calculated bei their id. or leave the planets out, the pops first of the whole empire in their id-order and then planets afterwards. this can be parallized. in the end each res is summed up and >>then<< the initial value is taken.
can there be negative values ? yeah, sure. but no problem, cause everything now effected cause of the negative values takes effect in the next cycle. but that is still all the pops of a planet, or the other method. this way its not getting rediculous to get the initial values for thousands of pops in a lategame and instead just one time per res, per empire, which should reduce it dramatically.

so, is this thought way to simple ??

You'd just go from having to calculate the changes to the main stockpile to calculating changes to the "buffer" of net resources gained/lost. All your suggestion would do is add an extra step. What needs to happen is basically just adding up a bunch of numbers. It doesn't matter if your initial value is added at the beginning or the end, it wouldn't reduce the amount of calculations that would need to be done.

What could theoretically work is some way of dividing the load, say calculating each planet or sector individually and then going through and adding the combined changes. Dividing based on jobs wouldn't work because jobs have a LOT of overlap in terms of resources used, If a miner and artisan and scientist all tried to process their production simultaneously it would break.
 
  • 2
Reactions:
The dev diary seems to be missing something. it jumps right from "it is hard to fix the slowdown" to "we did this instead" without clarifying if they are continuing to work on fixing the slowdown, or have given up.
 
The dev diary seems to be missing something. it jumps right from "it is hard to fix the slowdown" to "we did this instead" without clarifying if they are continuing to work on fixing the slowdown, or have given up.


It has been mentioned that performance is an "ongoing project" that gets tinkered with every patch...

... To be made worse evidence would suggest.
 
  • 4Haha
  • 2
  • 1Like
Reactions:
you forget that stellaris fans are the kind of people who can spend a hundred hours on a game and then call it unplayable.

This is far too true. I don't think Stellaris is unplayable, but I do feel 2.6 introduced a bug that made me not want to play (critical difference, the game is still playable, I just find that particular bug ruins my experience). With 800 hours into the game, I'm definitely rookie numbers compared to most, but it's long enough to notice this community is utterly unpleasable and will never be satisfied. Stellaris players always seem to need to find a reason to complain about what the devs are doing, and while I think that Stellaris could certainly be improved (and frankly does need more polish for its existing systems), the way people here go about it is... not okay.

We got radio silence, and we complained. So a member of PDX came and gave us radio contact, and we complained. They said they're looking at performance, and this little neat thing came out of it, and we complained. We demanded they fix systems, and they did, and we complained (specifically about it not being enough in this case, despite "enough" being subjective.) Frankly, every time this community asks for something, when we get it, people complain.

This is why fandom can't have nice things people. PDX has been absurdly tolerant of our crap, and seriously deserves a break from it all. I don't want to see the Stellaris dev team go the same way a lot of my favourite old things went; abandoned and despised by its creators for the people that came to call themselves fans.

Give them a bloody break; they're trying. Appreciate what we have, rather than always focusing on what we don't. Stop accentuating the negative, and just enjoy that we're getting improvements.
 
  • 8
  • 6
  • 2Like
  • 1Haha
Reactions:
i adore the game, and it is still playable, it's many glaring issues are the only reason i don't spend 10 hours a day on the game. the game is in a bad place at the moment. pretty much anyone will acknowledge it. honestly if the crisis actually worked and some of the worse bugs would be fixed i would be content, a game as vast as stellaris will never be bug free, that is a fact, out of every thousand bugs 999 of which are irritating at worst. it's the thousandth bug that gets you , there are more graphical glitches and minor issues than any sane man would care to learn about . those don't bother me, it's stuff like the awoken being broken since they were introduced that bugs me, seriously one of the coolest events in the game has been breathtakingly borked for years. and then there is the fleet manager which is a buggy mess(ghost fleet shenanigans ) . the ai is drunk, but since that is fixable via mods i really don't care. the crisis ai being borked however... that's a no-no that needs to be fixed. the sector ai is a mess and forces you to manage all worlds yourself but that can be mitigated via certain settings, and mods, if done correctly it is only a minor problem.
i try not to complain too much about minor problems but when major game features or important events are borked i am going to be annoyed. i think that is justified.

the biggest issues in my book are as follows: crisis ai, fleet manager problems, regular ai, major events being broken, micro issues( i shouldn't have to mod the hell out of the game to make micro bearable)
fix those and i will happily resume playing and buying dlc, but until then i can't bring myself to keep pouring money into a game that seems to only develop more problems with every patch
 
  • 2
  • 1Like
Reactions:
you forget that stellaris fans are the kind of people who can spend a hundred hours on a game and then call it unplayable.
let me tell you a story.

I've played HoI4, as a number of countries.
I have a bad tendency to restart after a few days, weeks, or months of gameplay for various reasons. Some will be detailed below. Some are as simple as "I realized playing as country X would be more fun"
If I only count countries I've played for more than 6 months, then I'd say I've only played as a major (GER, JAP, SOV, ENG, USA, ITA, FRA) a grand total of 15-20 times.

I've played Liberia at least 25 times.

Canada is another nation I've played a lot of. Given it's where I was born and live IRL perhaps that is not surprising. I want to tell you the events of about 2-3 weeks that happened to me a few years ago.

Game 1: Everything is going great. I'm doing my thing, happy! Not sure what to do with my army, but I shuffle them around the country. Then I notice that France is out of the war... but something about the Italian army in Paris? I pan over, and sure enough, Italy gobbled up the majority of france again, meaning the regular events of the fall of France do not fire.
Sigh.
I really would like to try to replicate the war. I really would like that event to fire. So time for

Game 2: Can't have this happen 2 times in a row, right? This time I figure out what to do with my army; africa! I'm going to take Italian east africa! Sure enough, war begins, and my armies march forth. I happen to pan up to europe and... wtf, AGAIN Italy is plunging deep into france.
ffs.
Okay. So lets do this again.
This time I'm going to move MY army to the France-Italy border.
Great. I do that, the war is declared, and I sit there and without clicking any units, just watch that front. Looks good. No breakthroughs. Wonderful. It does not catch my attention that I don't seem to be losing men or equipment until I pan back over and have Italian units behind me! How!!
...I forgot to declare war.
I FORGOT TO DECLARE WAR.

Game 3: Okay. This time I'm gonna do it right. Army in place, I declare war; great! Italy is being beat back. Except this time, France, Belgium, and the Netherlands manage to over-run Germany. I don't have a single unit in that battle. Germany gets over-run and the war 'ends'.
the hell. This is not what I wanted at all.

Game 4: Okay okay okay. This time I'm gonna do it. I've been trying to play this dang game for days now. Everything seems great, I even remember to declare war...
...I forgot to move my army to europe

Game 5: Army in place, everything seems great... except the soviets are on some totally bizzare alternate history path (this was before you could force them not to). Needless to say the USSR gets over-run in no time.
Wonderful. All I want is to play WW2 as Canada. None of this totally unrealistic nonsense.

Game 6: I forget to declare war again...

Game 7: Set the wrong option, and my army attacks instead of defends...

Game 8: I try to be clever and only send half my army. Italy breaks through.

Game 9: Czechs beat the germans (more accurately, hold them off so long France invades and wins)

After this I took a few days off from playing. return.

Game 10: forget to move my army.

Game 11: forget to declare war.


This is where I gave up.

I had an old dual core computer at the time. Very slow. I think all of this added about 30-60 hours to my playtime.

And I dunno about you, but this sounds pretty unplayable to me.


I've only had one (1) game of Stellaris I couldn't finish because of stupidity (one of my stations became invincible)
There are very few games of Stellaris I've abandoned because they got too annoying. Contrast this with HoI4 where about 5/6ths of all my attempts end in frustration.

My playtime?


700 hours in each. 714 in Stellaris, 686 in HoI4.

edited to add:

Know why I have so many HoI4 stories and so few Stellaris stories?

Stellaris is good game. I enjoy it.
HoI4 is deeply flawed. I want to enjoy it.

HoI4 probably isn't for me.
And by the same token.
Stellaris isn't for everyone.


edited the following day to clarify/add:
the whole reason I had to put my army there was the failure of the game to do what it was, by its own definition, supposed to do, and be at least somewhat realistic. The bug, that I don't know if is fixed yet, is that as soon as germany declared war on France, all of France's armies on the Italian border would rush away, only to see that undefended border be over-run as soon as Italy declared war. 100% of my efforts were spent in trying to solve a bug.

edited the day after the last edit:
this post made me fire up HoI4 again to see how bad it is (its bad. when I tell each nation to go historic and save that preset; I become unable to open the preset. even just after saving it. on account of me not owning the DLCs I own)
anyway after selecting liberia, it occured to me people might want to know why on earth I'd ever play liberia. the answer is simple. I want to see if I can "be the first to drop a nuke, but without ever inventing electricity" or, in other words, develop "nuclear bombs" tech, and drop a nuclear bomb - be the first to drop a nuclear bomb - and do so without ever inventing the "electronic mechanical engineering" tech. Thus dropping the world's first nuke, without ever 'inventing electricity'
 
Last edited:
  • 1
  • 1
  • 1Like
Reactions: