Skip to content

Commit 4024334

Browse files
bors[bot]matklad
andauthored
Merge #5579
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>
2 parents ad43d6b + afab67e commit 4024334

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

crates/ra_prof/src/memory_usage.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,43 @@ use std::fmt;
33

44
use cfg_if::cfg_if;
55

6+
#[derive(Copy, Clone)]
67
pub struct MemoryUsage {
78
pub allocated: Bytes,
8-
pub resident: Bytes,
9+
}
10+
11+
impl fmt::Display for MemoryUsage {
12+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
13+
write!(fmt, "{}", self.allocated)
14+
}
15+
}
16+
17+
impl std::ops::Sub for MemoryUsage {
18+
type Output = MemoryUsage;
19+
fn sub(self, rhs: MemoryUsage) -> MemoryUsage {
20+
MemoryUsage { allocated: self.allocated - rhs.allocated }
21+
}
922
}
1023

1124
impl MemoryUsage {
1225
pub fn current() -> MemoryUsage {
1326
cfg_if! {
1427
if #[cfg(target_os = "linux")] {
1528
// Note: This is incredibly slow.
16-
let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as usize;
17-
MemoryUsage { allocated: Bytes(alloc), resident: Bytes(0) }
29+
let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as isize;
30+
MemoryUsage { allocated: Bytes(alloc) }
1831
} else {
19-
MemoryUsage { allocated: Bytes(0), resident: Bytes(0) }
32+
MemoryUsage { allocated: Bytes(0) }
2033
}
2134
}
2235
}
2336
}
2437

25-
impl fmt::Display for MemoryUsage {
26-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
27-
write!(fmt, "{} allocated {} resident", self.allocated, self.resident,)
28-
}
29-
}
30-
3138
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
32-
pub struct Bytes(usize);
39+
pub struct Bytes(isize);
3340

3441
impl Bytes {
35-
pub fn megabytes(self) -> usize {
42+
pub fn megabytes(self) -> isize {
3643
self.0 / 1024 / 1024
3744
}
3845
}
@@ -42,10 +49,10 @@ impl fmt::Display for Bytes {
4249
let bytes = self.0;
4350
let mut value = bytes;
4451
let mut suffix = "b";
45-
if value > 4096 {
52+
if value.abs() > 4096 {
4653
value /= 1024;
4754
suffix = "kb";
48-
if value > 4096 {
55+
if value.abs() > 4096 {
4956
value /= 1024;
5057
suffix = "mb";
5158
}
@@ -56,7 +63,7 @@ impl fmt::Display for Bytes {
5663

5764
impl std::ops::AddAssign<usize> for Bytes {
5865
fn add_assign(&mut self, x: usize) {
59-
self.0 += x;
66+
self.0 += x as isize;
6067
}
6168
}
6269

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ pub fn analysis_stats(
111111
eprintln!("Total declarations: {}", num_decls);
112112
eprintln!("Total functions: {}", funcs.len());
113113
let item_collection_memory = ra_prof::memory_usage();
114-
eprintln!(
115-
"Item Collection: {:?}, {}",
116-
analysis_time.elapsed(),
117-
item_collection_memory.allocated
118-
);
114+
eprintln!("Item Collection: {:?}, {}", analysis_time.elapsed(), item_collection_memory);
119115

120116
if randomize {
121117
shuffle(&mut rng, &mut funcs);
@@ -140,7 +136,7 @@ pub fn analysis_stats(
140136
eprintln!(
141137
"Parallel Inference: {:?}, {}",
142138
inference_time.elapsed(),
143-
ra_prof::memory_usage().allocated
139+
ra_prof::memory_usage()
144140
);
145141
}
146142

@@ -297,11 +293,7 @@ pub fn analysis_stats(
297293

298294
let inference_time = inference_time.elapsed();
299295
let total_memory = ra_prof::memory_usage();
300-
eprintln!(
301-
"Inference: {:?}, {}",
302-
inference_time,
303-
total_memory.allocated - item_collection_memory.allocated
304-
);
296+
eprintln!("Inference: {:?}, {}", inference_time, total_memory - item_collection_memory);
305297

306298
let analysis_time = analysis_time.elapsed();
307299
eprintln!("Total: {:?}, {}", analysis_time, total_memory);

0 commit comments

Comments
 (0)