Skip to content

Commit

Permalink
Mention ThinVec.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Aug 19, 2022
1 parent d2b6f37 commit 881b21d
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/type-sizes.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ without any cloning or a reallocation.
[`Vec::into_boxed_slice`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.into_boxed_slice
[`slice::into_vec`]: https://doc.rust-lang.org/std/primitive.slice.html#method.into_vec

## `ThinVec`

An alternative to boxed slices is `ThinVec`, from the [`thin_vec`] crate. It is
functionally equivalent to `Vec`, but stores the length and capacity in the
same allocation as the elements (if there are any). This means that
`size_of::<ThinVec<T>>` is only one word.

`ThinVec` is a good choice within oft-instantiated types for vectors that are
often empty. It can also be used to shrink the largest variant of an enum, if
that variant contains a `Vec`.

[`thin_vec`]: https://crates.io/crates/thin-vec

## Avoiding Regressions

If a type is hot enough that its size can affect performance, it is a good idea
Expand Down

0 comments on commit 881b21d

Please sign in to comment.