From a727abc994129b02ff57e6a31a1a870da3d69fd7 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sat, 21 Dec 2019 19:18:20 +0300 Subject: [PATCH 01/12] add derive and remove bounds --- Cargo.toml | 1 + parity-util-mem/Cargo.toml | 1 + parity-util-mem/derive/Cargo.toml | 16 +++++++ parity-util-mem/derive/lib.rs | 76 ++++++++++++++++++++++++++++++ parity-util-mem/src/lib.rs | 5 +- parity-util-mem/src/malloc_size.rs | 12 ++--- 6 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 parity-util-mem/derive/Cargo.toml create mode 100644 parity-util-mem/derive/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 6c12c3205..c2511404c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,4 +20,5 @@ members = [ "primitive-types", "ethereum-types", "ethbloom", + "parity-util-mem/derive" ] diff --git a/parity-util-mem/Cargo.toml b/parity-util-mem/Cargo.toml index a89122723..2109d59fa 100644 --- a/parity-util-mem/Cargo.toml +++ b/parity-util-mem/Cargo.toml @@ -19,6 +19,7 @@ wee_alloc = { version = "0.4.5", optional = true } # The performance penalty is only around 3% on average over our benchmarks. 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" } smallvec = { version = "1.0.0", optional = true } ethereum-types = { version = "0.8.0", optional = true, path = "../ethereum-types" } diff --git a/parity-util-mem/derive/Cargo.toml b/parity-util-mem/derive/Cargo.toml new file mode 100644 index 000000000..8c12e930e --- /dev/null +++ b/parity-util-mem/derive/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "parity-util-mem-derive" +version = "0.1.0" +authors = ["Parity Technologies"] +license = "GPL-3.0" +description = "Crate for memory reporting" +repository = "https://github.com/paritytech/pariry-common/parity-util-mem/derive" + +[lib] +path = "lib.rs" +proc-macro = true + +[dependencies] +proc-macro2 = "1" +syn = { version = "1", features = ["full"] } +synstructure = "0.12" diff --git a/parity-util-mem/derive/lib.rs b/parity-util-mem/derive/lib.rs new file mode 100644 index 000000000..76231dc44 --- /dev/null +++ b/parity-util-mem/derive/lib.rs @@ -0,0 +1,76 @@ + +//! A crate for deriving the MallocSizeOf trait. + +extern crate proc_macro2; +#[macro_use] +extern crate syn; +#[macro_use] +extern crate synstructure; + +#[cfg(not(test))] +decl_derive!([MallocSizeOf, attributes(ignore_malloc_size_of)] => malloc_size_of_derive); + +fn malloc_size_of_derive(s: synstructure::Structure) -> proc_macro2::TokenStream { + let match_body = s.each(|binding| { + let ignore = binding + .ast() + .attrs + .iter() + .any(|attr| match attr.parse_meta().unwrap() { + syn::Meta::Path(ref path) | syn::Meta::List(syn::MetaList { ref path, .. }) + if path.is_ident("ignore_malloc_size_of") => + { + panic!( + "#[ignore_malloc_size_of] should have an explanation, \ + e.g. #[ignore_malloc_size_of = \"because reasons\"]" + ); + } + syn::Meta::NameValue(syn::MetaNameValue { ref path, .. }) + if path.is_ident("ignore_malloc_size_of") => + { + true + }, + _ => false, + }); + if ignore { + None + } else if let syn::Type::Array(..) = binding.ast().ty { + Some(quote! { + for item in #binding.iter() { + sum += parity_util_mem::MallocSizeOf::size_of(item, ops); + } + }) + } else { + Some(quote! { + sum += parity_util_mem::MallocSizeOf::size_of(#binding, ops); + }) + } + }); + + let ast = s.ast(); + let name = &ast.ident; + let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); + let mut where_clause = where_clause.unwrap_or(&parse_quote!(where)).clone(); + for param in ast.generics.type_params() { + let ident = ¶m.ident; + where_clause + .predicates + .push(parse_quote!(#ident: parity_util_mem::MallocSizeOf)); + } + + let tokens = quote! { + impl #impl_generics parity_util_mem::MallocSizeOf for #name #ty_generics #where_clause { + #[inline] + #[allow(unused_variables, unused_mut, unreachable_code)] + fn size_of(&self, ops: &mut parity_util_mem::MallocSizeOfOps) -> usize { + let mut sum = 0; + match *self { + #match_body + } + sum + } + } + }; + + tokens +} diff --git a/parity-util-mem/src/lib.rs b/parity-util-mem/src/lib.rs index 43fbc4ab6..23c07e27b 100644 --- a/parity-util-mem/src/lib.rs +++ b/parity-util-mem/src/lib.rs @@ -23,8 +23,6 @@ #[cfg(not(feature = "std"))] extern crate alloc; -use malloc_size_of_derive as malloc_size_derive; - cfg_if::cfg_if! { if #[cfg(all( feature = "jemalloc-global", @@ -72,7 +70,8 @@ pub mod impls; pub use allocators::MallocSizeOfExt; pub use malloc_size::{MallocSizeOf, MallocSizeOfOps}; -pub use malloc_size_derive::*; + +pub use parity_util_mem_derive::*; #[cfg(feature = "std")] #[cfg(test)] diff --git a/parity-util-mem/src/malloc_size.rs b/parity-util-mem/src/malloc_size.rs index a748eec82..b86538e6c 100644 --- a/parity-util-mem/src/malloc_size.rs +++ b/parity-util-mem/src/malloc_size.rs @@ -427,9 +427,6 @@ where #[cfg(feature = "std")] impl MallocShallowSizeOf for std::collections::HashMap -where - K: Eq + Hash, - S: BuildHasher, { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { // See the implementation for std::collections::HashSet for details. @@ -444,9 +441,8 @@ where #[cfg(feature = "std")] impl MallocSizeOf for std::collections::HashMap where - K: Eq + Hash + MallocSizeOf, - V: MallocSizeOf, - S: BuildHasher, + K: MallocSizeOf, + V: MallocSizeOf { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { let mut n = self.shallow_size_of(ops); @@ -459,8 +455,6 @@ where } impl MallocShallowSizeOf for rstd::collections::BTreeMap -where - K: Eq + Hash, { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { if ops.has_malloc_enclosing_size_of() { @@ -473,7 +467,7 @@ where impl MallocSizeOf for rstd::collections::BTreeMap where - K: Eq + Hash + MallocSizeOf, + K: MallocSizeOf, V: MallocSizeOf, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { From f453528a2c0a454260d9b5f73d3f91f8148f8c97 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sat, 21 Dec 2019 19:30:58 +0300 Subject: [PATCH 02/12] add smoky test --- parity-util-mem/tests/derive.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 parity-util-mem/tests/derive.rs diff --git a/parity-util-mem/tests/derive.rs b/parity-util-mem/tests/derive.rs new file mode 100644 index 000000000..bd862370a --- /dev/null +++ b/parity-util-mem/tests/derive.rs @@ -0,0 +1,15 @@ + +#[test] +fn derive_smoky() { + + use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; + + #[derive(MallocSizeOf)] + struct Trivia { + v: Vec, + } + + let t = Trivia { v: vec![0u8; 1024] }; + + assert_eq!(t.malloc_size_of(), 1024); +} From baf450a0b32add79630927b554fbaa46953187fa Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sat, 21 Dec 2019 19:33:50 +0300 Subject: [PATCH 03/12] add another test --- parity-util-mem/tests/derive.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/parity-util-mem/tests/derive.rs b/parity-util-mem/tests/derive.rs index bd862370a..f2b530ab1 100644 --- a/parity-util-mem/tests/derive.rs +++ b/parity-util-mem/tests/derive.rs @@ -13,3 +13,20 @@ fn derive_smoky() { assert_eq!(t.malloc_size_of(), 1024); } + +#[test] +fn derive_hashmap() { + + use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; + + #[derive(MallocSizeOf, Default)] + struct Trivia { + hm: std::collections::HashMap>, + } + + let mut t = Trivia::default(); + + t.hm.insert(1, vec![0u8; 2048]); + + assert_eq!(t.malloc_size_of(), 2088); +} From 61d84e99a7197540704b44fe9482779dab3c18b3 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sun, 22 Dec 2019 11:20:11 +0300 Subject: [PATCH 04/12] add also for fixed size arrays --- parity-util-mem/src/impls.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/parity-util-mem/src/impls.rs b/parity-util-mem/src/impls.rs index 322375873..011f1f187 100644 --- a/parity-util-mem/src/impls.rs +++ b/parity-util-mem/src/impls.rs @@ -32,6 +32,42 @@ malloc_size_of_is_0!(std::time::Duration); malloc_size_of_is_0!(U64, U128, U256, U512, H32, H64, H128, H160, H256, H264, H512, H520, Bloom); +malloc_size_of_is_0!( + [u8; 1], + [u8; 2], + [u8; 3], + [u8; 4], + [u8; 5], + [u8; 6], + [u8; 7], + [u8; 8], + [u8; 9], + [u8; 10], + [u8; 11], + [u8; 12], + [u8; 13], + [u8; 14], + [u8; 15], + [u8; 16], + [u8; 17], + [u8; 18], + [u8; 19], + [u8; 20], + [u8; 21], + [u8; 22], + [u8; 23], + [u8; 24], + [u8; 25], + [u8; 26], + [u8; 27], + [u8; 28], + [u8; 29], + [u8; 30], + [u8; 31], + [u8; 32] +); + + macro_rules! impl_smallvec { ($size: expr) => { impl MallocSizeOf for SmallVec<[T; $size]> From e5da39847d022fe3266471ac7ca2fc77191807b6 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 23 Dec 2019 22:11:50 +0300 Subject: [PATCH 05/12] cargo fmt --- parity-util-mem/derive/lib.rs | 109 +++++++++++++---------------- parity-util-mem/src/impls.rs | 36 +--------- parity-util-mem/src/malloc_size.rs | 8 +-- parity-util-mem/tests/derive.rs | 3 - 4 files changed, 55 insertions(+), 101 deletions(-) diff --git a/parity-util-mem/derive/lib.rs b/parity-util-mem/derive/lib.rs index 76231dc44..b4f0759fd 100644 --- a/parity-util-mem/derive/lib.rs +++ b/parity-util-mem/derive/lib.rs @@ -1,4 +1,3 @@ - //! A crate for deriving the MallocSizeOf trait. extern crate proc_macro2; @@ -11,66 +10,56 @@ extern crate synstructure; decl_derive!([MallocSizeOf, attributes(ignore_malloc_size_of)] => malloc_size_of_derive); fn malloc_size_of_derive(s: synstructure::Structure) -> proc_macro2::TokenStream { - let match_body = s.each(|binding| { - let ignore = binding - .ast() - .attrs - .iter() - .any(|attr| match attr.parse_meta().unwrap() { - syn::Meta::Path(ref path) | syn::Meta::List(syn::MetaList { ref path, .. }) - if path.is_ident("ignore_malloc_size_of") => - { - panic!( - "#[ignore_malloc_size_of] should have an explanation, \ - e.g. #[ignore_malloc_size_of = \"because reasons\"]" - ); - } - syn::Meta::NameValue(syn::MetaNameValue { ref path, .. }) - if path.is_ident("ignore_malloc_size_of") => - { - true - }, - _ => false, - }); - if ignore { - None - } else if let syn::Type::Array(..) = binding.ast().ty { - Some(quote! { - for item in #binding.iter() { - sum += parity_util_mem::MallocSizeOf::size_of(item, ops); - } - }) - } else { - Some(quote! { - sum += parity_util_mem::MallocSizeOf::size_of(#binding, ops); - }) - } - }); + let match_body = s.each(|binding| { + let ignore = binding.ast().attrs.iter().any(|attr| match attr.parse_meta().unwrap() { + syn::Meta::Path(ref path) | syn::Meta::List(syn::MetaList { ref path, .. }) + if path.is_ident("ignore_malloc_size_of") => + { + panic!( + "#[ignore_malloc_size_of] should have an explanation, \ + e.g. #[ignore_malloc_size_of = \"because reasons\"]" + ); + } + syn::Meta::NameValue(syn::MetaNameValue { ref path, .. }) if path.is_ident("ignore_malloc_size_of") => true, + _ => false, + }); + if ignore { + None + } else if let syn::Type::Array(..) = binding.ast().ty { + Some(quote! { + for item in #binding.iter() { + sum += parity_util_mem::MallocSizeOf::size_of(item, ops); + } + }) + } else { + Some(quote! { + sum += parity_util_mem::MallocSizeOf::size_of(#binding, ops); + }) + } + }); - let ast = s.ast(); - let name = &ast.ident; - let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); - let mut where_clause = where_clause.unwrap_or(&parse_quote!(where)).clone(); - for param in ast.generics.type_params() { - let ident = ¶m.ident; - where_clause - .predicates - .push(parse_quote!(#ident: parity_util_mem::MallocSizeOf)); - } + let ast = s.ast(); + let name = &ast.ident; + let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); + let mut where_clause = where_clause.unwrap_or(&parse_quote!(where)).clone(); + for param in ast.generics.type_params() { + let ident = ¶m.ident; + where_clause.predicates.push(parse_quote!(#ident: parity_util_mem::MallocSizeOf)); + } - let tokens = quote! { - impl #impl_generics parity_util_mem::MallocSizeOf for #name #ty_generics #where_clause { - #[inline] - #[allow(unused_variables, unused_mut, unreachable_code)] - fn size_of(&self, ops: &mut parity_util_mem::MallocSizeOfOps) -> usize { - let mut sum = 0; - match *self { - #match_body - } - sum - } - } - }; + let tokens = quote! { + impl #impl_generics parity_util_mem::MallocSizeOf for #name #ty_generics #where_clause { + #[inline] + #[allow(unused_variables, unused_mut, unreachable_code)] + fn size_of(&self, ops: &mut parity_util_mem::MallocSizeOfOps) -> usize { + let mut sum = 0; + match *self { + #match_body + } + sum + } + } + }; - tokens + tokens } diff --git a/parity-util-mem/src/impls.rs b/parity-util-mem/src/impls.rs index 011f1f187..4124b132a 100644 --- a/parity-util-mem/src/impls.rs +++ b/parity-util-mem/src/impls.rs @@ -33,41 +33,11 @@ malloc_size_of_is_0!(std::time::Duration); malloc_size_of_is_0!(U64, U128, U256, U512, H32, H64, H128, H160, H256, H264, H512, H520, Bloom); malloc_size_of_is_0!( - [u8; 1], - [u8; 2], - [u8; 3], - [u8; 4], - [u8; 5], - [u8; 6], - [u8; 7], - [u8; 8], - [u8; 9], - [u8; 10], - [u8; 11], - [u8; 12], - [u8; 13], - [u8; 14], - [u8; 15], - [u8; 16], - [u8; 17], - [u8; 18], - [u8; 19], - [u8; 20], - [u8; 21], - [u8; 22], - [u8; 23], - [u8; 24], - [u8; 25], - [u8; 26], - [u8; 27], - [u8; 28], - [u8; 29], - [u8; 30], - [u8; 31], - [u8; 32] + [u8; 1], [u8; 2], [u8; 3], [u8; 4], [u8; 5], [u8; 6], [u8; 7], [u8; 8], [u8; 9], [u8; 10], [u8; 11], [u8; 12], + [u8; 13], [u8; 14], [u8; 15], [u8; 16], [u8; 17], [u8; 18], [u8; 19], [u8; 20], [u8; 21], [u8; 22], [u8; 23], + [u8; 24], [u8; 25], [u8; 26], [u8; 27], [u8; 28], [u8; 29], [u8; 30], [u8; 31], [u8; 32] ); - macro_rules! impl_smallvec { ($size: expr) => { impl MallocSizeOf for SmallVec<[T; $size]> diff --git a/parity-util-mem/src/malloc_size.rs b/parity-util-mem/src/malloc_size.rs index b86538e6c..5a6bd715f 100644 --- a/parity-util-mem/src/malloc_size.rs +++ b/parity-util-mem/src/malloc_size.rs @@ -426,8 +426,7 @@ where } #[cfg(feature = "std")] -impl MallocShallowSizeOf for std::collections::HashMap -{ +impl MallocShallowSizeOf for std::collections::HashMap { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { // See the implementation for std::collections::HashSet for details. if ops.has_malloc_enclosing_size_of() { @@ -442,7 +441,7 @@ impl MallocShallowSizeOf for std::collections::HashMap impl MallocSizeOf for std::collections::HashMap where K: MallocSizeOf, - V: MallocSizeOf + V: MallocSizeOf, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { let mut n = self.shallow_size_of(ops); @@ -454,8 +453,7 @@ where } } -impl MallocShallowSizeOf for rstd::collections::BTreeMap -{ +impl MallocShallowSizeOf for rstd::collections::BTreeMap { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { if ops.has_malloc_enclosing_size_of() { self.values().next().map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) }) diff --git a/parity-util-mem/tests/derive.rs b/parity-util-mem/tests/derive.rs index f2b530ab1..80ad96ed7 100644 --- a/parity-util-mem/tests/derive.rs +++ b/parity-util-mem/tests/derive.rs @@ -1,7 +1,5 @@ - #[test] fn derive_smoky() { - use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; #[derive(MallocSizeOf)] @@ -16,7 +14,6 @@ fn derive_smoky() { #[test] fn derive_hashmap() { - use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; #[derive(MallocSizeOf, Default)] From 17016e0c2e66dcb3f1de9bcfa640b202327575ad Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 23 Dec 2019 22:46:29 +0300 Subject: [PATCH 06/12] fix tests for different archs --- parity-util-mem/tests/derive.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/parity-util-mem/tests/derive.rs b/parity-util-mem/tests/derive.rs index 80ad96ed7..6f922c260 100644 --- a/parity-util-mem/tests/derive.rs +++ b/parity-util-mem/tests/derive.rs @@ -1,4 +1,5 @@ #[test] +#[cfg(feature="std")] fn derive_smoky() { use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; @@ -9,10 +10,11 @@ fn derive_smoky() { let t = Trivia { v: vec![0u8; 1024] }; - assert_eq!(t.malloc_size_of(), 1024); + assert!(t.malloc_size_of() > 1000); } #[test] +#[cfg(feature="std")] fn derive_hashmap() { use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; @@ -25,5 +27,5 @@ fn derive_hashmap() { t.hm.insert(1, vec![0u8; 2048]); - assert_eq!(t.malloc_size_of(), 2088); + assert!(t.malloc_size_of() > 2000); } From 5eeac76445d40c05c2dc0ded9287a3ecca14174b Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 24 Dec 2019 19:00:58 +0300 Subject: [PATCH 07/12] cargo fmt --- parity-util-mem/tests/derive.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/parity-util-mem/tests/derive.rs b/parity-util-mem/tests/derive.rs index 6f922c260..597b4f73c 100644 --- a/parity-util-mem/tests/derive.rs +++ b/parity-util-mem/tests/derive.rs @@ -1,5 +1,5 @@ #[test] -#[cfg(feature="std")] +#[cfg(feature = "std")] fn derive_smoky() { use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; @@ -14,7 +14,7 @@ fn derive_smoky() { } #[test] -#[cfg(feature="std")] +#[cfg(feature = "std")] fn derive_hashmap() { use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; From 75863f9cff6154663c28ffce5b29bbbe21536ff8 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 30 Dec 2019 19:55:41 +0300 Subject: [PATCH 08/12] address review --- parity-util-mem/Cargo.toml | 1 - parity-util-mem/derive/Cargo.toml | 2 +- parity-util-mem/tests/derive.rs | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/parity-util-mem/Cargo.toml b/parity-util-mem/Cargo.toml index 2109d59fa..10a90c3cc 100644 --- a/parity-util-mem/Cargo.toml +++ b/parity-util-mem/Cargo.toml @@ -9,7 +9,6 @@ edition = "2018" [dependencies] cfg-if = "0.1.10" -malloc_size_of_derive = "0.1.1" dlmalloc = { version = "0.1.3", features = ["global"], optional = true } wee_alloc = { version = "0.4.5", optional = true } # from https://github.com/microsoft/mimalloc: diff --git a/parity-util-mem/derive/Cargo.toml b/parity-util-mem/derive/Cargo.toml index 8c12e930e..f37d38013 100644 --- a/parity-util-mem/derive/Cargo.toml +++ b/parity-util-mem/derive/Cargo.toml @@ -2,7 +2,7 @@ name = "parity-util-mem-derive" version = "0.1.0" authors = ["Parity Technologies"] -license = "GPL-3.0" +license = "MIT" description = "Crate for memory reporting" repository = "https://github.com/paritytech/pariry-common/parity-util-mem/derive" diff --git a/parity-util-mem/tests/derive.rs b/parity-util-mem/tests/derive.rs index 597b4f73c..7cc3a1f12 100644 --- a/parity-util-mem/tests/derive.rs +++ b/parity-util-mem/tests/derive.rs @@ -1,6 +1,6 @@ #[test] #[cfg(feature = "std")] -fn derive_smoky() { +fn derive_vec() { use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; #[derive(MallocSizeOf)] From 1d894fee2d3981fdc0fbe88da63209a62b6f4e24 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 30 Dec 2019 21:51:03 +0300 Subject: [PATCH 09/12] add ignore test --- parity-util-mem/tests/derive.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/parity-util-mem/tests/derive.rs b/parity-util-mem/tests/derive.rs index 7cc3a1f12..b525ae42f 100644 --- a/parity-util-mem/tests/derive.rs +++ b/parity-util-mem/tests/derive.rs @@ -1,8 +1,8 @@ +use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; + #[test] #[cfg(feature = "std")] fn derive_vec() { - use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; - #[derive(MallocSizeOf)] struct Trivia { v: Vec, @@ -16,8 +16,6 @@ fn derive_vec() { #[test] #[cfg(feature = "std")] fn derive_hashmap() { - use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; - #[derive(MallocSizeOf, Default)] struct Trivia { hm: std::collections::HashMap>, @@ -29,3 +27,21 @@ fn derive_hashmap() { assert!(t.malloc_size_of() > 2000); } + +#[test] +#[cfg(feature = "std")] +fn derive_ignore() { + #[derive(MallocSizeOf, Default)] + struct Trivia { + hm: std::collections::HashMap>, + #[ignore_malloc_size_of = "I don't like vectors"] + v: Vec, + } + + let mut t = Trivia::default(); + + t.hm.insert(1, vec![0u8; 2048]); + t.v = vec![0u8; 1024]; + + assert!(t.malloc_size_of() < 3000); +} From 698dd77a4064c0049a14e66c0d3f630690366bef Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Mon, 30 Dec 2019 21:55:46 +0300 Subject: [PATCH 10/12] Update parity-util-mem/src/malloc_size.rs Co-Authored-By: David --- 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 5a6bd715f..b9e966a30 100644 --- a/parity-util-mem/src/malloc_size.rs +++ b/parity-util-mem/src/malloc_size.rs @@ -43,7 +43,7 @@ //! measured as well as the thing it points to. E.g. //! ` as MallocSizeOf>::size_of(field, ops)`. -//! This is an extended (for own internal needs) version of the Servo internal malloc_size crate. +//! This is an extended version of the Servo internal malloc_size crate. //! We should occasionally track the upstream changes/fixes and reintroduce them here, be they applicable. #[cfg(not(feature = "std"))] From d160bbfd06fa0850040f6a62e056347b5c03dd00 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Mon, 30 Dec 2019 21:56:44 +0300 Subject: [PATCH 11/12] Update parity-util-mem/src/malloc_size.rs Co-Authored-By: David --- 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 b9e966a30..a86823db5 100644 --- a/parity-util-mem/src/malloc_size.rs +++ b/parity-util-mem/src/malloc_size.rs @@ -44,7 +44,7 @@ //! ` as MallocSizeOf>::size_of(field, ops)`. //! This is an extended version of the Servo internal malloc_size crate. -//! We should occasionally track the upstream changes/fixes and reintroduce them here, be they applicable. +//! We should occasionally track the upstream changes/fixes and reintroduce them here, whenever applicable. #[cfg(not(feature = "std"))] use alloc::vec::Vec; From 25bb7ee3b2984b81a0f916216e474bcc90bccdc2 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 31 Dec 2019 17:58:19 +0300 Subject: [PATCH 12/12] add license preamble --- parity-util-mem/derive/lib.rs | 19 +++++++++++++++++++ parity-util-mem/tests/derive.rs | 16 ++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/parity-util-mem/derive/lib.rs b/parity-util-mem/derive/lib.rs index b4f0759fd..c1c1e504e 100644 --- a/parity-util-mem/derive/lib.rs +++ b/parity-util-mem/derive/lib.rs @@ -1,4 +1,23 @@ +// Copyright 2015-2019 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 . + //! A crate for deriving the MallocSizeOf trait. +//! +//! This is a copy of Servo malloc_size_of_derive code, modified to work with +//! our `parity_util_mem` library extern crate proc_macro2; #[macro_use] diff --git a/parity-util-mem/tests/derive.rs b/parity-util-mem/tests/derive.rs index b525ae42f..10dc6975d 100644 --- a/parity-util-mem/tests/derive.rs +++ b/parity-util-mem/tests/derive.rs @@ -1,3 +1,19 @@ +// Copyright 2015-2019 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 . + use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; #[test]