agentleFS
jscott3201jscott3201/yet-more-skills/skills/rust-memory-layout/SKILL.md

rust-memory-layout

Skill4 starsChanged 2 months agoApache-2.0View on GitHub

Investigate Rust allocations, copies, buffer retention, collection layout, and cache locality. Use for measured memory or data-access bottlenecks, not blanket clone removal.

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 3 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Start with the ownership path and observed cost.

    line 9
  2. Distinctive:

    Prefer moving existing ownership, writing into caller-owned buffers, reserving a justified capacity, or reusing scratch storage within a …

    line 19
  3. Distinctive:

    Keep zero-copy boundaries honest.

    line 21
  4. Very specific:

    Use safe slice/chunk APIs before unchecked indexing or representation casts.

    line 29
  5. Distinctive:

    Read memory investigation notes for implementation-specific questions.

    line 37

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Rust Memory and Data Layout

Start with the ownership path and observed cost. Preserve the existing safety, API, and serialization contracts. Use $rust-performance for the overall experiment when available; this skill focuses on the memory mechanism.

Trace data through the operation

Map input ownership, parsing, intermediate values, queues/caches, output ownership, and destruction. Identify actual allocations and deep copies separately from shallow handles. Measure allocations per operation, bytes allocated, peak live memory, and retained memory under representative steady-state and burst load. RSS is useful but is not identical to live heap bytes or a leak.

Examine common and worst-case sizes and lifetimes. A small borrowed view or Bytes slice may retain a much larger owner. A buffer pool may lower allocation count while raising high-water memory. A change is not better merely because .clone() disappears or an allocation counter falls.

Choose the simplest effective representation

Prefer moving existing ownership, writing into caller-owned buffers, reserving a justified capacity, or reusing scratch storage within a bounded lifetime. Validate input lengths before reserving; attacker-controlled lengths are not safe capacity hints. Use checked arithmetic for byte counts, offsets, multiplication, and conversion across integer widths.

Keep zero-copy boundaries honest. Distinguish no payload copy from no allocation, no reference-count operation, and no later consumer copy. Convert to ownership deliberately when a result must outlive input or enter a background task. A small copy can reduce retention and simplify aliasing.

Consider contiguous layouts, compact indices, inline small values, batching, or structure-of-arrays only for the observed access pattern. Include object size, cache misses, initialization, deletion, and index maintenance in the comparison. Do not assume an inline collection is always cheaper: unused capacity can inflate every object. Prefer existing types and small local code over a dependency without a measured need.

Choose maps and hashers according to adversarial input, deterministic output requirements, and lookup workload. Do not trade away hash-flooding resistance casually. Stable output ordering should be explicit; it must not depend on incidental randomized iteration order.

Preserve safety and portability

Use safe slice/chunk APIs before unchecked indexing or representation casts. Never change repr, field layout, packed access, or byte interpretation merely to silence padding reports; Rust memory layout is not a wire format. Do not bypass a crate's forbid(unsafe_code) policy for a microbenchmark.

For long-lived caches and arenas, define the retention boundary and invalidation owner. Inspect stale IDs and generation checks after deletion or compaction. Avoid making every request retain an entire graph snapshot when a smaller result suffices.

Validate and return

Run behavioral tests and realistic burst/steady-state measurements. Check tiny inputs, large payloads, high concurrency, and repeated clear/reuse cycles. Compare latency and memory together. Report which copy/allocation was removed, the ownership lifetime before and after, measured costs, and any increased retained memory or complexity.

Read memory investigation notes for implementation-specific questions.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.