Skip to content

Commit afab67e

Browse files
committed
Allow negative bytes
Gotta be optimistic about those memory usage optimizations
1 parent dae99b6 commit afab67e

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

crates/ra_prof/src/memory_usage.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,30 @@ use std::fmt;
33

44
use cfg_if::cfg_if;
55

6+
#[derive(Copy, Clone)]
67
pub struct MemoryUsage {
78
pub allocated: Bytes,
89
}
910

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+
}
22+
}
23+
1024
impl MemoryUsage {
1125
pub fn current() -> MemoryUsage {
1226
cfg_if! {
1327
if #[cfg(target_os = "linux")] {
1428
// Note: This is incredibly slow.
15-
let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as usize;
29+
let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as isize;
1630
MemoryUsage { allocated: Bytes(alloc) }
1731
} else {
1832
MemoryUsage { allocated: Bytes(0) }
@@ -21,17 +35,11 @@ impl MemoryUsage {
2135
}
2236
}
2337

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

3341
impl Bytes {
34-
pub fn megabytes(self) -> usize {
42+
pub fn megabytes(self) -> isize {
3543
self.0 / 1024 / 1024
3644
}
3745
}
@@ -41,10 +49,10 @@ impl fmt::Display for Bytes {
4149
let bytes = self.0;
4250
let mut value = bytes;
4351
let mut suffix = "b";
44-
if value > 4096 {
52+
if value.abs() > 4096 {
4553
value /= 1024;
4654
suffix = "kb";
47-
if value > 4096 {
55+
if value.abs() > 4096 {
4856
value /= 1024;
4957
suffix = "mb";
5058
}
@@ -55,7 +63,7 @@ impl fmt::Display for Bytes {
5563

5664
impl std::ops::AddAssign<usize> for Bytes {
5765
fn add_assign(&mut self, x: usize) {
58-
self.0 += x;
66+
self.0 += x as isize;
5967
}
6068
}
6169

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)