mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 20:43:21 +00:00
Allow negative bytes
Gotta be optimistic about those memory usage optimizations
This commit is contained in:
parent
dae99b6661
commit
afab67e69c
2 changed files with 30 additions and 30 deletions
|
@ -3,35 +3,43 @@ use std::fmt;
|
||||||
|
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
pub struct MemoryUsage {
|
pub struct MemoryUsage {
|
||||||
pub allocated: Bytes,
|
pub allocated: Bytes,
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
MemoryUsage { allocated: Bytes(alloc) }
|
|
||||||
} else {
|
|
||||||
MemoryUsage { allocated: Bytes(0) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for MemoryUsage {
|
impl fmt::Display for MemoryUsage {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(fmt, "{}", self.allocated)
|
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 isize;
|
||||||
|
MemoryUsage { allocated: Bytes(alloc) }
|
||||||
|
} else {
|
||||||
|
MemoryUsage { allocated: Bytes(0) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
|
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
|
||||||
pub struct Bytes(usize);
|
pub struct Bytes(isize);
|
||||||
|
|
||||||
impl Bytes {
|
impl Bytes {
|
||||||
pub fn megabytes(self) -> usize {
|
pub fn megabytes(self) -> isize {
|
||||||
self.0 / 1024 / 1024
|
self.0 / 1024 / 1024
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,10 +49,10 @@ impl fmt::Display for Bytes {
|
||||||
let bytes = self.0;
|
let bytes = self.0;
|
||||||
let mut value = bytes;
|
let mut value = bytes;
|
||||||
let mut suffix = "b";
|
let mut suffix = "b";
|
||||||
if value > 4096 {
|
if value.abs() > 4096 {
|
||||||
value /= 1024;
|
value /= 1024;
|
||||||
suffix = "kb";
|
suffix = "kb";
|
||||||
if value > 4096 {
|
if value.abs() > 4096 {
|
||||||
value /= 1024;
|
value /= 1024;
|
||||||
suffix = "mb";
|
suffix = "mb";
|
||||||
}
|
}
|
||||||
|
@ -55,7 +63,7 @@ impl fmt::Display for Bytes {
|
||||||
|
|
||||||
impl std::ops::AddAssign<usize> for Bytes {
|
impl std::ops::AddAssign<usize> for Bytes {
|
||||||
fn add_assign(&mut self, x: usize) {
|
fn add_assign(&mut self, x: usize) {
|
||||||
self.0 += x;
|
self.0 += x as isize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -111,11 +111,7 @@ pub fn analysis_stats(
|
||||||
eprintln!("Total declarations: {}", num_decls);
|
eprintln!("Total declarations: {}", num_decls);
|
||||||
eprintln!("Total functions: {}", funcs.len());
|
eprintln!("Total functions: {}", funcs.len());
|
||||||
let item_collection_memory = ra_prof::memory_usage();
|
let item_collection_memory = ra_prof::memory_usage();
|
||||||
eprintln!(
|
eprintln!("Item Collection: {:?}, {}", analysis_time.elapsed(), item_collection_memory);
|
||||||
"Item Collection: {:?}, {}",
|
|
||||||
analysis_time.elapsed(),
|
|
||||||
item_collection_memory.allocated
|
|
||||||
);
|
|
||||||
|
|
||||||
if randomize {
|
if randomize {
|
||||||
shuffle(&mut rng, &mut funcs);
|
shuffle(&mut rng, &mut funcs);
|
||||||
|
@ -140,7 +136,7 @@ pub fn analysis_stats(
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"Parallel Inference: {:?}, {}",
|
"Parallel Inference: {:?}, {}",
|
||||||
inference_time.elapsed(),
|
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 inference_time = inference_time.elapsed();
|
||||||
let total_memory = ra_prof::memory_usage();
|
let total_memory = ra_prof::memory_usage();
|
||||||
eprintln!(
|
eprintln!("Inference: {:?}, {}", inference_time, total_memory - item_collection_memory);
|
||||||
"Inference: {:?}, {}",
|
|
||||||
inference_time,
|
|
||||||
total_memory.allocated - item_collection_memory.allocated
|
|
||||||
);
|
|
||||||
|
|
||||||
let analysis_time = analysis_time.elapsed();
|
let analysis_time = analysis_time.elapsed();
|
||||||
eprintln!("Total: {:?}, {}", analysis_time, total_memory);
|
eprintln!("Total: {:?}, {}", analysis_time, total_memory);
|
||||||
|
|
Loading…
Reference in a new issue