From afab67e69c39027fb99878751309d4050324beef Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 09:47:16 +0200 Subject: Allow negative bytes Gotta be optimistic about those memory usage optimizations --- crates/ra_prof/src/memory_usage.rs | 32 ++++++++++++++++---------- crates/rust-analyzer/src/cli/analysis_stats.rs | 14 +++-------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/crates/ra_prof/src/memory_usage.rs b/crates/ra_prof/src/memory_usage.rs index 857b51321..22b61e4a2 100644 --- a/crates/ra_prof/src/memory_usage.rs +++ b/crates/ra_prof/src/memory_usage.rs @@ -3,16 +3,30 @@ use std::fmt; use cfg_if::cfg_if; +#[derive(Copy, Clone)] pub struct MemoryUsage { pub allocated: Bytes, } +impl fmt::Display for MemoryUsage { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "{}", self.allocated) + } +} + +impl std::ops::Sub for MemoryUsage { + type Output = MemoryUsage; + fn sub(self, rhs: MemoryUsage) -> MemoryUsage { + MemoryUsage { allocated: self.allocated - rhs.allocated } + } +} + impl MemoryUsage { pub fn current() -> MemoryUsage { cfg_if! { if #[cfg(target_os = "linux")] { // Note: This is incredibly slow. - let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as usize; + let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as isize; MemoryUsage { allocated: Bytes(alloc) } } else { MemoryUsage { allocated: Bytes(0) } @@ -21,17 +35,11 @@ impl MemoryUsage { } } -impl fmt::Display for MemoryUsage { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.allocated) - } -} - #[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] -pub struct Bytes(usize); +pub struct Bytes(isize); impl Bytes { - pub fn megabytes(self) -> usize { + pub fn megabytes(self) -> isize { self.0 / 1024 / 1024 } } @@ -41,10 +49,10 @@ impl fmt::Display for Bytes { let bytes = self.0; let mut value = bytes; let mut suffix = "b"; - if value > 4096 { + if value.abs() > 4096 { value /= 1024; suffix = "kb"; - if value > 4096 { + if value.abs() > 4096 { value /= 1024; suffix = "mb"; } @@ -55,7 +63,7 @@ impl fmt::Display for Bytes { impl std::ops::AddAssign for Bytes { fn add_assign(&mut self, x: usize) { - self.0 += x; + self.0 += x as isize; } } diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 66d201ba6..cf0d82b62 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -111,11 +111,7 @@ pub fn analysis_stats( eprintln!("Total declarations: {}", num_decls); eprintln!("Total functions: {}", funcs.len()); let item_collection_memory = ra_prof::memory_usage(); - eprintln!( - "Item Collection: {:?}, {}", - analysis_time.elapsed(), - item_collection_memory.allocated - ); + eprintln!("Item Collection: {:?}, {}", analysis_time.elapsed(), item_collection_memory); if randomize { shuffle(&mut rng, &mut funcs); @@ -140,7 +136,7 @@ pub fn analysis_stats( eprintln!( "Parallel Inference: {:?}, {}", inference_time.elapsed(), - ra_prof::memory_usage().allocated + ra_prof::memory_usage() ); } @@ -297,11 +293,7 @@ pub fn analysis_stats( let inference_time = inference_time.elapsed(); let total_memory = ra_prof::memory_usage(); - eprintln!( - "Inference: {:?}, {}", - inference_time, - total_memory.allocated - item_collection_memory.allocated - ); + eprintln!("Inference: {:?}, {}", inference_time, total_memory - item_collection_memory); let analysis_time = analysis_time.elapsed(); eprintln!("Total: {:?}, {}", analysis_time, total_memory); -- cgit v1.2.3