Skip to content

Commit 421be84

Browse files
committed
refactor: util::mem_file_check to have a platform adjustment factor
- also use atoi_simd instead of parse
1 parent 508d1ce commit 421be84

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/util.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -849,9 +849,10 @@ pub fn file_metadata(md: &fs::Metadata) -> (u64, u64) {
849849
/// Check if there is enough memory to process the file.
850850
/// Return the maximum file size that can be processed.
851851
/// If the file is larger than the maximum file size, return an error.
852-
/// If memcheck is true, check memory in CONSERVATIVE mode (i.e., Filesize < AVAIL memory + SWAP -
853-
/// headroom) If memcheck is false, check memory in NORMAL mode (i.e., Filesize < TOTAL memory -
854-
/// headroom)
852+
/// If memcheck is true, check memory in CONSERVATIVE mode
853+
/// (i.e., Filesize < (AVAIL memory + SWAP) * platform_factor - headroom)
854+
/// If memcheck is false, check memory in NORMAL mode
855+
/// (i.e., Filesize < TOTAL memory - headroom)
855856
pub fn mem_file_check(
856857
path: &Path,
857858
version_check: bool,
@@ -868,8 +869,9 @@ pub fn mem_file_check(
868869
let conservative_memcheck_work = get_envvar_flag("QSV_MEMORY_CHECK") || conservative_memcheck;
869870

870871
let mut mem_pct = env::var("QSV_FREEMEMORY_HEADROOM_PCT")
871-
.unwrap_or_else(|_| DEFAULT_FREEMEMORY_HEADROOM_PCT.to_string())
872-
.parse::<u8>()
872+
.map(|val| {
873+
atoi_simd::parse::<u8>(val.as_bytes()).unwrap_or(DEFAULT_FREEMEMORY_HEADROOM_PCT)
874+
})
873875
.unwrap_or(DEFAULT_FREEMEMORY_HEADROOM_PCT);
874876

875877
// if QSV_FREEMEMORY_HEADROOM_PCT is 0, we skip the memory check
@@ -887,11 +889,30 @@ pub fn mem_file_check(
887889
// nor above 90% memory headroom as its too memory-restrictive
888890
mem_pct = mem_pct.clamp(10, 90);
889891

892+
// Platform-specific adjustment factors for conservative mode
893+
// These account for OS-specific memory management capabilities
894+
#[cfg(target_os = "macos")]
895+
let platform_factor = 1.3; // macOS has aggressive memory compression & dynamic swap
896+
897+
#[cfg(target_os = "linux")]
898+
let platform_factor = 1.15; // Linux page cache is reclaimable, but be conservative
899+
900+
#[cfg(target_os = "windows")]
901+
let platform_factor = 1.0; // Windows memory reporting is already conservative
902+
903+
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
904+
let platform_factor = 1.0; // Other platforms: no adjustment
905+
906+
// Calculate maximum available memory based on mode
890907
#[allow(clippy::cast_precision_loss)]
891908
let max_avail_mem = if conservative_memcheck_work {
892-
((avail_mem + free_swap) as f32 * ((100 - mem_pct) as f32 / 100.0_f32)) as u64
909+
// CONSERVATIVE: Use available + swap, with platform adjustments
910+
let base_mem = (avail_mem + free_swap) as f64;
911+
let adjusted_mem = base_mem * platform_factor;
912+
(adjusted_mem * ((100 - mem_pct) as f64 / 100.0)) as u64
893913
} else {
894-
(total_mem as f32 * ((100 - mem_pct) as f32 / 100.0_f32)) as u64
914+
// NORMAL: Use total memory
915+
(total_mem as f64 * ((100 - mem_pct) as f64 / 100.0)) as u64
895916
};
896917

897918
// if we're calling this from version(), we don't need to check the file size

0 commit comments

Comments
 (0)