After a lot of testing, I've tentatively narrowed down the culprit to casus belli. I've made one new casus belli, and added a minor alteration to a default cb. Can anyone spot anything in the code that would cause the game to slow down/hitch/choke?
Here is the custom cb
...
#THIS is the ONLY change I made to the code, iirc. I allowed it to be used in the same realm, and I required the character to either be rank 2 in a custom society or have a custom modifier
Initially, I thought that perhaps the slowdown could be caused by literally every AI evaluating whether they wanted to go to war with literally every neighbour. But then I realised that this is also gated on society rank and/or modifier, which probably means that only a few characters can potentially use this CB. (At least, I'm assuming there aren't hundreds of landed AI in the society...) For example, this CB should have fewer evaluations than the Germanic coastal conquest CB, where any landed Germanic character can target any coastal county in a different realm.
Then again, your problem description says you wait 15 years then the slowdowns happen once or twice per year... and that would be consistent with a new society forming, being populated, and then needing to wait a while for some members to reach rank 2.
Side-note #1: Since this can explicitly only target neighbours, perhaps you don't need
allow_distant = yes
. There doesn't appear to be any documentation for this, but the name suggests it's something like "can AI attack someone far away", which would be needed for Germanic county conquest, but not for conquest of neighbours.
Side-note #2: It seems unlikely, but maybe unlanded characters are evaluating the CB? There doesn't seem to be anything in
can_use
that would block an unlanded character from trying to use it... but I wouldn't expect the engine to try to evaluate CBs for characters with no troops, hence why this seems implausible. You could add
is_landed = yes
or
higher_real_tier_than = BARON
to be sure, although it seems a bit like gold-plating.
---
The default cb that I changed was tribal_invasion. The only thing I changed was to add a custom trait as a potential condition for declaring the cb under can_use_title, at the bottom of the quoted code below.
I can't see any obvious problems with this.
---
Overall, assuming that you're correct about not having changed anything else, I don't see anything in these CBs that would affect performance.
If I were you, my next step would be to design a test that would definitively confirm or deny the hypothesis that it's the CBs that are causing the problem. For example, run a game in observer both with and without the CBs (and with no other changes) - if the slowdowns occur in both runs then it's not the CBs.
Also, it's worth considering whether the slowdowns are caused by an external factor, eg. if some other program on your computer is sucking up all of the resources for some reason, eg. antivirus gone crazy. If you've got a second monitor, it might be worthwhile to play CK2 with task manager visible in that second monitor, so you can see if it's CK2 or another program that spikes to 100% CPU usage during these periodic slowdowns.