Skip to content

Commit cd9f7d3

Browse files
nicoburnsmbrubeck
authored andcommitted
Implement MallocSizeOf for SmallVec (v1)
1 parent 03d9069 commit cd9f7d3

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

.github/workflows/main.yml

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ jobs:
4848
if: matrix.toolchain != '1.36.0'
4949
run: cargo test --verbose --features serde
5050

51+
- name: Cargo test w/ malloc_size_of
52+
if: matrix.toolchain != '1.36.0'
53+
run: cargo test --verbose --features malloc_size_of
54+
5155
- name: Cargo check w/o default features
5256
if: matrix.toolchain == 'nightly'
5357
run: cargo check --verbose --no-default-features

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ debugger_visualizer = []
2727

2828
[dependencies]
2929
serde = { version = "1", optional = true, default-features = false }
30+
malloc_size_of = { version = "0.1", optional = true, default-features = false }
3031
arbitrary = { version = "1", optional = true }
3132

3233
[dev_dependencies]

src/lib.rs

+29
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ use core::ops::{self, Range, RangeBounds};
126126
use core::ptr::{self, NonNull};
127127
use core::slice::{self, SliceIndex};
128128

129+
#[cfg(feature = "malloc_size_of")]
130+
use malloc_size_of::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps};
131+
129132
#[cfg(feature = "serde")]
130133
use serde::{
131134
de::{Deserialize, Deserializer, SeqAccess, Visitor},
@@ -1971,6 +1974,32 @@ where
19711974
}
19721975
}
19731976

1977+
#[cfg(feature = "malloc_size_of")]
1978+
impl<A: Array> MallocShallowSizeOf for SmallVec<A> {
1979+
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
1980+
if self.spilled() {
1981+
unsafe { ops.malloc_size_of(self.as_ptr()) }
1982+
} else {
1983+
0
1984+
}
1985+
}
1986+
}
1987+
1988+
#[cfg(feature = "malloc_size_of")]
1989+
impl<A> MallocSizeOf for SmallVec<A>
1990+
where
1991+
A: Array,
1992+
A::Item: MallocSizeOf,
1993+
{
1994+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
1995+
let mut n = self.shallow_size_of(ops);
1996+
for elem in self.iter() {
1997+
n += elem.size_of(ops);
1998+
}
1999+
n
2000+
}
2001+
}
2002+
19742003
#[cfg(feature = "specialization")]
19752004
trait SpecFrom<A: Array, S> {
19762005
fn spec_from(slice: S) -> SmallVec<A>;

0 commit comments

Comments
 (0)