Profile picture
, 29 tweets, 7 min read Read on Twitter
This thread is about the status of the software we use and create every day. I'll start the thread saying that everything started when my girlfriend @wylfi and I participated on the @globalGameJam in Prague on January this year and I did't wanted to use any game engine.
Using game engines as GameMaker and Unity does not feel comfortable to me, the source code seems to rot and stop working with every new version of this engines. This is especially true in the case of Unity.
Also, I wanted to learn something new, and start coding in C++ and a media library called @sfmldev. I made some entity component and an animation handler, just to avoid writing basic stuff in the jam.
We manage to pull out a nice game prototype, with local multiplayer, joystick controller, sounds, animations and several bugs in less than 5 megabytes, assets included. Just to have some perspective, an empty unity executable is around 20MB.
I continued working on the prototype after the jam, managed to make it run in a raspberry pi at 60fps.
Then, I started to hear about Data Oriented Design from people like @mike_acton, @Jonathan_Blow and @cmuratori and I decided to give it a try and reimplement the prototype with a more data oriented approach.
So, I keep digging in data oriented techniques, and reimplemented the prototype. It was not easy, since I was unable to find material about developing games using data oriented design. The material available lack on examples in c++.
What I found on this road (which I keep walking). Is that the data-oriented design is easier to parallelize and is much more performant.
Going back to the image I post on the first tweet. On the left, we can see the Data Oriented Version, and in the Right, the Object Oriented Version. You'll notice 2 things: First, the DOV has way less context switches but consumes way more CPU. What this means?
The CPU has 3 levels of cache, if the data you need is not on this caches, the CPU need to go all the way to the memory to get this data. When the CPU need to get it's data from the main memory, it stalls for 200+ cycles.
Just to put things on perspective, the most expensive operations on the CPU(like square root) takes about 20 cycles.
Of course, there are floating operations that wakes way more time than the square root, but they are not that common. A float aTan is between 60 and 350 cycles. But I never directly used this operation in my whole life.
So, when the processor does not have to go tho the memory that often, it does not stalls, the Operative System will not create a context switch(will not use that core for something else). So, the program get's more CPU time, plus, the overhead of a context switching is minimized.
Mystery solved: We have higher processor usage because our software is running all the time. Can we do something about this? I'll say yes: There is ways to tell the operative system "Ok, don't need this core in a while, you can do something else. I'll sleep for X milliseconds"
When we "sleep" the core, the computer does not consume any power, our program is just waiting for a period of time. Is a more effective solution than skipping the frame inside a while loop.
So, I decided that I wanted to run the game at 60fps, an update the logic of the game also, 60 times per second. So what I did, was move the "draw" and "update" routines to different cores, and count how many milliseconds takes to go through the loops.
If the amount of time needed to process the loop was less than 1/60 I "sleep" the core for 1/60 - deltaT. This is for both draw and update routines.
What I had was this: Now I have a program that does the same as the original, same functionality, but only consumes 3% of the CPU.
This translates directly into less energy consumption. The original program had a score of 50, when this new version has a score of 3.5.
On my daily work, I use NodeJS, typescript and other web technologies, they feel totally bloated and slow compares with what I can do on C++ and a little of care. I wish I can do this types of optimizations all the time.
Now, maybe we don't care because we use desktop computers and server racks with 8 cores and unlimited amount of energy from the grid, but what about the software that runs on our phones? Most of the software uses way more energy that it should.
The battery will age faster and, with the time, it will become an environmental threat. This happens with millions of phones using millions of unoptimized apps on all platforms.
This is just an example on how crappy, inefficient software pollutes the planet. But you can imagine how much energy server farms takes from the grid or how many computers extra we are using to run our software. We are using hardware brute force to fix our performance problems.
It's not only about batteries, or hardware or energy consumed by processors, it's also about supporting infrastructure, like air conditioners to prevent our servers to melt.
My point is, software development is not as environmental friendly as we think. This is just an example of a simple program written in a very powerful language like C++. The whole world runs on software and we are doing it wrong.
Some raspberry pi magic at 60fps.
Please keep in mind that I'm not a hardware expert, I'm still trying to confirm my findings. Take my conclusions with a grain of salt and don't hesitate in recomend new ways to profile cache optimizations that I can perform in OSX or linux.
I don't want to post some conclusions without the code, because maybe I'm wrong. So, here is the code I was working on:
Data Oriented: github.com/WEREMSOFT/DODG…
Object Oriented: github.com/WEREMSOFT/Purf…
Don't hesitate on contacting me if you check my code and want to share your opinion.
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to WEREMSOFT
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!