From 585a2f711371fcb9299a18911031a0ceb0301047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Dobra?= Date: Sat, 13 Nov 2021 12:36:22 +0100 Subject: [PATCH] Minor performance optimizations (#89) * Optimize performance by calling FreePlayedBlocks rarely Don't call it at every main loop iteration, only after someTime. * Optimize more aggressively in Release mode --- SpaceCadetPinball/Sound.cpp | 4 ++-- SpaceCadetPinball/Sound.h | 2 +- SpaceCadetPinball/SpaceCadetPinball.vcxproj | 8 ++++++++ SpaceCadetPinball/WaveMix.cpp | 9 +++++---- SpaceCadetPinball/WaveMix.h | 2 +- SpaceCadetPinball/winmain.cpp | 8 ++++++-- 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/SpaceCadetPinball/Sound.cpp b/SpaceCadetPinball/Sound.cpp index 0225bec..acdf672 100644 --- a/SpaceCadetPinball/Sound.cpp +++ b/SpaceCadetPinball/Sound.cpp @@ -114,10 +114,10 @@ void Sound::Enable(int channelFrom, int channelTo, int enableFlag) } } -void Sound::Idle() +void Sound::Idle(bool performCleanup) { if (pMem) - WaveMix::Pump(); + WaveMix::Pump(performCleanup); } void Sound::Activate() diff --git a/SpaceCadetPinball/Sound.h b/SpaceCadetPinball/Sound.h index c599fe5..f8dbf48 100644 --- a/SpaceCadetPinball/Sound.h +++ b/SpaceCadetPinball/Sound.h @@ -6,7 +6,7 @@ class Sound public: static int Init(HINSTANCE hInstance, int voices, void (* someFuncPtr)(int, MIXWAVE*, int)); static void Enable(int channelFrom, int channelTo, int enableFlag); - static void Idle(); + static void Idle(bool performCleanup = true); static void Activate(); static void Deactivate(); static void Close(); diff --git a/SpaceCadetPinball/SpaceCadetPinball.vcxproj b/SpaceCadetPinball/SpaceCadetPinball.vcxproj index 5333673..d8a8e76 100644 --- a/SpaceCadetPinball/SpaceCadetPinball.vcxproj +++ b/SpaceCadetPinball/SpaceCadetPinball.vcxproj @@ -308,6 +308,8 @@ true pch.h true + AnySuitable + Speed Windows @@ -350,6 +352,8 @@ false pch.h true + AnySuitable + Speed Windows @@ -392,6 +396,8 @@ true pch.h true + AnySuitable + Speed Windows @@ -434,6 +440,8 @@ false pch.h true + AnySuitable + Speed Windows diff --git a/SpaceCadetPinball/WaveMix.cpp b/SpaceCadetPinball/WaveMix.cpp index e07524c..fae0da6 100644 --- a/SpaceCadetPinball/WaveMix.cpp +++ b/SpaceCadetPinball/WaveMix.cpp @@ -550,7 +550,7 @@ int WaveMix::Activate(HANDLE hMixSession, bool fActivate) return 0; } -void WaveMix::Pump() +void WaveMix::Pump(bool performCleanup) { Globals = GlobalsActive; if (GlobalsActive) @@ -567,7 +567,8 @@ void WaveMix::Pump() else xHDR = xHDR->QNext; } - FreePlayedBlocks(); + if (performCleanup) + FreePlayedBlocks(); while (MixerPlay(GetWaveBlock(), 1)); } } @@ -1382,12 +1383,12 @@ int WaveMix::GetConfig(HANDLE hMixSession, MIXCONFIG* lpConfig) unsigned WaveMix::MyWaveOutGetPosition(HWAVEOUT hWaveOut, int fGoodGetPos) { - mmtime_tag pmmt{}; + MMTIME pmmt{}; if (!fGoodGetPos) return (timeGetTime() - Globals->dwBaseTime) * Globals->PCM.wf.nAvgBytesPerSec / 0x3E8 & 0xFFFFFFF8; pmmt.wType = TIME_BYTES; - waveOutGetPosition(hWaveOut, &pmmt, 0xCu); + waveOutGetPosition(hWaveOut, &pmmt, sizeof(MMTIME)); return Globals->pfnSampleAdjust(pmmt.u.ms, Globals->dwWaveOutPos); } diff --git a/SpaceCadetPinball/WaveMix.h b/SpaceCadetPinball/WaveMix.h index d089938..78dae28 100644 --- a/SpaceCadetPinball/WaveMix.h +++ b/SpaceCadetPinball/WaveMix.h @@ -151,7 +151,7 @@ public: static MIXWAVE* OpenWave(HANDLE hMixSession, LPCSTR szWaveFilename, HINSTANCE hInst, unsigned int dwFlags); static int FreeWave(HANDLE hMixSession, MIXWAVE* lpMixWave); static int Activate(HANDLE hMixSession, bool fActivate); - static void Pump(); + static void Pump(bool performCleanup = true); static int Play(MIXPLAYPARAMS* lpMixPlayParams); private: diff --git a/SpaceCadetPinball/winmain.cpp b/SpaceCadetPinball/winmain.cpp index 688f5f5..b71c8ad 100644 --- a/SpaceCadetPinball/winmain.cpp +++ b/SpaceCadetPinball/winmain.cpp @@ -228,9 +228,10 @@ int winmain::WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi float sleepRemainder = 0, frameDuration = TargetFrameTime; while (true) { - if (!someTimeCounter) + if (someTimeCounter == 0) { someTimeCounter = 300; + if (DispFrameRate) { auto curTime = timeGetTime(); @@ -257,7 +258,10 @@ int winmain::WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi } } - Sound::Idle(); + // Don't look for and free already played blocks at every single frame, + // as it is quite costly to look up. Only do that "sometimes". + bool performSoundCleanup = someTimeCounter == 300; + Sound::Idle(performSoundCleanup); if (!ProcessWindowMessages() || bQuit) break;