m02-resource
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseResource Management
资源管理
Layer 1: Language Mechanics
第一层:语言机制
Core Question
核心问题
What ownership pattern does this resource need?
Before choosing a smart pointer, understand:
- Is ownership single or shared?
- Is access single-threaded or multi-threaded?
- Are there potential cycles?
该资源需要何种所有权模式?
在选择智能指针之前,请明确:
- 所有权是独占还是共享?
- 访问是单线程还是多线程?
- 是否存在潜在的引用循环?
Error → Design Question
错误 → 设计问题
| Error | Don't Just Say | Ask Instead |
|---|---|---|
| "Need heap allocation" | "Use Box" | Why can't this be on stack? |
| Rc memory leak | "Use Weak" | Is the cycle necessary in design? |
| RefCell panic | "Use try_borrow" | Is runtime check the right approach? |
| Arc overhead complaint | "Accept it" | Is multi-thread access actually needed? |
| 错误 | 不要只说 | 而应询问 |
|---|---|---|
| "需要堆分配" | "使用Box" | 为什么不能放在栈上? |
| Rc内存泄漏 | "使用Weak" | 设计中是否真的需要这个循环? |
| RefCell恐慌 | "使用try_borrow" | 运行时检查是正确的方案吗? |
| Arc开销过大 | "接受它" | 真的需要多线程访问吗? |
Thinking Prompt
思考提示
Before choosing a smart pointer:
-
What's the ownership model?
- Single owner → Box or owned value
- Shared ownership → Rc/Arc
- Weak reference → Weak
-
What's the thread context?
- Single-thread → Rc, Cell, RefCell
- Multi-thread → Arc, Mutex, RwLock
-
Are there cycles?
- Yes → One direction must be Weak
- No → Regular Rc/Arc is fine
在选择智能指针之前:
-
所有权模型是什么?
- 独占所有权 → Box 或自有值
- 共享所有权 → Rc/Arc
- 弱引用 → Weak
-
线程上下文是什么?
- 单线程 → Rc、Cell、RefCell
- 多线程 → Arc、Mutex、RwLock
-
是否存在引用循环?
- 是 → 其中一个方向必须使用Weak
- 否 → 常规Rc/Arc即可
Trace Up ↑
向上追溯 ↑
When pointer choice is unclear, trace to design:
"Should I use Arc or Rc?"
↑ Ask: Is this data shared across threads?
↑ Check: m07-concurrency (thread model)
↑ Check: domain-* (performance constraints)| Situation | Trace To | Question |
|---|---|---|
| Rc vs Arc confusion | m07-concurrency | What's the concurrency model? |
| RefCell panics | m03-mutability | Is interior mutability right here? |
| Memory leaks | m12-lifecycle | Where should cleanup happen? |
当指针选择不明确时,追溯到设计层面:
"应该使用Arc还是Rc?"
↑ 询问:该数据是否跨线程共享?
↑ 查看:m07-concurrency(线程模型)
↑ 查看:domain-*(性能约束)| 场景 | 追溯至 | 问题 |
|---|---|---|
| 混淆Rc与Arc | m07-concurrency | 并发模型是什么? |
| RefCell恐慌 | m03-mutability | 这里使用内部可变性是否正确? |
| 内存泄漏 | m12-lifecycle | 清理操作应该在何处执行? |
Trace Down ↓
向下落地 ↓
From design to implementation:
"Need single-owner heap data"
↓ Use: Box<T>
"Need shared immutable data (single-thread)"
↓ Use: Rc<T>
"Need shared immutable data (multi-thread)"
↓ Use: Arc<T>
"Need to break reference cycle"
↓ Use: Weak<T>
"Need shared mutable data"
↓ Single-thread: Rc<RefCell<T>>
↓ Multi-thread: Arc<Mutex<T>> or Arc<RwLock<T>>从设计到实现:
"需要独占所有权的堆数据"
↓ 使用:Box<T>
"需要共享不可变数据(单线程)"
↓ 使用:Rc<T>
"需要共享不可变数据(多线程)"
↓ 使用:Arc<T>
"需要打破引用循环"
↓ 使用:Weak<T>
"需要共享可变数据"
↓ 单线程:Rc<RefCell<T>>
↓ 多线程:Arc<Mutex<T>> 或 Arc<RwLock<T>>Quick Reference
快速参考
| Type | Ownership | Thread-Safe | Use When |
|---|---|---|---|
| Single | Yes | Heap allocation, recursive types |
| Shared | No | Single-thread shared ownership |
| Shared | Yes | Multi-thread shared ownership |
| Weak ref | Same as Rc/Arc | Break reference cycles |
| Single | No | Interior mutability (Copy types) |
| Single | No | Interior mutability (runtime check) |
| 类型 | 所有权 | 线程安全 | 适用场景 |
|---|---|---|---|
| 独占 | 是 | 堆分配、递归类型 |
| 共享 | 否 | 单线程共享所有权 |
| 共享 | 是 | 多线程共享所有权 |
| 弱引用 | 与Rc/Arc一致 | 打破引用循环 |
| 独占 | 否 | 内部可变性(Copy类型) |
| 独占 | 否 | 内部可变性(运行时检查) |
Decision Flowchart
决策流程图
Need heap allocation?
├─ Yes → Single owner?
│ ├─ Yes → Box<T>
│ └─ No → Multi-thread?
│ ├─ Yes → Arc<T>
│ └─ No → Rc<T>
└─ No → Stack allocation (default)
Have reference cycles?
├─ Yes → Use Weak for one direction
└─ No → Regular Rc/Arc
Need interior mutability?
├─ Yes → Thread-safe needed?
│ ├─ Yes → Mutex<T> or RwLock<T>
│ └─ No → T: Copy? → Cell<T> : RefCell<T>
└─ No → Use &mut T需要堆分配?
├─ 是 → 独占所有权?
│ ├─ 是 → Box<T>
│ └─ 否 → 多线程?
│ ├─ 是 → Arc<T>
│ └─ 否 → Rc<T>
└─ 否 → 栈分配(默认)
存在引用循环?
├─ 是 → 其中一个方向使用Weak
└─ 否 → 常规Rc/Arc
需要内部可变性?
├─ 是 → 需要线程安全?
│ ├─ 是 → Mutex<T> 或 RwLock<T>
│ └─ 否 → T: Copy? → Cell<T> : RefCell<T>
└─ 否 → 使用 &mut TCommon Errors
常见错误
| Problem | Cause | Fix |
|---|---|---|
| Rc cycle leak | Mutual strong refs | Use Weak for one direction |
| RefCell panic | Borrow conflict at runtime | Use try_borrow or restructure |
| Arc overhead | Atomic ops in hot path | Consider Rc if single-threaded |
| Box unnecessary | Data fits on stack | Remove Box |
| 问题 | 原因 | 修复方案 |
|---|---|---|
| Rc循环泄漏 | 相互强引用 | 其中一个方向使用Weak |
| RefCell恐慌 | 运行时借用冲突 | 使用try_borrow或重构代码 |
| Arc开销过大 | 热点路径中的原子操作 | 如果是单线程场景,考虑使用Rc |
| 不必要的Box | 数据可放在栈上 | 移除Box |
Anti-Patterns
反模式
| Anti-Pattern | Why Bad | Better |
|---|---|---|
| Arc everywhere | Unnecessary atomic overhead | Use Rc for single-thread |
| RefCell everywhere | Runtime panics | Design clear ownership |
| Box for small types | Unnecessary allocation | Stack allocation |
| Ignore Weak for cycles | Memory leaks | Design parent-child with Weak |
| 反模式 | 危害 | 更佳方案 |
|---|---|---|
| 处处使用Arc | 不必要的原子操作开销 | 单线程场景使用Rc |
| 处处使用RefCell | 运行时恐慌风险 | 设计清晰的所有权模型 |
| 为小类型使用Box | 不必要的分配 | 栈分配 |
| 忽略Weak解决循环 | 内存泄漏 | 设计父子关系并使用Weak |
Related Skills
相关技能
| When | See |
|---|---|
| Ownership errors | m01-ownership |
| Interior mutability details | m03-mutability |
| Multi-thread context | m07-concurrency |
| Resource lifecycle | m12-lifecycle |
| 场景 | 参考 |
|---|---|
| 所有权错误 | m01-ownership |
| 内部可变性细节 | m03-mutability |
| 多线程上下文 | m07-concurrency |
| 资源生命周期 | m12-lifecycle |