From 2854f7873776f505f4d2d2c93c42cb5a46b03353 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 3 Feb 2020 12:39:13 +0300 Subject: [PATCH 1/4] split off primitives --- parity-util-mem/Cargo.toml | 7 ++--- parity-util-mem/src/ethereum_impls.rs | 4 +-- parity-util-mem/src/lib.rs | 3 +++ parity-util-mem/src/primitives_impls.rs | 34 +++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 parity-util-mem/src/primitives_impls.rs diff --git a/parity-util-mem/Cargo.toml b/parity-util-mem/Cargo.toml index baf11981e..f38ea6ac5 100644 --- a/parity-util-mem/Cargo.toml +++ b/parity-util-mem/Cargo.toml @@ -26,6 +26,7 @@ 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" } parking_lot = { version = "0.9.0", optional = true } +primitive-types = { version = "0.6", path = "../primitive-types", default-features = false, optional = true } [target.'cfg(target_os = "windows")'.dependencies] winapi = { version = "0.3.8", features = ["heapapi"] } @@ -35,7 +36,7 @@ version = "0.3.2" optional = true [features] -default = ["std", "ethereum-impls", "lru", "hashbrown", "smallvec"] +default = ["std", "ethereum-impls", "lru", "hashbrown", "smallvec", "primitive-types"] std = ["parking_lot"] # use dlmalloc as global allocator dlmalloc-global = ["dlmalloc", "estimate-heapsize"] @@ -46,6 +47,6 @@ jemalloc-global = ["jemallocator"] # use mimalloc as global allocator mimalloc-global = ["mimallocator", "mimalloc-sys"] # implement additional types -ethereum-impls = ["ethereum-types"] +ethereum-impls = ["ethereum-types", "primitive-types"] # Full estimate: no call to allocator -estimate-heapsize = [] +estimate-heapsize = [] \ No newline at end of file diff --git a/parity-util-mem/src/ethereum_impls.rs b/parity-util-mem/src/ethereum_impls.rs index 243230106..4379b3b0e 100644 --- a/parity-util-mem/src/ethereum_impls.rs +++ b/parity-util-mem/src/ethereum_impls.rs @@ -17,6 +17,6 @@ //! Implementation of `MallocSize` for common ethereum types: fixed hashes //! and uints. -use ethereum_types::{Bloom, H128, H160, H256, H264, H32, H512, H520, H64, U128, U256, U512, U64}; +use ethereum_types::{Bloom, H128, H264, H32, H520, H64, U64}; -malloc_size_of_is_0!(U64, U128, U256, U512, H32, H64, H128, H160, H256, H264, H512, H520, Bloom); +malloc_size_of_is_0!(U64, H32, H64, H128, H264, H520, Bloom); diff --git a/parity-util-mem/src/lib.rs b/parity-util-mem/src/lib.rs index ddd8f1fd5..cdea52e42 100644 --- a/parity-util-mem/src/lib.rs +++ b/parity-util-mem/src/lib.rs @@ -68,6 +68,9 @@ mod malloc_size; #[cfg(feature = "ethereum-impls")] pub mod ethereum_impls; +#[cfg(feature = "primitive-types")] +pub mod primitives_impls; + pub use allocators::MallocSizeOfExt; pub use malloc_size::{MallocSizeOf, MallocSizeOfOps}; diff --git a/parity-util-mem/src/primitives_impls.rs b/parity-util-mem/src/primitives_impls.rs new file mode 100644 index 000000000..ab5953dcc --- /dev/null +++ b/parity-util-mem/src/primitives_impls.rs @@ -0,0 +1,34 @@ +// Copyright 2020 Parity Technologies (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! Implementation of `MallocSize` primitive types. + +use primitive_types::{H160, H256, H512, U128, U256, U512}; + +malloc_size_of_is_0!(U128, U256, U512, H160, H256, H512); + +#[cfg(test)] +mod tests { + + use primitive_types::H256; + + #[test] + fn smoky() { + let v = vec![H256::zero(), H256::zero()]; + + assert!(crate::MallocSizeOfExt::malloc_size_of(&v) >= 64); + } +} From fd0e16d5c44fe915d473f4b2af4167b95496aa88 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 3 Feb 2020 20:10:17 +0300 Subject: [PATCH 2/4] add for BTreeSet --- parity-util-mem/src/malloc_size.rs | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/parity-util-mem/src/malloc_size.rs b/parity-util-mem/src/malloc_size.rs index 2f180a676..8151e8204 100644 --- a/parity-util-mem/src/malloc_size.rs +++ b/parity-util-mem/src/malloc_size.rs @@ -455,6 +455,33 @@ where } } + +impl MallocShallowSizeOf for std::collections::BTreeSet +{ + fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { + if ops.has_malloc_enclosing_size_of() { + // See implementation for HashSet how this works. + self.iter().next().map_or(0, |t| unsafe { ops.malloc_enclosing_size_of(t) }) + } else { + // An estimate. + self.len() * (size_of::() + size_of::()) + } + } +} + +impl MallocSizeOf for rstd::collections::BTreeSet +where + T: MallocSizeOf, +{ + fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { + let mut n = self.shallow_size_of(ops); + for k in self.iter() { + n += k.size_of(ops); + } + n + } +} + // PhantomData is always 0. impl MallocSizeOf for rstd::marker::PhantomData { fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { @@ -677,6 +704,7 @@ mod tests { use crate::{allocators::new_malloc_size_ops, MallocSizeOf, MallocSizeOfOps}; use smallvec::SmallVec; use std::mem; + use std::collections::BTreeSet; impl_smallvec!(3); #[test] @@ -727,4 +755,12 @@ mod tests { let expected_min_allocs = mem::size_of::() * 4 + "ÖWL".len() + "COW".len() + "PIG".len() + "DUCK".len(); assert!(v.size_of(&mut ops) >= expected_min_allocs); } + + #[test] + fn btree_set() { + let mut set = BTreeSet::new(); + for t in 0..100 { set.insert(vec![t]); } + // ~36 per value + assert!(crate::malloc_size(&set) > 3000); + } } From 47d8e2c8cf685af88a315b5b78048052d45810fe Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Mon, 3 Feb 2020 23:47:52 -0800 Subject: [PATCH 3/4] Update parity-util-mem/src/malloc_size.rs Co-Authored-By: Andronik Ordian --- parity-util-mem/src/malloc_size.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parity-util-mem/src/malloc_size.rs b/parity-util-mem/src/malloc_size.rs index 8151e8204..5d62c8cee 100644 --- a/parity-util-mem/src/malloc_size.rs +++ b/parity-util-mem/src/malloc_size.rs @@ -456,7 +456,7 @@ where } -impl MallocShallowSizeOf for std::collections::BTreeSet +impl MallocShallowSizeOf for rstd::collections::BTreeSet { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { if ops.has_malloc_enclosing_size_of() { From 5b77a048098eb4b49312215352a6d82b7ea0be1d Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 4 Feb 2020 13:59:43 +0300 Subject: [PATCH 4/4] cargo fmt --- parity-util-mem/src/malloc_size.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/parity-util-mem/src/malloc_size.rs b/parity-util-mem/src/malloc_size.rs index 5d62c8cee..864c94abd 100644 --- a/parity-util-mem/src/malloc_size.rs +++ b/parity-util-mem/src/malloc_size.rs @@ -455,9 +455,7 @@ where } } - -impl MallocShallowSizeOf for rstd::collections::BTreeSet -{ +impl MallocShallowSizeOf for rstd::collections::BTreeSet { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { if ops.has_malloc_enclosing_size_of() { // See implementation for HashSet how this works. @@ -703,8 +701,8 @@ malloc_size_of_is_0!(std::time::Duration); mod tests { use crate::{allocators::new_malloc_size_ops, MallocSizeOf, MallocSizeOfOps}; use smallvec::SmallVec; - use std::mem; use std::collections::BTreeSet; + use std::mem; impl_smallvec!(3); #[test] @@ -759,7 +757,9 @@ mod tests { #[test] fn btree_set() { let mut set = BTreeSet::new(); - for t in 0..100 { set.insert(vec![t]); } + for t in 0..100 { + set.insert(vec![t]); + } // ~36 per value assert!(crate::malloc_size(&set) > 3000); }