MCPcopy Index your code
hub / github.com/Totoro-jam/battle-tested-patterns

github.com/Totoro-jam/battle-tested-patterns @v1.16.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.16.0 ↗ · + Follow
3,382 symbols 7,858 edges 496 files 345 documented · 10% updated 1d agov1.16.1 · 2026-07-07★ 3187 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Battle-Tested Patterns

从顶级开源项目源码中提炼的代码级编程模式。

交互式可视化(支持时间旅行回放)· 精确到行号的源码链接 · 多语言实现 · 可运行练习

📖 Documentation · 📖 中文文档

English | 简体中文

CI Deploy License: MIT PRs Welcome

LRU Cache 模式 — 交互式可视化、属性表格与侧边导航

46 个模式速览

**🧠 数据结构** - [Bitmask](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/bitmask/) — 标志位压缩 - [Min Heap](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/min-heap/) — 优先队列 - [Ring Buffer](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/ring-buffer/) — 定长 FIFO - [Trie](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/trie/) — 前缀搜索 - [Skip List](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/skip-list/) — 概率有序 - [Bloom Filter](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/bloom-filter/) — 集合判存 - [LRU Cache](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/lru-cache/) — 淘汰策略 - [B+ Tree](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/b-plus-tree/) — 磁盘优化索引 - [Tagged Union](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/tagged-union/) — 类型安全分发 - [Merkle Tree](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/merkle-tree/) — 完整性证明 - [Merge Iterator](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/merge-iterator/) — K 路合并 **⚡ 并发** - [Semaphore](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/semaphore/) — 有界访问 - [Actor Model](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/actor-model/) — 消息传递 - [Work Stealing](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/work-stealing/) — 负载均衡 - [MVCC](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/mvcc/) — 快照隔离 - [Cooperative Scheduling](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/cooperative-scheduling/) — 协作让出 - [Double Buffering](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/double-buffering/) — 原子交换 - [Backpressure](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/backpressure/) — 流量控制 - [Event Loop](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/event-loop/) — I/O 多路复用 - [Logical Clock](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/logical-clock/) — 事件排序 **🏗️ 系统** - [Circuit Breaker](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/circuit-breaker/) — 容错 - [Rate Limiter](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/rate-limiter/) — 限流 - [Retry Backoff](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/retry-backoff/) — 弹性 - [Write-Ahead Log](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/write-ahead-log/) — 持久性 - [Batch Processing](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/batch-processing/) — 吞吐 - [Consistent Hashing](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/consistent-hashing/) — 分布 - [Dependency Graph](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/dependency-graph/) — 排序 - [Middleware Chain](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/middleware-chain/) — 管道 - [Registry](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/registry/) — 自注册 - [Dirty Flag](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/dirty-flag/) — 延迟重算 - [LSM Tree](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/lsm-tree/) — 写优化存储 - [Checkpointing](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/checkpointing/) — 快照恢复
**♻️ 内存** - [Object Pool](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/object-pool/) — 复用实例 - [Flyweight](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/flyweight/) — 共享不可变 - [Arena Allocator](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/arena-allocator/) — bump 分配 - [Free List](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/free-list/) — O(1) 分配释放 - [Copy-on-Write](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/copy-on-write/) — 延迟复制 - [Reference Counting](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/reference-counting/) — 自动清理 - [Tombstone](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/tombstone/) — 延迟删除 - [Interning](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/interning/) — 去重驻留 **🔄 行为型** - [State Machine](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/state-machine/) — 状态转换 - [Observer](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/observer/) — 发布订阅 - [Iterator](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/iterator/) — 惰性求值 - [Diff / Patch](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/diff-patch/) — 最小编辑 - [Vtable](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/vtable/) — 手动多态 - [Visitor](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/visitor/) — 树遍历分发 **📊 验证来源** - React · Linux · Go - Redis · PostgreSQL - Kafka · Chromium - Tokio · Erlang/OTP - LevelDB · RocksDB · etcd - Nginx · Akka - LLVM · Vue · Godot - PyTorch · CPython · ZFS

填补什么空白

现有资源 缺失的部分
设计模式书 太抽象,过于 OOP
算法仓库 脱离工程实践
系统设计指南 架构层面,不涉及代码级

本项目:从 React、Linux、Go、Chromium 等项目中提取的代码级技巧——每个都有可验证的源码链接

编程模式

模式 做什么 验证来源
位掩码 将 N 个标志打包到一个整数,O(1) 检查任意组合 React Flags · Linux stat.h
双缓冲 原子交换两份副本,零分配 React Fiber · SDL
协作调度 在工作块之间让出控制权保持响应 React Scheduler · Go Runtime
最小堆 O(1) 查看最高优先级,O(log n) 插入/删除 React MinHeap · Linux CFS
差异/补丁 计算两个序列之间的最小编辑 React Reconciler · Git
对象池 预分配复用对象,避免 GC 压力 Go sync.Pool · Godot
环形缓冲区 固定大小循环队列,零分配 LMAX Disruptor · Linux
状态机 显式状态,不可能的转换不存在 XState · Linux TCP
写时复制 引用共享,修改时才复制 Git objects · Rust Cow
观察者 订阅事件,生产者消费者解耦 Node EventEmitter · Redux
迭代器 惰性序列,零中间分配 Rust Iterator · Python gen
信号量 计数器限制并发 Linux · Go x/sync
批处理 累积操作批量执行 Kafka
指数退避重试 指数延迟 + 抖动重试 Kubernetes · gRPC
事件循环 单线程循环通过 epoll/kqueue 复用 I/O libuv · Redis ae
享元/驻留 共享相同对象避免重复 Python int cache
布隆过滤器 概率集合成员测试,零漏判 LevelDB · Chromium
熔断器 停止调用故障服务,快速失败 Hystrix · gobreaker
Arena 分配器 区域内推进指针分配,一次性释放 bumpalo · Go arena
B+ 树 高扇出平衡树——内部节点引导,叶节点存储并链接用于范围扫描 PostgreSQL · SQLite
背压 消费者跟不上时减慢生产者 Node.js Streams · [Reactive Streams](https://github.com/reactive-streams/reactive-streams-jvm/blob/a625d3aba756e9842ad

Extension points exported contracts — how you extend this code

ASTVisitor (Interface)
(no doc) [4 implementers]
exercises/rust/src/visitor/mod.rs
Expr (Interface)
(no doc) [3 implementers]
exercises/answers/go/visitor/visitor.go
ShapeVtable (Interface)
A vtable is a record of function pointers for shape operations
exercises/typescript/vtable/01-basic.test.ts
ASTNode (Interface)
(no doc) [2 implementers]
exercises/go/visitor/visitor_test.go
MermaidError (Interface)
(no doc)
scripts/verify-mermaid.ts
PythonInfo (Interface)
(no doc)
scripts/run-python.ts
ParsedLink (Interface)
(no doc)
scripts/verify-line-ranges.ts
Suite (Interface)
(no doc)
scripts/test-exercises.ts

Core symbols most depended-on inside this repo

find
called by 259
exercises/answers/rust/trie/trie.rs
push
called by 125
exercises/rust/src/min_heap/mod.rs
send
called by 80
exercises/typescript/actor-model/02-intermediate.test.ts
commit
called by 59
exercises/rust/src/write_ahead_log/mod.rs
filter
called by 59
exercises/rust/src/iterator/mod.rs
len
called by 45
exercises/answers/rust/ring_buffer/ring_buffer.rs
set
called by 44
exercises/rust/src/copy_on_write/mod.rs
report
called by 39
scripts/lib/patterns.ts

Shape

Method 1,655
Function 998
Class 492
Struct 122
Interface 88
Enum 11
TypeAlias 9
FuncType 7

Languages

TypeScript36%
Python23%
Go21%
Rust20%

Modules by API surface

exercises/python/lsm_tree/test_lsm_tree.py23 symbols
scripts/lib/patterns.ts19 symbols
exercises/python/iterator/test_iterator.py19 symbols
exercises/go/visitor/visitor_test.go19 symbols
exercises/typescript/lsm-tree/02-intermediate.test.ts18 symbols
exercises/answers/typescript/lsm-tree/lsm-tree.ts18 symbols
exercises/typescript/lsm-tree/01-basic.test.ts17 symbols
exercises/python/checkpointing/test_checkpointing.py17 symbols
exercises/answers/python/lsm_tree/lsm_tree.py17 symbols
docs/__tests__/helpers/viz-interactions.ts17 symbols
exercises/typescript/mvcc/02-intermediate.test.ts16 symbols
exercises/python/b_plus_tree/test_b_plus_tree.py16 symbols

For agents

$ claude mcp add battle-tested-patterns \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page