Using sound thread quadruples CPU usage

Not sure if anyone else noticed this, but I’ve always had an issue with Defold using so much more CPU than I’d expect it to. Today I was playing around with game.project and found out that disabling sound threading lowered average CPU usage from ~40% to ~10%.

Using sound thread

Not using sound thread

This is an HP Elitebook 840r G4 with Intel Core i5-7300U @ 2.60Gz running Windows 10.
I don’t necessarily need the threading, but this is interesting considering that thread on is default. I’ve been experiencing a lot of audio cracking and with this project from CPU issues (entire computer lags) and this had been a contributing to it.
Not sure what category to put this one in, but any thoughts?

Interesting. On macOS I see more or less the same CPU usage with and without the Sound Thread.

Can you share a bit more info about your project? How many sounds are you playing? Which format? Which sample rate? What does the engine show on startup?

INFO:SOUND:   DSP backend: Fallback
INFO:SOUND: Sound
INFO:SOUND:   nSamplesPerSec:   48000
INFO:SOUND: Mix format:
INFO:SOUND:   wFormatTag:       fffe  IEEE_FLOAT/PCM/EXTENSIBLE: 3 / 1 / fffe
INFO:SOUND:   nChannels:        2
INFO:SOUND:   nSamplesPerSec:   48000
INFO:SOUND:   nAvgBytesPerSec:  384000
INFO:SOUND:   nBlockAlign:      8
INFO:SOUND:   wBitsPerSample:   32
INFO:SOUND:   -> Format:        3  IEEE_FLOAT/PCM/EXTENSIBLE: 3 / 1 / fffe
INFO:SOUND:   DSP backend: SSE
INFO:SOUND: Sound
INFO:SOUND:   nSamplesPerSec:   48000

The game contains multiple sound files and components, but at the time it was only playing 1 song, an ogg file (loaded from disk and resource.set_sound’d onto a sound component) with a 44.1k sample rate.

Are you able to share this as an isolated project for us to test (you can email me at bjorn@defold.se) if you prefer.

battle test barebones.zip (6.6 MB)

Here’s a version of it with all the content removed. I checked whether it had the same effect, and it did.

Thanks. I’m still not able to reproduce the CPU usage difference. I’m seeing the same usage (~28-30%) with and without using sound threads. I stripped down the project further:

Archive.zip (3.3 MB)

Can you please check that this version behaves the same on your machine?

Note: I also did a change and set Update Frequency in game.project Display section from hardcoded 60 to 0 (variable).

I’d also like you to test the CPU usage with other tools, such as Process Explorer.
(The reason for that is that I’ve heard Windows’ CPU measurements may a little bit misleading sometimes)

This was again on macOS.

I’ve now also tested on Windows 10 on a fairly low end PC and when measuring with the Windows task Manager I barely reach 2% CPU usage with and without threads. It is very strange that we are getting so very different results.

INFO:SOUND: Mix format:
INFO:SOUND:   wFormatTag:       fffe  IEEE_FLOAT/PCM/EXTENSIBLE: 3 / 1 / fffe
INFO:SOUND:   nChannels:        2
INFO:SOUND:   nSamplesPerSec:   44100
INFO:SOUND:   nAvgBytesPerSec:  352800
INFO:SOUND:   nBlockAlign:      8
INFO:SOUND:   wBitsPerSample:   32
INFO:SOUND:   -> Format:        3  IEEE_FLOAT/PCM/EXTENSIBLE: 3 / 1 / fffe
INFO:SOUND:   DSP backend: SSE
INFO:SOUND: Sound
INFO:SOUND:   nSamplesPerSec:   44100

We do have different samples per second though…

Edit:

> file sound/mus_forest.ogg 
sound/mus_forest.ogg: Ogg data, Vorbis audio, stereo, 44100 Hz, ~224000 bps, created by: Xiph.Org libVorbis I

I wonder if it is upsampling from 44 to 48 which is the reason?

I still see the same effect, though Process Explorer does show it running with 66-80% of what task manager says, there is still a significant increase shown by it (without → 1-2%, with → 20-30%). I’ll now create a 48k version of the file to see if that’s the issue. But I am confused why the system is 48k instead of 44.1k? What is Defold reading when it prints these messages?

Just ran it with a 48k file, it does the exact same thing

It gets the devices from the WASAPI api, which gets it from your system.