thestk / rtaudio

A set of C++ classes that provide a common API for realtime audio input/output across Linux (native ALSA, JACK, PulseAudio and OSS), Macintosh OS X (CoreAudio and JACK), and Windows (DirectSound, ASIO, and WASAPI) operating systems.
Other
1.51k stars 322 forks source link

Don't repeatedly compute device count when locating default device #305

Closed nyanpasu64 closed 3 years ago

nyanpasu64 commented 3 years ago

Previously, when calling RtApi::getDefaultInputDevice() or RtApi::getDefaultOutputDevice(), every iteration of the for loop until the default device was found (up to O(n)) would call getDeviceCount. On PulseAudio, this would call RtApiPulse::collectDeviceInfo and rescan the list of devices (O(n)), resulting in up to O(n^2) runtime with the number of devices. Additionally it could result in tearing where the device count changes midway through the loop.

This changes the code to cache the device count upfront, which shouldn't have any negative effects hopefully.

Tested on PulseAudio and ALSA.

Discovered at https://github.com/thestk/rtaudio/pull/296#issuecomment-890134199.

nyanpasu64 commented 3 years ago

poke