Merge pull request #22 from Indy2l/perf/helper-priority
perf(cores): run the submitting thread's helpers at its priority
This commit is contained in:
5 files changed
+62
-1
No files matched your search
@@ -111,6 +111,54 @@ fn ask(what: &str, from: u32) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the threads the submitting thread waits for run at its priority
|
||||
/// (`VICTORY_HELPER_PRIORITY`, on unless `0`; `VICTORY_AB` can flip it while
|
||||
/// the title runs): above normal, ahead of the title's threads and of the
|
||||
/// threads that translate lists. They are the threads that spread its work
|
||||
/// (`shadow::wide`, the pools of `land`), the one that makes the heaps'
|
||||
/// records and the one that passes over the heap's views beside the lists.
|
||||
///
|
||||
/// On a processor with six cores (i5-12400F, 12 threads) the title's threads
|
||||
/// and eight translating lists kept every core busy, and the submitting
|
||||
/// thread (above normal since `queue::behind`) stood waiting for helpers at
|
||||
/// normal priority that were not running. Measured with `VICTORY_AB` in one
|
||||
/// run (run10, Series S at 540p, medians of 5-second windows in the world,
|
||||
/// off / on): 8.6 / 9.6 fps, refreshes 23.1 / 17.6 ms a frame, waiting for
|
||||
/// the heaps' records 6.2 / 2.5 ms, the card idle behind the submitting
|
||||
/// thread 24.7 / 13.2 ms. Lowering the translating threads below normal as
|
||||
/// well made each list call about 5 % slower, with no clear gain of its own.
|
||||
fn helpers_raised() -> bool {
|
||||
crate::toggled!("VICTORY_HELPER_PRIORITY")
|
||||
}
|
||||
|
||||
/// A helper's priority ([`helpers_raised`]). [`Self::keep`] is called before
|
||||
/// each piece of work; it asks the system only when the switch has changed.
|
||||
pub(crate) struct Priority {
|
||||
applied: Option<bool>,
|
||||
}
|
||||
|
||||
impl Priority {
|
||||
pub(crate) fn new() -> Self {
|
||||
let mut priority = Self { applied: None };
|
||||
priority.keep();
|
||||
priority
|
||||
}
|
||||
|
||||
pub(crate) fn keep(&mut self) {
|
||||
use windows_sys::Win32::System::Threading::{
|
||||
GetCurrentThread, SetThreadPriority, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_NORMAL,
|
||||
};
|
||||
let on = helpers_raised();
|
||||
if self.applied == Some(on) {
|
||||
return;
|
||||
}
|
||||
let priority = if on { THREAD_PRIORITY_ABOVE_NORMAL } else { THREAD_PRIORITY_NORMAL };
|
||||
// SAFETY: our own thread.
|
||||
unsafe { SetThreadPriority(GetCurrentThread(), priority) };
|
||||
self.applied = Some(on);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -272,6 +272,8 @@ pub(crate) fn begin_sync_heaps() -> SyncedHeaps {
|
||||
STARTED.call_once(|| {
|
||||
let made = std::thread::Builder::new().name("victory-heap-sync".into()).spawn(|| {
|
||||
crate::cores::keep_on_fast_cores("victory-heap-sync");
|
||||
// (The submitting thread waits for its rounds.)
|
||||
let mut priority = crate::cores::Priority::new();
|
||||
let rounds = || SYNC_ROUNDS.0.lock().unwrap_or_else(|e| e.into_inner());
|
||||
/// A round is over, however it ends.
|
||||
struct Over;
|
||||
@@ -300,6 +302,7 @@ pub(crate) fn begin_sync_heaps() -> SyncedHeaps {
|
||||
}
|
||||
}
|
||||
drop(r);
|
||||
priority.keep();
|
||||
let _over = asked.then(|| {
|
||||
last = std::time::Instant::now();
|
||||
Over
|
||||
|
||||
@@ -67,7 +67,12 @@ pub(crate) fn begin(work: impl FnOnce() -> Vec<Refresh> + Send + 'static) -> std
|
||||
.name("victory-aside".into())
|
||||
.spawn(move || {
|
||||
crate::cores::keep_on_fast_cores("victory-aside");
|
||||
jobs.into_iter().for_each(|job| job())
|
||||
// (The submitting thread waits for its pass at the lists' close.)
|
||||
let mut priority = crate::cores::Priority::new();
|
||||
jobs.into_iter().for_each(|job| {
|
||||
priority.keep();
|
||||
job()
|
||||
})
|
||||
})
|
||||
.ok()
|
||||
.map(|_| Mutex::new(send))
|
||||
|
||||
@@ -250,6 +250,8 @@ impl Pool {
|
||||
}
|
||||
|
||||
fn help(&self) {
|
||||
// (The thread that calls `each` waits for this one's share.)
|
||||
let mut priority = crate::cores::Priority::new();
|
||||
let mut state = self.state();
|
||||
loop {
|
||||
let Some((work, i)) = take(&mut state) else {
|
||||
@@ -262,6 +264,7 @@ impl Pool {
|
||||
// SAFETY: counted in `running`, so `each` has not returned.
|
||||
let ran = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| unsafe { (*work.0)(i) }));
|
||||
working.end();
|
||||
priority.keep();
|
||||
state = self.state();
|
||||
state.running -= 1;
|
||||
state.panicked |= ran.is_err();
|
||||
|
||||
@@ -1562,8 +1562,10 @@ fn workers() -> &'static Workers {
|
||||
for i in 0..wide_threads() {
|
||||
let _ = std::thread::Builder::new().name(format!("victory-wide-{i}")).spawn(move || {
|
||||
crate::cores::keep_helper_on_fast_cores("victory-wide");
|
||||
let mut priority = crate::cores::Priority::new();
|
||||
let workers = WORKERS.get().expect("made above");
|
||||
loop {
|
||||
priority.keep();
|
||||
let job = {
|
||||
// A little while awake first: the next work comes
|
||||
// within microseconds more often than not.
|
||||
|
||||
Reference in new issue
Block a user