Skip to content

Commit

Permalink
also move smallvec and fixed arrays from impl module
Browse files Browse the repository at this point in the history
  • Loading branch information
NikVolf committed Jan 4, 2020
1 parent d8d6e15 commit 4cfd69a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
4 changes: 2 additions & 2 deletions parity-util-mem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ version = "0.3.2"
optional = true

[features]
default = ["std", "ethereum-impls", "lru", "hashbrown"]
default = ["std", "ethereum-impls", "lru", "hashbrown", "smallvec"]
std = ["parking_lot"]
# use dlmalloc as global allocator
dlmalloc-global = ["dlmalloc", "estimate-heapsize"]
Expand All @@ -46,6 +46,6 @@ jemalloc-global = ["jemallocator"]
# use mimalloc as global allocator
mimalloc-global = ["mimallocator", "mimalloc-sys"]
# implement additional types
ethereum-impls = ["ethereum-types", "smallvec"]
ethereum-impls = ["ethereum-types"]
# Full estimate: no call to allocator
estimate-heapsize = []
36 changes: 0 additions & 36 deletions parity-util-mem/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,10 @@
//! - ethereum types uint and fixed hash.
//! - smallvec arrays of sizes 32, 36
use super::{MallocSizeOf, MallocSizeOfOps};

use ethereum_types::{Bloom, H128, H160, H256, H264, H32, H512, H520, H64, U128, U256, U512, U64};
use smallvec::SmallVec;

#[cfg(not(feature = "std"))]
use core as std;

#[cfg(feature = "std")]
malloc_size_of_is_0!(std::time::Instant);
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<T> MallocSizeOf for SmallVec<[T; $size]>
where
T: MallocSizeOf,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
let mut n = if self.spilled() { self.capacity() * core::mem::size_of::<T>() } else { 0 };
for elem in self.iter() {
n += elem.size_of(ops);
}
n
}
}
};
}

impl_smallvec!(32); // kvdb uses this
impl_smallvec!(36); // trie-db uses this

#[cfg(test)]
mod tests {
use crate::{allocators::new_malloc_size_ops, MallocSizeOf, MallocSizeOfOps};
Expand Down
33 changes: 33 additions & 0 deletions parity-util-mem/src/malloc_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ use std::hash::BuildHasher;
#[cfg(feature = "std")]
use std::os::raw::c_void;

use smallvec::SmallVec;

/// A C function that takes a pointer to a heap allocation and returns its size.
pub type VoidPtrToSizeFn = unsafe extern "C" fn(ptr: *const c_void) -> usize;

Expand Down Expand Up @@ -639,3 +641,34 @@ where
n
}
}

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<T> MallocSizeOf for SmallVec<[T; $size]>
where
T: MallocSizeOf,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
let mut n = if self.spilled() { self.capacity() * core::mem::size_of::<T>() } else { 0 };
for elem in self.iter() {
n += elem.size_of(ops);
}
n
}
}
};
}

impl_smallvec!(32); // kvdb uses this
impl_smallvec!(36); // trie-db uses this

#[cfg(feature = "std")]
malloc_size_of_is_0!(std::time::Instant);
#[cfg(feature = "std")]
malloc_size_of_is_0!(std::time::Duration);

0 comments on commit 4cfd69a

Please sign in to comment.