diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-07-30 08:58:06 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-07-30 08:58:06 +0100 |
commit | 402433470a5f7cdb16083919c76f5d6f6f90ce3d (patch) | |
tree | f21490b1f28db69d5d8132c245cf1df7ee1e48bb /crates/ra_prof | |
parent | ad43d6bde67fd649347c3e2ac46bb8a75e71a2ab (diff) | |
parent | afab67e69c39027fb99878751309d4050324beef (diff) |
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 <[email protected]>
Diffstat (limited to 'crates/ra_prof')
-rw-r--r-- | crates/ra_prof/src/memory_usage.rs | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/crates/ra_prof/src/memory_usage.rs b/crates/ra_prof/src/memory_usage.rs index 745345fac..22b61e4a2 100644 --- a/crates/ra_prof/src/memory_usage.rs +++ b/crates/ra_prof/src/memory_usage.rs | |||
@@ -3,9 +3,22 @@ use std::fmt; | |||
3 | 3 | ||
4 | use cfg_if::cfg_if; | 4 | use cfg_if::cfg_if; |
5 | 5 | ||
6 | #[derive(Copy, Clone)] | ||
6 | pub struct MemoryUsage { | 7 | pub struct MemoryUsage { |
7 | pub allocated: Bytes, | 8 | 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 | } | ||
9 | } | 22 | } |
10 | 23 | ||
11 | impl MemoryUsage { | 24 | impl MemoryUsage { |
@@ -13,26 +26,20 @@ impl MemoryUsage { | |||
13 | cfg_if! { | 26 | cfg_if! { |
14 | if #[cfg(target_os = "linux")] { | 27 | if #[cfg(target_os = "linux")] { |
15 | // Note: This is incredibly slow. | 28 | // Note: This is incredibly slow. |
16 | let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as usize; | 29 | let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as isize; |
17 | MemoryUsage { allocated: Bytes(alloc), resident: Bytes(0) } | 30 | MemoryUsage { allocated: Bytes(alloc) } |
18 | } else { | 31 | } else { |
19 | MemoryUsage { allocated: Bytes(0), resident: Bytes(0) } | 32 | MemoryUsage { allocated: Bytes(0) } |
20 | } | 33 | } |
21 | } | 34 | } |
22 | } | 35 | } |
23 | } | 36 | } |
24 | 37 | ||
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 | |||
31 | #[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] | 38 | #[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] |
32 | pub struct Bytes(usize); | 39 | pub struct Bytes(isize); |
33 | 40 | ||
34 | impl Bytes { | 41 | impl Bytes { |
35 | pub fn megabytes(self) -> usize { | 42 | pub fn megabytes(self) -> isize { |
36 | self.0 / 1024 / 1024 | 43 | self.0 / 1024 / 1024 |
37 | } | 44 | } |
38 | } | 45 | } |
@@ -42,10 +49,10 @@ impl fmt::Display for Bytes { | |||
42 | let bytes = self.0; | 49 | let bytes = self.0; |
43 | let mut value = bytes; | 50 | let mut value = bytes; |
44 | let mut suffix = "b"; | 51 | let mut suffix = "b"; |
45 | if value > 4096 { | 52 | if value.abs() > 4096 { |
46 | value /= 1024; | 53 | value /= 1024; |
47 | suffix = "kb"; | 54 | suffix = "kb"; |
48 | if value > 4096 { | 55 | if value.abs() > 4096 { |
49 | value /= 1024; | 56 | value /= 1024; |
50 | suffix = "mb"; | 57 | suffix = "mb"; |
51 | } | 58 | } |
@@ -56,7 +63,7 @@ impl fmt::Display for Bytes { | |||
56 | 63 | ||
57 | impl std::ops::AddAssign<usize> for Bytes { | 64 | impl std::ops::AddAssign<usize> for Bytes { |
58 | fn add_assign(&mut self, x: usize) { | 65 | fn add_assign(&mut self, x: usize) { |
59 | self.0 += x; | 66 | self.0 += x as isize; |
60 | } | 67 | } |
61 | } | 68 | } |
62 | 69 | ||