Game Design, Programming and running a one-man games business…

Speeding up Democracy 4 simulation processing (atof is slow)

I just made a major speedup to the loading/next turn/new game code in Democracy 4, and thought I may as well share the gains with readers of this blog :D. It kind of makes me look a bit like an idiot to admit this is a big speedup (I should have known), but anyway, knowledge sharing (especially on optimization) is always good.

Fundamentally, the code in Democracy 4 is structured like a neural network. Without going into tons of details, every object in the game (policy, dilemma option, voter, voter group, situation…) is modeled as a neuron, which is basically just a named object connected by a ton of inputs and outputs to other neurons. You can run through the inputs and outputs, process the values and get a current value for a neuron at any time, which is done for every one of them, every turn.

Also… when we start a new game, I need ‘historical’ data for each value, so the game pre-processes the whole simulation about 30 times before you start to give us meaningful background data, and to ensure the current simulation sits as a reasonable equilibrium.

Those connections to neurons should probably be called dendrites or whatever, but I call them SIM_NeuralEffect. They contain basically the names of a host and a target (resolved to actual C++ pointers to objects), and an equation explaining the connection, and some other housekeeping stuff.

At the heart of it all, is an equation processor which lets you write this:

OilPrice,0+(0.22*x)*GDP

And actually turn it into a value for that effect, given the current situation. The Equation processor runs each turn, on every neural effect, and there are LOTS of them. Thus, if the equation processor is slow, its all slow.

I just installed a new version of the free vtune profiler from intel. Its not recognizing my ultra-amazing new chip, so only doing usermode sampling, but nonetheless it draws pretty flame charts like this:

Before I optimised

This is showing the code inside that 30-turn pre-game processing called PreCalcCoreSimulation. Lots of stuff goes on, but what I immediately noticed was all this atof stuff. Omgz. Thats a low level c runtime function, not one of mine, and it seems to be slowing down everything. This is a HUGE chunk of the whole equation processing code. How is this possible?

Now, you may think ‘dude, atof is pretty standard. No way are you are going to be able to make that code faster’, to which I reply ‘dude, obviously not. But the fastest code is code that never runs.’.

All those calls to atof are absolute nonsense.

Looking back at the equation above (OilPrice,0+(0.22*x)*GDP) there is obviously some stuff in there which is volatile. I do not know what the current value of x or GDP is, so I will need to grab their pointers and query them when I process the equation, but the rest of that stuff is static. That * is going to remain * and that 0 and 0.22 will remain fixed too. This is the key to a roughly 33% speedup of the whole processing in the game.

I actually did know to look into this, and I do not do manual text parsing of the equation each time. I parse them equation on startup, and stick the various values into buckets, so I am not wasting time each turn. But one thing I had not done is store the atof() outcomes. I was still storing variables[0] as ‘0’ instead of just 0.

Now you may think atof is fast. Its fast enough for most cases, but its WAY slower than just accessing the value of a floating point number thats already in RAM, and cached happily in the equation processor itself. Here is the new diagram:

Faster!

The difference is, (on my superfast PC), for the whole precalc simulation function: 0.85 vs 1.50 seconds. This probably makes me sound pedantic as hell, but I’m rocking some stupidly new and pricey PC, so there are likely people playing D4 on laptops a fifth the speed. I might be knocking a whole 3 seconds off the new game time for some players!

Also, and worth remembering, I just saved doing a ton of processing, which means a ton of CPU time, power and heat. If you can make your game run more quietly, more coolly, and faster on players PCs, you absolutely should do it.

Re-framing social media

I haven’t been on twitter for a few weeks, and its been good. Here is why.

I started doing a lot of thinking about the internet, and social media specifically, and I really do not like the current state of things. I read a truly excellent long blogpost about the bloat in web pages these days, that would be funny if it was not so tragic. It reminded me of a time when people in IT gave a flying fuck about performance, efficiency, and common sense, rather than ‘user engagement’ ‘metrics’ and ‘stock options’.

The moment you step aside from social media, you start really seeing it properly for the absolute dystopian hell that it is. Anybody with common sense would absolutely leave it immediately for their own health, and no doubt many do, but the rest of us who are still there do not even know that happens because… we only know about what happens on social media.

With thousands of ‘followers’ on social media, we are part of a rich ecosystem of always-on, always updating action. The fact that 10 people we knew just stopped posting entirely and disappeared will not even register to us because…oh look a kitten video.

Lets be perfectly clear. Twitter is a private company, not a service. Its sole reason to exist is to make as much money as possible. if that means destabilizing governments, wrecking democracies, driving people to suicide, and destroying the mental health of an entire generation or two, then nobody at twitter is going to care one bit. They have shown this to be true time and time again. Its still rife with abuse, hatred, misinformation and crypto-scam troll-farm bullshit.

If you are anything even vaguely like me, then you will know, deep down, that twitter, and similar sites like Facebook, are absolutely disastrous for your mental health. By participating in these sites you are putting up a big ‘open for business’ sign next to the emotional center of your brain, with an open invite to people with extreme, warped views and aggressively evangelical personalities to come unload their hatred and prejudices into your skull 24/7. Its the most destructive drug imaginable. Even with heroin or cocaine, you actually run out at some point, and need to call your dealer. With social medial, the dealer is right there, right next to you, with a constant fine-tuned supply being injected straight into your veins all the time, for free. Best of all, hardly any of you, statistically, will kill themselves, so the dealer can keep it up for longer.

The whole idea of craving more ‘followers’ on twitter is absolutely insane. Jesus had followers. I guess Gandhi did too. Maybe Martin Luther King. Certainly some horrible dictators also had many followers. But does someone who makes videos about knitting on the internet need ‘followers’? and do they need to know the exact number of them, all the time, updating hourly? This is objectively insane. Its a skinner box for faked charisma. Its pure evil. We cannot cope living like this.

I think I have 10-12,000 twitter followers the last time I looked. I don’t think its changed for years. Many are likely bots. Its depressing that I even know the number to that level of accuracy. We should not want to know. We should not want to play such a game. Many people I know DO play this game, because they are content creators (indie game developers) and they think this will raise their profile and sell more games. I think this is a mistake.

Here is a top tip: Take all the time, effort and energy you spend tweeting, replying, re-tweeting and scrolling on twitter and spend all that effort instead working in a coffee shop waiting tables. Then take the money from that 2nd job and spend it on advertising or improving your content (whether its indie games, art, books, whatever). Your ROI will be WAY better.

We all know people who ‘got famous through twitter’. Of course we do, because twitters algorithm will take any story along those lines and promote the fuck out of it. How many people do you actually really know personally who owe their fantastic success to their twitter account? Do you actually ever do any analysis on whether or not being viral on twitter sells more video games? Let me help you with that.

Here is one of the most famous people on the planet replying to a tweet at him by me:

WHOAH. I bet a TON of people checked out my twitter profile and then went and bought my games on apr 14th 2021 right? Lets see the spike:

Oh dear…nothing… ok Here is a tweet I did on a random whim after I saw a link about it. I didn’t even create the original content.

Ok… now we are talking. Thats pretty cool right. A tweet with 48,000 likes. Not bad cliffski, not bad at all, this is where we see the sales spike:

yeah…I’m kinda not seeing it. But maybe I’m a freak and my steam sales are never spiky. Lets looks at the current Christmas steam sale to check that theory:

Hmmm.

And YES, I get it… MAYBE some small number of people who see you tweeting something viral are going to check your profile and MAYBE some of those people will then be 1% more likely to buy your game in the next year and YES maybe its hard to track but…

I’ve been on twitter forever, and I basically have had about half a dozen ‘viral’ moments, and none of them have led to any noticeable boost to my business. If I could go back in time, never join twitter, and never join reddit or facebook, and get back ALL that time and convert it into more direct work on marketing or developing my game would I do it? Of course.

For those who like the numbers. Lets say those 48k likes represented clicks on an ad. Thats 48,000 clicks at maybe $0.05 CPC because its poorly targeted. $2,400. Assuming I’m due another viral tweet every 10 years, thats $240 a year for my social media addiction. yay?

Apart from anything, I really resent putting any effort into these social media platforms in the past only to realize way later that I’m basically an unpaid content creator for a billion dollar US-owned social media company. At least twitter have not YET started charging people money to ensure all your followers see your tweet (thats coming though…), whereas facebook blatantly flipped that switch years ago.

Twitter is disastrous for your mental health, and sites like facebook and reddit are even worse. Actually RANKING peoples opinions with up and downvotes? Are we serious here? Imagine going round a friends house for a few beers and a chat, and every time anybody said something people all held up up and downvote paddles. Does that sound like fucked-up psychotic behavior to you?

Full disclaimer: I advertise on social media. It works reasonably well, I wont stop doing that. But I left facebook a few years ago with no regrets, and only maintain facebook and reddit accounts because my games have pages on both networks. I’m not on tiktok or snapchat or instagram.

I’m calmer, and happier since I stopped using twitter, and I have a very, very hard time seeing any way in which its affecting my business. Frankly I have more time now and less distractions to actually improve my game. If you follow me on twitter you may see the odd post for some big event that I want to ensure any press following me see (like the upcoming release of Democracy 4 from Early Access), but in terms of casual day-to-day tweeting and banter on twitter? I think I’m going to save that for real-life friends from now on.

What exactly is it that you do here?

I’m in the middle of making deals with two different sets of what we shall call ‘contractors’ here, although in both cases they would probably be offended at the term, because somehow people get very exercised at pretending they are not working for somebody when they are in fact, working for somebody… (you can tell they are, because that person pays them)

This process is absolutely infuriating to me, because what my ‘probably on the spectrum’ brain thinks should happen, and what actually happens is so completely and utterly different. Here is how I expect things to go when I need some work doing by a third party.

  • Cliff searches internet for lists of people who do X.
  • Cliff sees the prices each contractor quotes to do X
  • Cliff selects the best price/experience/talent combination for his needs and a contract is signed.

Thats not vaguely what happens, because what actually happens is more like this.

  • Cliff searches internet for lists of people who do X.
  • Cliff finds that each individual has a contact form, but no actual prices.
  • Cliff fills out contact forms and sends emails to people who randomly guesses are affordable, but has no real clue
  • Strange middleman Y who works for X, replies and suggests that cliff ‘hops on a call to discuss the job with Y, who I should repeat is not X, and is apparently somehow ‘front-running’ the whole deal making process.
  • After declining this social invitation, Y is now prone to asking cliff was his budget is, or what his expectations are, or what will happen to the work after its done and other such bullshit.
  • Cliff gives up and just doesn’t get the work done at all, in despair.

The whole thing just reminds me of office space:

A classic film, with a great scene where someone whose entire job description is that they serve as a pointless middleman between the customers and the workers, desperately tries to justify his existence and continued employment.

Fuck this.

If I go to a shop to buy some bread, it has a price on the product. I can buy it, or not buy it. Thats not to demean the bread, it could be a cheap sliced loaf, or an exotic sounding freshly baked warm artisanal loaf that costs 5x as much. Thats fine. I get that quality costs money.

What I don’t get, thank fuck, is some salesperson leaping in front of the bread and saying “Hi there, you seem to be interested in working with our range of yeast-based consumable experiences, what kind of budget do you have in mind? I can talk to our bakers and see if they can provide something that meets your expectations, but first let me talk a bit about what a great deal you will be getting if you choose to go for a sourdough option”.

I do not care if you sell bread, artwork, music, PR services, animation, full motion video, advertising, I really do not give a fuck. All I want is a price, and as few middle-men as possible.

I recently bought a pretty pricey car. I did it with one click on a website. I strongly suspect the only human involved in me getting delivery of the car will be the person who physically hands me a key to it when its in the UK. I did not need, nor did I pay for, a middle-man to explain to me what wheel options exist or comparing the different trims. This is all on the website.

Imagine that, a website where you describe the product or service is plain text, saving us all the futility of inserting some useless, waste-of-space middleman into the transaction.

BTW, some services (game dev related) I recently tried to get would cost $5-10,000. The middlemen responsible for ‘representing’ those service providers did not even reply to me. 10 seconds google will tell you that yup, cliffski can probably afford those services, and isn’t likely to fuck around, or fail to pay.

In this case, not only did the middle-men add no value, they actually destroyed it, and the gullible fools who hired them will never know about all the money they should have earned easily, but missed.

And here is the thing: some people think hiring an agent will make them MORE money. No. In my case, not only and I likely to pay WAY LESS for someone with an agent (because I know the real cost of their services is way lower, due to the agent needing to be paid), but its actually a net negative, because by wasting my time talking to a pointless intermediary, you have actually provided a WORSE service for me.

Plus I KNOW if I have to haggle with someone over pricing, that its because they are trying to size me up and squeeze more money out of me. Its the online equivalent of someone quoting more to do your plumbing because they notice your nice car. Its insulting, its rude, and aggravating, and it just means you are training your customers to be aggressive, confrontational, penny-pinching bastards. Is that how people want to do business?

Needless to say, I don’t have an agent. If someone wants to contact me, cliff AT positech dot co dot uk is my email address, Direct to me.