AGENT SKILL

ac-memory-layout-and-caches

How a program's memory behaves on real hardware: cache lines and what sharing one costs, the memory hierarchy's latency ladder, false sharing and padding as a fix, aligned access and unaligned penalties, prefetching and the access pattern that makes it useless, page faults and the major-minor split, NUMA and the locality that decides remote memory costs, and the written layout record. It explains how data placement performs and how to observe it; it never recommends a processor and never promises a speedup. Use when asking "cache line", "false sharing", "memory alignment", "prefetch", "page fault", "NUMA", "memory latency", "cache miss", "why is iteration order so much faster", "padded structure", "locality of reference". Not for profiling methodology in general (ac-performance), not for page allocation inside a database engine (ac-query-and-index-design), not for OS process memory limits (ac-resource-exhaustion-diagnosis).

License 许可
MIT
最近更新
2026-11-15
Stars
★ 81
Forks
17

01 前言 Front Matter

name
ac-memory-layout-and-caches
status
seedling
license
MIT
metadata
{ "domain": "software-engineering" }
description
How a program's memory behaves on real hardware: cache lines and what sharing one costs, the memory hierarchy's latency ladder, false sharing and padding as a fix, aligned access and unaligned penalties, prefetching and the access pattern that makes it useless, page faults and the major-minor split, NUMA and the locality that decides remote memory costs, and the written layout record. It explains how data placement performs and how to observe it; it never recommends a processor and never promises a speedup. Use when asking "cache line", "false sharing", "memory alignment", "prefetch", "page fault", "NUMA", "memory latency", "cache miss", "why is iteration order so much faster", "padded structure", "locality of reference". Not for profiling methodology in general (ac-performance), not for page allocation inside a database engine (ac-query-and-index-design), not for OS process memory limits (ac-resource-exhaustion-diagnosis).

02 资源文件 11

03 SKILL.md 正文

Memory layout and caches

Overview

A program's memory accesses land on a hierarchy, not a flat store: registers, L1 through L3 caches, DRAM, and beyond. Each step down costs an order of magnitude more time, and the hardware accelerates the common case by fetching in cache-line units and predicting access patterns. A program whose data layout cooperates with that machinery can be several times faster than one with identical algorithmic complexity laid out against the grain.

This skill covers the layout decisions that interact with the hierarchy: cache-line sharing and false sharing, alignment, access order and prefetch, and NUMA locality. These are design-level choices — how a struct is ordered, how a matrix is traversed, which thread writes which field — and they are observable directly in profiler counters rather than only in end-to-end timing.

Primary sources: https://www.akkadia.org/drepper/cpumemory.pdf and https://man7.org/linux/man-pages/man2/mmap.2.html.

When to use

  • When a hot loop is slower than its algorithm predicts, or two equivalent traversals differ.
  • When threads share a structure and scaling degrades with thread count.
  • When a syscall with eager memory setup is outperformed by a lazy variant.
  • When a data-parallel computation degrades on a multi-socket machine.

When NOT to use

  • For profiling methodology and flame graphs (use ac-performance).
  • For database page and buffer management (use ac-query-and-index-design).
  • For process memory limits and OOM diagnosis (use ac-resource-exhaustion-diagnosis).

Subskill Index

Leaf Domain segment Scope (one line)
subskills/cache-lines-and-coherence.md software-engineering/cache-lines-and-coherence The unit of transfer and what sharing a line across cores costs.
subskills/hierarchy-latency.md software-engineering/hierarchy-latency The latency ladder and the arithmetic that makes locality decisive.
subskills/false-sharing-and-padding.md software-engineering/false-sharing-and-padding Two threads, one cache line, and the padding that separates them.
subskills/alignment-and-unaligned-access.md software-engineering/alignment-and-unaligned-access What alignment guarantees, and when reading unaligned is safe or traps.
subskills/prefetch-and-access-order.md software-engineering/prefetch-and-access-order Sequential streams, strided access, and the patterns hardware predicts.
subskills/page-faults-and-major-minor.md software-engineering/page-faults-and-major-minor The faults that hit storage, the ones that do not, and page residency.
subskills/numa-and-locality.md software-engineering/numa-and-locality Remote memory costs, allocation affinity, and migration.
subskills/layout-record.md software-engineering/layout-record The written record: layout decisions and the measurements behind them.

Status and honest limits

Cache behaviour varies by processor and even by SKU; counters exist for it, but their names and units differ across vendors, and a tuning that helps one chip can hurt another. Latency-aware design also compromises readability — padded structs and split arrays confuse as often as they accelerate, and the honest method is to measure the hot path before restructuring it, not to rearrange layouts speculatively. Where microarchitecture claims matter, primary vendor documentation is the source, and anything here is a starting hypothesis, not a portable law.

04 同仓库更多 Skills

jiayaoqijia
jiayaoqijia/cryptoskill

troves

Deposit and withdraw from Troves DeFi yield vault strategies on Starknet. View available strategies, check positions, and manage vault deposits. Use this skill when the user mentions Troves, vault, yield vault, DeFi vault, strategy, yield farming, vault deposit, vault withdraw, earning yield through vaults, passive yield strategies, or wants to deposit into or withdraw from a yield strategy — even if they don't say "Troves" explicitly.

★ 81
jiayaoqijia
jiayaoqijia/cryptoskill

trade

Swap tokens on Starknet via Fibrous DEX aggregator. Supports per-provider selection (avnu, ekubo, or auto to race all). Use this skill when the user wants to swap, exchange, trade, convert, buy, or sell one token for another on Starknet. Also use when the user asks about token prices, exchange rates, DEX routing, best swap route, or wants to check how much they would receive for a given amount — even if they don't explicitly say "swap".

★ 81
jiayaoqijia
jiayaoqijia/cryptoskill

staking

Stake, unstake, claim, and compound rewards for STRK, WBTC, tBTC, SolvBTC, and LBTC on Starknet. View validators, pools, and staking positions. Use this skill when the user mentions staking, delegating, validators, staking pools, rewards, compounding, unstaking, earning yield, passive income on crypto, or wants to earn on their STRK or BTC tokens — even if they don't say "stake" explicitly.

★ 81
jiayaoqijia
jiayaoqijia/cryptoskill

send

Send STRK, ETH, or any ERC-20 token to a Starknet address. Supports simulation (dry-run) before execution. Use this skill when the user wants to send, transfer, pay, tip, or move tokens to another wallet address on Starknet. Also trigger when the user mentions sending funds to someone, making a payment, transferring assets, or moving crypto to a specific address — even if they just say "pay 10 STRK to 0x...".

★ 81
jiayaoqijia
jiayaoqijia/cryptoskill

portfolio

View a comprehensive DeFi portfolio dashboard and optimize portfolio allocation via automated rebalancing. Use this skill when the user wants an overview, summary, dashboard, total value, net worth, portfolio rebalancing, target allocation, or wants to see all their DeFi positions across Starknet at once. Also trigger when the user asks "what do I have", "show me everything", "how much am I worth", "my positions", "my investments", "rebalance my portfolio", "I want 50% ETH", "optimize my holdings", or any request for a holistic view of their Starknet holdings — even if they don't explicitly say "portfolio".

★ 81
jiayaoqijia
jiayaoqijia/cryptoskill

multi-swap

Execute multiple token swaps in a single Starknet transaction via Fibrous (default). Supports per-provider selection (avnu, ekubo, or auto to race all). Supports 2-3 swap pairs bundled into one multicall. Use this skill when the user wants to swap multiple token pairs at once, do batch swaps, execute several trades simultaneously, perform parallel swaps, or do bulk trading in one transaction. Also trigger when the user mentions "two swaps", "three swaps", "swap X and Y at the same time", or wants to convert multiple tokens in a single call.

★ 81