You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 10, 2023. It is now read-only.
A central idea behind this crate is that most resources in Rust have one well-defined owner. heap_size_of_children can trace recursively and only consider owned resources. For &T references it can return zero since they usually either point to non-heap memory, or to heap memory owned by something else that will be accounted for there.
This breaks down for types like Rc<T> and Arc<T> that introduce "shared ownership". They do have heap memory that we want to count, but in a fully generic context there is no clear single point where it should be counted.
(In a more specific context there count be a "main" Rc reference for a given resource. For example, following only the "first child" and "next sibling" references in a Kuchiki tree allows traversing the whole tree without duplication.)
This impl is returns an incorrect number for two reasons:
It’s too low because it only counts the size of resources owned by T, not the size of T itself and the two reference counts that Arc holds internally. This part is easy to fix.
It’s too high because there could be many Arc references to the same T value, and this impl duplicates by that many times the count of memory owned by T.
One idea that I’ve heard/read (I don’t remember from who, sorry) was to divide by the strong reference count. If all strong references are traced correctly these fractions should add up to the correct value. But the usize return type would have to be changed to f32 or f64 to get remotely accurate results, which is weird at best. And Arc::strong_count and Rc::strong_count are still #[unstable]: Tracking issue for Arc/Rc extras rust-lang/rust#28356
The current version of this crate has another impl:
impl<T>HeapSizeOfforVec<Rc<T>>{fnheap_size_of_children(&self) -> usize{// The fate of measuring Rc<T> is still undecided, but we still want to measure// the space used for storing them.unsafe{heap_size_of(self.as_ptr()as*constc_void)}}}
My understanding of it as that, in practice, when we can’t have a correct impl of HeapSizeOf, Servo sometimes implement it (incorrectly) returning zero because an underestimated answer is better(?) than no answer.
This Vec<Rc<T>> follows this idea, just making the error smaller: don’t count Rc’s but count the Vec containing them.
Still, I don’t think there’s a reason to treat Rc<T> and Arc<T> differently.
A central idea behind this crate is that most resources in Rust have one well-defined owner.
heap_size_of_childrencan trace recursively and only consider owned resources. For&Treferences it can return zero since they usually either point to non-heap memory, or to heap memory owned by something else that will be accounted for there.This breaks down for types like
Rc<T>andArc<T>that introduce "shared ownership". They do have heap memory that we want to count, but in a fully generic context there is no clear single point where it should be counted.(In a more specific context there count be a "main"
Rcreference for a given resource. For example, following only the "first child" and "next sibling" references in a Kuchiki tree allows traversing the whole tree without duplication.)In the current version (90dd7e0) this crate has:
This impl is returns an incorrect number for two reasons:
It’s too low because it only counts the size of resources owned by
T, not the size ofTitself and the two reference counts thatArcholds internally. This part is easy to fix.It’s too high because there could be many
Arcreferences to the sameTvalue, and this impl duplicates by that many times the count of memory owned byT.One idea that I’ve heard/read (I don’t remember from who, sorry) was to divide by the strong reference count. If all strong references are traced correctly these fractions should add up to the correct value. But the
usizereturn type would have to be changed tof32orf64to get remotely accurate results, which is weird at best. AndArc::strong_countandRc::strong_countare still#[unstable]: Tracking issue for Arc/Rc extras rust-lang/rust#28356The current version of this crate has another impl:
My understanding of it as that, in practice, when we can’t have a correct impl of
HeapSizeOf, Servo sometimes implement it (incorrectly) returning zero because an underestimated answer is better(?) than no answer.This
Vec<Rc<T>>follows this idea, just making the error smaller: don’t countRc’s but count theVeccontaining them.Still, I don’t think there’s a reason to treat
Rc<T>andArc<T>differently.