5579: Allow negative bytes r=matklad a=matklad

Gotta be optimistic about those memory usage optimizations



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-07-30 07:58:06 +00:00 committed by GitHub
commit 402433470a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 26 deletions

View File

@ -3,9 +3,22 @@
use cfg_if::cfg_if;
#[derive(Copy, Clone)]
pub struct MemoryUsage {
pub allocated: Bytes,
pub resident: 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 {
@ -13,26 +26,20 @@ 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), resident: Bytes(0) }
let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as isize;
MemoryUsage { allocated: Bytes(alloc) }
} else {
MemoryUsage { allocated: Bytes(0), resident: Bytes(0) }
MemoryUsage { allocated: Bytes(0) }
}
}
}
}
impl fmt::Display for MemoryUsage {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{} allocated {} resident", self.allocated, self.resident,)
}
}
#[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
}
}
@ -42,10 +49,10 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
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";
}
@ -56,7 +63,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl std::ops::AddAssign<usize> for Bytes {
fn add_assign(&mut self, x: usize) {
self.0 += x;
self.0 += x as isize;
}
}

View File

@ -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);