Use SmallVec in Bitfield (#3025)

## Issue Addressed

Alternative to #2935

## Proposed Changes

Replace the `Vec<u8>` inside `Bitfield` with a `SmallVec<[u8; 32>`. This eliminates heap allocations for attestation bitfields until we reach 500K validators, at which point we can consider increasing `SMALLVEC_LEN` to 40 or 48.

While running Lighthouse under `heaptrack` I found that SSZ encoding and decoding of bitfields corresponded to 22% of all allocations by count. I've confirmed that with this change applied those allocations disappear entirely.

## Additional Info

We can win another 8 bytes of space by using `smallvec`'s [`union` feature](https://docs.rs/smallvec/1.8.0/smallvec/#union), although I might leave that for a future PR because I don't know how experimental that feature is and whether it uses some spicy `unsafe` blocks.
This commit is contained in:
Michael Sproul
2022-02-17 23:55:04 +00:00
parent 0a6a8ea3b0
commit da4ca024f1
6 changed files with 174 additions and 110 deletions

2
Cargo.lock generated
View File

@@ -1678,6 +1678,7 @@ dependencies = [
"serde",
"serde_derive",
"serde_json",
"smallvec",
"tree_hash",
"tree_hash_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"typenum",
@@ -6496,6 +6497,7 @@ dependencies = [
"serde_json",
"serde_yaml",
"slog",
"smallvec",
"state_processing",
"superstruct",
"swap_or_not_shuffle",