mirror of
https://github.com/arabianq/pipewire-soundpad.git
synced 2026-04-28 06:21:23 +00:00
perf: optimize AudioPlayer::update to avoid blocking the Tokio executor thread (#32)
Offload synchronous `fs::File::open` and `Decoder::try_from` operations to `tokio::task::spawn_blocking` in `AudioPlayer::update`. This allows the Tokio runtime to process other asynchronous tasks concurrently without being blocked by file I/O operations and audio header decoding during looped playback. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
c99d0749e3
commit
498d0d25af
@@ -374,16 +374,31 @@ impl AudioPlayer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut restart_futures = vec![];
|
||||||
|
|
||||||
for id in restarts {
|
for id in restarts {
|
||||||
if let Some(sound) = self.tracks.get_mut(&id) {
|
if let Some(sound) = self.tracks.get(&id) {
|
||||||
if let Ok(file) = fs::File::open(&sound.path) {
|
let path = sound.path.clone();
|
||||||
|
let handle = tokio::task::spawn_blocking(move || {
|
||||||
|
if let Ok(file) = fs::File::open(&path) {
|
||||||
if let Ok(source) = Decoder::try_from(file) {
|
if let Ok(source) = Decoder::try_from(file) {
|
||||||
|
return Some((id, source));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
});
|
||||||
|
restart_futures.push(handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for handle in restart_futures {
|
||||||
|
if let Ok(Some((id, source))) = handle.await {
|
||||||
|
if let Some(sound) = self.tracks.get_mut(&id) {
|
||||||
sound.sink.append(source);
|
sound.sink.append(source);
|
||||||
sound.sink.play();
|
sound.sink.play();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
self.tracks
|
self.tracks
|
||||||
.retain(|_, sound| !sound.sink.empty() || sound.looped);
|
.retain(|_, sound| !sound.sink.empty() || sound.looped);
|
||||||
|
|||||||
Reference in New Issue
Block a user