Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions parity-util-mem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ hashbrown = { version = "0.6", optional = true }
mimallocator = { version = "0.1.3", features = ["secure"], optional = true }
mimalloc-sys = { version = "0.1.6", optional = true }
parity-util-mem-derive = { path = "derive", version = "0.1" }
impl-trait-for-tuples = "0.1.3"

smallvec = { version = "1.0.0", optional = true }
ethereum-types = { version = "0.8.0", optional = true, path = "../ethereum-types" }
Expand Down
40 changes: 5 additions & 35 deletions parity-util-mem/src/malloc_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,42 +261,12 @@ impl<T: MallocSizeOf + ?Sized> MallocSizeOf for Box<T> {
}
}

impl MallocSizeOf for () {
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
0
}
}

impl<T1, T2> MallocSizeOf for (T1, T2)
where
T1: MallocSizeOf,
T2: MallocSizeOf,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.0.size_of(ops) + self.1.size_of(ops)
}
}

impl<T1, T2, T3> MallocSizeOf for (T1, T2, T3)
where
T1: MallocSizeOf,
T2: MallocSizeOf,
T3: MallocSizeOf,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.0.size_of(ops) + self.1.size_of(ops) + self.2.size_of(ops)
}
}

impl<T1, T2, T3, T4> MallocSizeOf for (T1, T2, T3, T4)
where
T1: MallocSizeOf,
T2: MallocSizeOf,
T3: MallocSizeOf,
T4: MallocSizeOf,
{
#[impl_trait_for_tuples::impl_for_tuples(12)]
impl MallocSizeOf for Tuple {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.0.size_of(ops) + self.1.size_of(ops) + self.2.size_of(ops) + self.3.size_of(ops)
let mut result = 0;
for_tuples!( #( result += Tuple.size_of(ops); )* );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The full function can be written like:

for_tuples!( #( Tuple.size_of(ops) )+* )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is first what I tried - does not work, probably because of () variant is also expanded, producing () return value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yeah. Do we need the () case? If not impl_for_tuple(1, 12)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do need it

result
}
}

Expand Down
14 changes: 14 additions & 0 deletions parity-util-mem/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,17 @@ fn derive_morecomplex() {

assert!(t.malloc_size_of() > 8000);
}

#[test]
fn derive_tuple() {
#[derive(MallocSizeOf)]
struct Trivia {
tp1: (),
tp2: (Vec<u8>, Vec<u8>),
}

let t = Trivia { tp1: (), tp2: (vec![7u8; 1024], vec![9u8; 1024]) };

assert!(t.malloc_size_of() > 2000);
assert!(t.malloc_size_of() < 3000);
}