-
Notifications
You must be signed in to change notification settings - Fork 245
Provide #[derive(MallocSizeOf)] that is actually working #291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a727abc
add derive and remove bounds
NikVolf f453528
add smoky test
NikVolf baf450a
add another test
NikVolf 61d84e9
add also for fixed size arrays
NikVolf e5da398
cargo fmt
NikVolf 17016e0
fix tests for different archs
NikVolf 5eeac76
cargo fmt
NikVolf 75863f9
address review
NikVolf 1d894fe
add ignore test
NikVolf 698dd77
Update parity-util-mem/src/malloc_size.rs
NikVolf d160bbf
Update parity-util-mem/src/malloc_size.rs
NikVolf 25bb7ee
add license preamble
NikVolf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,4 +20,5 @@ members = [ | |
| "primitive-types", | ||
| "ethereum-types", | ||
| "ethbloom", | ||
| "parity-util-mem/derive" | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [package] | ||
| name = "parity-util-mem-derive" | ||
| version = "0.1.0" | ||
| authors = ["Parity Technologies"] | ||
| license = "MIT" | ||
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| //! A crate for deriving the MallocSizeOf trait. | ||
|
NikVolf marked this conversation as resolved.
|
||
|
|
||
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; | ||
|
NikVolf marked this conversation as resolved.
|
||
|
|
||
| #[test] | ||
| #[cfg(feature = "std")] | ||
| fn derive_vec() { | ||
| #[derive(MallocSizeOf)] | ||
| struct Trivia { | ||
| v: Vec<u8>, | ||
| } | ||
|
|
||
| let t = Trivia { v: vec![0u8; 1024] }; | ||
|
|
||
| assert!(t.malloc_size_of() > 1000); | ||
| } | ||
|
|
||
|
dvdplm marked this conversation as resolved.
|
||
| #[test] | ||
| #[cfg(feature = "std")] | ||
| fn derive_hashmap() { | ||
| #[derive(MallocSizeOf, Default)] | ||
| struct Trivia { | ||
| hm: std::collections::HashMap<u64, Vec<u8>>, | ||
| } | ||
|
|
||
| let mut t = Trivia::default(); | ||
|
|
||
| t.hm.insert(1, vec![0u8; 2048]); | ||
|
|
||
| assert!(t.malloc_size_of() > 2000); | ||
| } | ||
|
|
||
| #[test] | ||
| #[cfg(feature = "std")] | ||
| fn derive_ignore() { | ||
| #[derive(MallocSizeOf, Default)] | ||
| struct Trivia { | ||
| hm: std::collections::HashMap<u64, Vec<u8>>, | ||
| #[ignore_malloc_size_of = "I don't like vectors"] | ||
| v: Vec<u8>, | ||
| } | ||
|
|
||
| let mut t = Trivia::default(); | ||
|
|
||
| t.hm.insert(1, vec![0u8; 2048]); | ||
| t.v = vec![0u8; 1024]; | ||
|
|
||
| assert!(t.malloc_size_of() < 3000); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.