Files
DX12/docs/架构分析/Id机制分析.md
2026-03-19 18:27:49 +08:00

47 lines
1.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Id 机制分析
## 作用定位
`Engine/Common/Id.h` 是整个运行时句柄系统的基础。
核心目标是用一个 `u32` 同时表达:
- 资源在数组中的索引;
- 该索引的生命周期代数generation
## 位布局
- `id_type = u32`
- 高位generation`generation_bits = 8`
- 低位index`index_bit = 24`
对应关键常量与工具:
- `index_mask`:提取低位索引
- `generation_mask`:提取高位代数
- `invalid_id`:无效句柄哨兵值
## 核心函数语义
- `is_valid(id)`:判断是否不是无效值
- `index(id)`:提取索引位
- `generation(id)`:提取代数位
- `new_generation(id)`:索引不变,代数递增
## 运行时价值
它主要解决“删除后旧句柄仍被外部持有”的问题:
1. 对象删除后,槽位可复用;
2. 复用时代数 +1
3. 旧句柄的 generation 与当前 generation 不一致;
4. 存活校验失败,避免误访问新对象。
## 强类型策略
`DEFINE_TYPED_ID(name)`
- Debug生成包装类型增强类型隔离
- Release退化为 `id::id_type`,零额外开销。
这是“开发期安全 + 发布期性能”的组合设计。