[−][src]Trait succinct::SpaceUsage
Computes the space usage of an object.
We calculate the space usage as split into two portions, the heap
portion (returned by heap_bytes
and the stack portion (returned by
stack_bytes
). The stack portion is the statically-known size for
every object of its type as allocated on the stack; the dynamic
portion is the additional heap allocation that may depend on
run-time factors.
Examples:
-
Primitive types like
u32
andusize
are stack-only. -
A tuple or struct type is stack-only when all its components are. Its heap portion is the sum of their heap portions, but its stack portion may exceed the sum of their stack portions because of alignment and padding.
-
The size of a vector includes a stack portion, the vector struct itself, and a heap portion, the array holding its elements. The heap portion of a vector includes the stack portions of its elements. (Should they be called something else for this reason? I considered static/dynamic, but
Box
shows why that doesn’t express exactly the right property.)
Required methods
fn is_stack_only() -> bool
Is the size of this type known statically?
If this method returns true then heap_bytes
should always
return 0.
fn heap_bytes(&self) -> usize
Calculates the heap portion of the size of an object.
This is the memory used by (or, rather, owned by) the object, not
including any portion of its size that is
included in stack_bytes
. This is typically for containers
that heap allocate varying amounts of memory.
Provided methods
fn total_bytes(&self) -> usize
Computes the size of the receiver in bytes.
This includes not just the immediate stack object, but any heap memory that it owns.
The default implementation returns
Self::stack_bytes() + self.heap_bytes()
.
fn stack_bytes() -> usize
Calculates the stack portion of the size of this type.
This is the size of the immediate storage that all objects of this type occupy; it excludes storage that objects of the type might allocate dynamically.
The default implementation returns std::mem::size_of::<Self>()
.