Initial
Rust validation / Formatting (push) Failing after 27s
EonaCat/ci-workflows: SonarQube / sonar (push) Successful in 47s
Rust validation / Windows x64 build and tests (push) Canceled after 0s

This commit is contained in:
EonaCat committed 2026-12-04 21:03:25 +01:00
1 parent 2cff35f215
commit 1b0d534e6c
358 files changed
+253978 -74

No files matched your search

+130
View File
@@ -0,0 +1,130 @@
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
use windows::Win32::Graphics::Direct3D12::ID3D12Resource;
pub(crate) fn on() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("EONACAT_XS_BUFFER_POOL").is_some_and(|v| v == "1"))
}
const REST_MS: u64 = 100;
const LARGEST: u64 = 16 << 20;
const MOST: u64 = 1 << 30;
const IDLE_MS: u64 = 30_000;
#[derive(Clone, Copy)]
pub(crate) enum Kind {
Shared = 0,
Upload = 1,
#[allow(dead_code)]
Card = 2,
}
struct Kept {
buffer: ID3D12Resource,
data: usize,
class: u64,
unheld_since: u64,
}
unsafe impl Send for Kept {}
struct Pool {
kept: Vec<Kept>,
bytes: u64,
}
static POOLS: [Mutex<Pool>; 3] = [const { Mutex::new(Pool { kept: Vec::new(), bytes: 0 }) }; 3];
static COUNTS: [[AtomicU64; 4]; 3] = [const { [const { AtomicU64::new(0) }; 4] }; 3];
pub(crate) fn class(size: u64) -> Option<u64> {
(size <= LARGEST).then(|| size.max(1 << 16).next_power_of_two())
}
pub(crate) fn take(kind: Kind, class: u64) -> Option<(ID3D12Resource, *mut u8)> {
let now = super::now_ms();
let mut pool = POOLS[kind as usize].lock().unwrap_or_else(|e| e.into_inner());
let mut found = None;
let mut gone = 0;
let mut i = 0;
while i < pool.kept.len() {
let k = &mut pool.kept[i];
if super::references(&k.buffer) > 1 {
k.unheld_since = 0;
i += 1;
continue;
}
if k.unheld_since == 0 {
k.unheld_since = now;
} else if now - k.unheld_since >= IDLE_MS {
let k = pool.kept.swap_remove(i);
gone += k.class;
COUNTS[kind as usize][3].fetch_add(1, Relaxed);
continue;
} else if found.is_none() && k.class == class && now - k.unheld_since >= REST_MS {
k.unheld_since = 0;
found = Some((k.buffer.clone(), k.data as *mut u8));
}
i += 1;
}
pool.bytes -= gone;
drop(pool);
if found.is_some() {
let n = COUNTS[kind as usize][0].fetch_add(1, Relaxed) + 1;
if n.is_power_of_two() && n >= 64 {
say(kind);
}
}
found
}
pub(crate) fn keep(kind: Kind, class: u64, buffer: &ID3D12Resource, data: *mut u8) {
let mut pool = POOLS[kind as usize].lock().unwrap_or_else(|e| e.into_inner());
if pool.bytes + class > MOST {
COUNTS[kind as usize][2].fetch_add(1, Relaxed);
return;
}
pool.bytes += class;
pool.kept.push(Kept { buffer: buffer.clone(), data: data as usize, class, unheld_since: 0 });
COUNTS[kind as usize][1].fetch_add(1, Relaxed);
}
fn say(kind: Kind) {
let c = &COUNTS[kind as usize];
let pool = POOLS[kind as usize].lock().unwrap_or_else(|e| e.into_inner());
tracing::info!(
kind = ["shared", "upload", "card"][kind as usize],
again = c[0].load(Relaxed),
made = c[1].load(Relaxed),
not_kept = c[2].load(Relaxed),
let_go = c[3].load(Relaxed),
kept = pool.kept.len(),
kept_mb = pool.bytes >> 20,
"a list's buffers taken again instead of made (EONACAT_XS_BUFFER_POOL=1)"
);
}
#[cfg(test)]
mod tests {
use super::class;
#[test]
fn sizes_go_to_powers_of_two_from_64_kib() {
assert_eq!(class(1), Some(1 << 16));
assert_eq!(class(1 << 16), Some(1 << 16));
assert_eq!(class((1 << 16) + 1), Some(1 << 17));
assert_eq!(class(16 << 20), Some(16 << 20));
assert_eq!(class((16 << 20) + 1), None);
}
}