Rust mem layout

From KilimcininKorOglu/cli-tweaks · View interactive version

Rust mem layout is an AI agent skill from the GitHub repository KilimcininKorOglu/cli-tweaks. Reduces the memory footprint and allocation count of Rust data structures held in bulk (caches, maps, record lists, arenas) or on memory-constrained targets. Scans the crate for the five layout anti-patterns from Cloudflare's 1.1.1.1 DNS-cache optimization (growable containers on immutable data, par

How to use this skill

Skill file

Rust Memory Layout — scan & fix

Find structs that are instantiated in bulk (cache entries, records, nodes, events) and cut their per-instance footprint and allocation count without changing behavior. Based on the five optimizations Cloudflare applied to Big Pineapple's DNS cache (953 B → 420 B per entry, −56%; inserts +43%, lookups −19%): <https://blog.cloudflare.com/dns-cache-memory-optimization-1111/>

Default behavior is scan (dry-run). Layout changes touch every construction/read site of a type. Fix only after reviewing the scan report.

Usage

/rust-mem-layout          # Scan only — report candidates with estimated savings (DEFAULT)
/rust-mem-layout scan     # Same as default
/rust-mem-layout fix      # Apply fixes from the last scan, one pattern at a time

Scope rule

Cardinality sets priority, and for the patterns that add a cost it sets whether the trade pays off — it never decides whether a change is allowed. The five patterns split in two:

Before anything else, identify bulk types, because they carry both the highest priority and the case for the cost-bearing patterns:

On a normal-RAM target, report a near-free hit on a low-cardinality type as low priority (the win is real but tiny against the diff churn); on a memory-constrained target, fix it. Report a cost-bearing hit on a non-bulk type as low priority and do not fix it unless asked.

Phase 1: Measure

Never estimate when you can measure.

# Exact size/alignment/padding per type (nightly only)
RUSTFLAGS="-Zprint-type-sizes" cargo +nightly build --release 2>&1 \
  | grep -A20 'type: `crate::path::TypeName`'

# Clippy layout lints (stable)
cargo clippy --all-targets -- -W clippy::large_enum_variant -W clippy::box_collection -W clippy::vec_box

If nightly is unavailable, add a temporary test:

#[test]
fn print_sizes() {
    use std::mem::{size_of, align_of};
    macro_rules! p { ($t:ty) => { eprintln!("{:<40} size={:>4} align={}", stringify!($t), size_of::<$t>(), align_of::<$t>()); } }
    p!(CacheEntry); p!(Record); p!(RecordData); p!(CacheKey);
}

Run with cargo test print_sizes -- --nocapture. Record numbers in the scan report as before.

For allocation counts, wrap the global allocator in tests/benches:

use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};

pub struct Counting;
pub static ALLOCS: AtomicUsize = AtomicUsize::new(0);
pub static BYTES:  AtomicUsize = AtomicUsize::new(0);

unsafe impl GlobalAlloc for Counting {
    unsafe fn alloc(&self, l: Layout) -> *mut u8 {
        ALLOCS.fetch_add(1, Relaxed); BYTES.fetch_add(l.size(), Relaxed);
        System.alloc(l)
    }
    unsafe fn dealloc(&self, p: *mut u8, l: Layout) { System.dealloc(p, l) }
}
#[global_allocator] static A: Counting = Counting;
// Measure: snapshot ALLOCS/BYTES, construct N entries, diff / N.

Phase 2: Scan patterns

Grep each bulk struct for the five patterns. For each hit, compute the estimated saving per instance and multiply by the instance count if known.

P1 — Growable container on immutable data

Vec<T> = ptr + len + cap (24 B). String = same. If the field is never mutated after construction, cap is dead weight and the heap buffer may be over-allocated.

Detect: a Vec<T> / String field where no code calls .push, .extend, .insert, .remove, .clear, .truncate, .reserve, .drain, &mut borrow, or std::mem::take on it after the constructor. Check impl blocks, Default, From, serde Deserialize, and builders.

Fix: Vec<T> → Box<[T]>, String → Box<str>. Saves 8 B per field on the struct plus any unused capacity on the heap.

// before
pub answers: Vec<Record>,
pub name: String,
// after
pub answers: Box<[Record]>,
pub name: Box<str>,

Conversion: vec.into_boxed_slice() / string.into_boxed_str(). Both call shrink_to_fit first, which may reallocate — acceptable at construction, but see P5 for the scratch-buffer approach if this is on a hot insert path.

Option<Box<str>> and Option<Box<[T]>> are niche-optimized (no extra tag byte) — free to use.

P2 — Parallel lists of the same element type

Several Box<[T]> / Vec<T> fields holding the same T each cost 16–24 B of header and a separate allocation.

Detect: ≥ 2 fields of identical element type in one struct (e.g. answers, authority, additional: Vec<Record>).

Fix: one Box<[T]> plus small offsets marking section boundaries. Choose the offset width from the real maximum count (u8/u16). Expose the sections via accessor methods so call sites don't change.

pub struct Entry {
    records: Box<[Record]>,
    authority_start: u16,
    additional_start: u16,
}
impl Entry {
    pub fn answers(&self)    -> &[Record] { &self.records[..self.authority_start as usize] }
    pub fn authority(&self)  -> &[Record] { &self.records[self.authority_start as usize..self.additional_start as usize] }
    pub fn additional(&self) -> &[Record] { &self.records[self.additional_start as usize..] }
}

Three lists → one: saves 2×16 B headers − 2×2 B offsets = 28 B, and two allocations.

Also in this phase — bool packing: ≥ 2 bool fields → pack into a u8 bitfield or bitflags!. The direct saving is small; the real gain is the padding that disappears around them. Re-measure after — savings are often larger than the sum of the removed fields.

Note: repr(Rust) already reorders fields to minimize padding. Do not manually reorder fields unless the struct is #[repr(C)]; instead reduce field count and width.

P3 — Derivable fields

Data that can be recovered from context at read time (the map key, a parent struct, a global table) is stored redundantly per instance.

Detect: a field whose value equals, or is almost always equal to, something available at every access site — typically the key of the map the struct lives in. Examples: record owner name == query name; child parent_id when the child is only reached via the parent; a kind tag duplicated from the enum variant.

Fix: store Option<T> (or Option<Box<T>> for large T) and use None for the common "same as context" case. Reconstruct in the accessor from the context that is already in hand.

pub struct Record {
    owner: Option<Box<Name>>,   // None => same as the CacheKey's qname
    ...
}
impl Record {
    pub fn owner<'a>(&'a self, key: &'a CacheKey) -> &'a Name {
        self.owner.as_deref().unwrap_or(&key.qname)
    }
}

Cost: the struct is no longer self-contained. Only apply when every read site already has the context; if it requires threading a new parameter through many layers, report it instead of fixing.

P4 — Enum sized by its rarest variant

An enum is as large as its largest variant. If the common variants are small and a rare one is big, every common instance pays the difference in padding.

Detect: -Zprint-type-sizes shows variant sizes with one outlier; or clippy::large_enum_variant fires. Confirm with the actual distribution — which variants dominate at runtime.

Fix: box the large/rare variants, keep small/common ones inline.

pub enum RecordData {
    A(Ipv4Addr),          // 4 B, inline
    Aaaa(Ipv6Addr),       // 16 B, inline
    Txt(Box<Txt>),        // large → heap
    Naptr(Box<Naptr>),
    Svcb(Box<Svcb>),
}

The enum shrinks to max(inline variants, 8) + tag + padding (typically 24 B). Boxed variants pay one extra allocation and allocator size-class rounding (jemalloc/glibc round up to bins: a 40 B request costs 48 B). Accept this only when the boxed variants are genuinely rare. If they are common, go to P5 instead.

P5 — Per-record heap boxes → one packed byte buffer

If a struct holds many small variable-size records (each boxed after P4), the boxes fragment the heap, hurt cache locality, and each carries allocator overhead.

Detect: a Box<[Record]> where Record contains Box<...> variants or nested Box<str>/Box<[u8]>, and the struct already has (or could trivially have) a canonical byte encoding (wire format, protobuf, a fixed header + payload).

Fix: store the records as a single Box<[u8]> of [len: u16][bytes]... frames. Iterate sequentially; random indexing is lost, which is fine for small counts.

pub struct Entry {
    records: Box<[u8]>,       // u16-length-prefixed frames, section offsets in bytes
    authority_start: u32,
    additional_start: u32,
}

pub struct Frames<'a>(&'a [u8]);
impl<'a> Iterator for Frames<'a> {
    type Item = &'a [u8];
    fn next(&mut self) -> Option<&'a [u8]> {
        if self.0.len() < 2 { return None; }
        let n = u16::from_le_bytes([self.0[0], self.0[1]]) as usize;
        let (frame, rest) = self.0[2..].split_at(n);
        self.0 = rest;
        Some(frame)
    }
}

Build the buffer through a reusable scratch Vec<u8> kept across inserts (thread-local or owned by the writer), then copy into an exactly-sized Box<[u8]>:

scratch.clear();
for r in records { encode(&mut scratch, r); }
let packed: Box<[u8]> = scratch.as_slice().into();   // one exact allocation, no shrink

Do not Vec::with_capacity(guess) then into_boxed_slice() — the shrink may not release the tail. Do not encode straight into a fresh Vec per insert — that's the reallocation churn the scratch buffer exists to avoid.

Records that need no interpretation on read (fixed layout, no internal pointers) can be memcpy'd straight out of the buffer; only decode the ones that need transformation. This is where lookup latency improves, not just memory.

Phase 3: Scan report format

Output exactly this structure. No changes to files in scan mode.

## rust-mem-layout scan — <crate>

### Bulk types found
| Type | Where held | Est. instances | size_of (before) | align |
|------|------------|----------------|------------------|-------|

### Findings
#### [P1] src/cache/entry.rs:42 — CacheEntry.answers: Vec<Record>
Never mutated after `CacheEntry::new` (checked: 3 impl blocks, 1 builder).
Fix: Box<[Record]>. Saves 8 B/instance + unused capacity. Touches: 4 sites.

#### [P4] src/dns/record.rs:10 — enum RecordData (144 B, largest variant Naptr 136 B)
A/Aaaa variants observed at ~80% of instances.
Fix: box Txt/Naptr/Svcb/... → ~24 B. Saves ~120 B/instance for A/Aaaa. Touches: 11 match sites.

### Low priority (non-bulk)
- [P1] src/config.rs:8 — Config.paths: Vec<String> (1 instance) — skip

### Summary
Est. per-instance: <before> B → <after> B (−N%), allocations: <before> → <after>
Recommended fix order: P1 → P2 → P3 → P4 → P5 (re-measure after each)

Phase 4: Fix mode

Fix mode applies one pattern after another. Continue to the next pattern in the

same turn. A decision the run genuinely needs from the user, such as a benchmark

regression, still ends the turn.

Apply findings in order P1 → P5, one pattern per commit, re-measuring after each. Each step:

  1. Change the type definition.
  2. Update constructors/builders/From/serde impls.
  3. Add accessor methods so read sites keep compiling where possible; update the rest.
  4. Add a size assertion so the gain can't silently regress:

```rust

const _: () = assert!(std::mem::size_of::<CacheEntry>() <= 128);

```

  1. cargo build --release && cargo test && cargo clippy --all-targets
  2. Re-run the Phase 1 measurement, append after numbers to the report.
  3. If a bench exists (criterion/divan), run it. A memory win that costs throughput needs an explicit decision from the user — stop and ask.

Stop and report (do not fix) when:

Verification

# Sizes before/after
RUSTFLAGS="-Zprint-type-sizes" cargo +nightly build --release 2>&1 | grep -E 'type: `.*(Entry|Record|Data)`' -A3

# No behavior change
cargo test --all

# Lints clean
cargo clippy --all-targets -- -D warnings

# Allocation count per instance (counting allocator test)
cargo test --release alloc_per_entry -- --nocapture

# Throughput/latency unchanged or better
cargo bench

Rules

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.