Making a pinball game for Playdate: Part 13, the performance tools

Welcome to this December adventure, where I will try to write about the process of our latest game, Devils on the Moon pinball.

If you enjoy this article consider wishlisting our game on Steam! I will talk more about the PC version of the game in later logs, but every wishlist helps.


If you have developed a game on the Playdate you might have got to the point where you start having performance issues.

This also tends to happen at the worst possible time, when you are trying to finish your game and adding all the content that you had planned. And if you have been making this game for, I don't know, two years, it can be really hard to figure out what to optimize.

This has happened to us before with Pullfrog Deluxe. We were adding the last couple of ideas we had for special blocks, and with the last one it was a little too much for the Playdate.

starblock-with-text-export.gif

It's something that can happen to you even if you have experience doing games elsewhere like mobile or PC.

Hardware nowadays is so fast that you can almost always do the naive thing with your small indie 2D game. Well, the Playdate's CPU, even though it's not slow, isn't infinitely fast. And I think it's one of the most common issues that happen when you start developing games for it.

The sampler

There are a couple of tools you can use. I talked a little about them in my previous post about profiling.

One tool I always have a hard time using is the sampler which tells you how many times a function has run. It's common to have a function that is running way more times than you expected.

For example on our game we saw that the mem_set function was running way more times than we expected.

sampler.png

Our physics system runs 4 times on every frame. One thing you need to do in a physics system is to collect colliding pairs.

You do your broad collision detection and mark which entities are colliding with who.

One thing you want to avoid is checking the same pair multiple times so you need some bookkeeping that is cheaper than your broad collision detection. One way of doing it is using bit flags. You can read a more in depth explanation in the book Real-Time Collision Detection in the section Avoid retesting.

It works like this: you generate a bit array that can hold a bit per entity pair. When you get a pair of entities you want to check, first you check if their pair bit is set. If it is, you can skip the full test. If not, you do the full test and then mark the bit.

Simple and effective. Well as the game grew and we created more entities we ended up with around 450, this means that our bit array needed to be 450 * 449 / 2 = 101025 bits. We create and clear this array every physics frame so 4 times every frame, we end up clearing ~49KiB of memory every frame. I skipped the last part in that book's chapter but the warning was there!

Even for a modest number of objects, this operation now quickly becomes very expensive.

Another option the book recommends is to keep a timestamp on each body and each time we do a query we make sure the timestamp equals the query timestamp.

After thinking about it for a bit, in our game we only have colliding pairs between balls and bodies and we have a single ball. Sure I would like to be able to add multiball to our game or any other table but then again it would be 3 balls at most, so I decided to just keep a small linear array and scan it every time we need to check if those pairs are already present.

No more memory clearing and we gained a fair bit of performance there.

The problem is, we know the game runs faster, we have a simple FPS counter and you can see the graph on the resource view of the Playdate SDK but it's hard to measure how fast. Or, even worse, whether something else became slower because of your change.

device-info.png

The profiler, again

To measure this one tool you can use is a profiler. The problem is that there is no built-in profiler for the Playdate. And the existing solutions tend to be too big for it.

There are also nuanced differences between profilers that I didn't realize before starting this project.

What I have always used before in other projects is what is called a sampling profiler, an automated tool that measures the code that you already have by interrupting the process and measuring how long each part of your code took.

Another type is an instrumentation profiler where you manually add zones to your code to measure specific areas you are interested in.

Each of them has its own pros and cons. For example the biggest and most obvious pro for the sampling profiler is that you don't really need to do much to gain a lot of information about your game code. You just run your game, play for a bit, and then go to the profiler and analyze the data that it captured.

But this was really hard to do on the Playdate, you don't have the permissions you would need to modify how a process runs. The instrumentation profiler seemed more feasible because I could manually edit my code and add the zones where I thought the game was being slow.

I already talked about the options for instrumentation profilers in my previous post and we ended up using Spall for a couple of months at the start of the project.

spall.png

At first the project didn't have that many things going on so it wasn't that hard to start adding zones everywhere, I also made the mistake of trying to measure a lot of small functions. I wanted to replicate the overview that a sampling profiler could give thinking the only cost was adding the instrumentation code, which would just take a couple of hours.

Turns out that a sampling profiler can measure all the functions because they use low-level APIs that allow them to collect a lot of data really fast, but with an instrumentation profiler a lot of times the instrumentation code was slower than the small functions I wanted to measure, which added a lot of noise to the report.

Another issue that I was having was with how Spall works, you pass a memory buffer and it fills it with as much data as possible and when it's filled it writes the data to disk and starts filling the buffer again. Turns out, the Playdate is quite slow writing data to disk and if you try to measure a lot of zones then the memory buffer fills quickly. So I could measure around 5 seconds of data before the game slowed to a crawl and then the data wasn't useful at all.

So there was nothing left to do but write our own profiler.

But this post is a little bit long already, so once again, wishlist the game on Steam and see you on the next adventure.

Comments