pub struct Collector { /* private fields */ }Expand description
后端侧帧统计收集器:把“要不要测 / 怎么记账 / 不许伪造“集中在一处。
为什么抽出它:A 阶段落地的三个后端(X11、Web、Win32 软路径)各自手写了
6 个几乎相同的辅助方法(begin_frame / mark / record / dirty /
mark_skipped / finish),共约 120 行重复代码。重复的不只是行数 ——
“测不到就写 0” 这类规则一旦各写一遍,迟早有后端写错。
语义(与手写版本逐条相同):
- 关闭统计时
begin_frame只做一次Option赋值,不读时钟、不分配; mark()在关闭时返回None,于是record什么都不做;- 每帧复用同一个
RenderStats(reset_for_next_frame),零堆分配; take()是取走语义:同一帧第二次调用必得None,不会重复消费。
Implementations§
Source§impl Collector
impl Collector
Sourcepub fn new(backend: BackendKind) -> Self
pub fn new(backend: BackendKind) -> Self
建一个收集器(backend 决定能力表,进而决定 validate() 的判据)。
Sourcepub fn begin_frame(&mut self)
pub fn begin_frame(&mut self)
每帧开始:统计开启时复用/新建,关闭时清空(不分配)。
Sourcepub fn record_elapsed(&mut self, id: StageId, elapsed: Option<Duration>)
pub fn record_elapsed(&mut self, id: StageId, elapsed: Option<Duration>)
记录一段已完成的耗时(替代“先 mark 再 record“的场景,Web 用得上)。
Sourcepub fn record(&mut self, id: StageId, start: Option<Instant>)
pub fn record(&mut self, id: StageId, start: Option<Instant>)
记录“从 start 到现在“的耗时(start == None 时不动)。
Sourcepub fn mark_skipped(&mut self)
pub fn mark_skipped(&mut self)
跳过帧(没有渲染):统计对象照常交付,但标 frame_skipped。
Sourcepub fn finish(&mut self)
pub fn finish(&mut self)
帧末:把 CPU 后端确实没有的量显式写 0(不是“测得 0“)。
presented 不在这里写 —— 它归 Runtime 所有(B1 裁决)。
只在成功出口调用;失败帧应由调用方 discard(),不交付半截数据。
Sourcepub fn stats_mut(&mut self) -> &mut Option<RenderStats>
pub fn stats_mut(&mut self) -> &mut Option<RenderStats>
借出底层 Option<RenderStats>(仅供批量写入与测试)。
为什么保留这个“后门“:record_upload 这类“一次写四段“的辅助函数与单元测试
需要直接构造/检查统计对象;走 Collector 的逐个 record_elapsed 会让测试
也必须先把环境变量打开(enabled() 是进程级 OnceLock,一开全进程都变),
反而更容易写出“依赖执行顺序“的测试。
Sourcepub fn take(&mut self) -> Option<RenderStats>
pub fn take(&mut self) -> Option<RenderStats>
取走本帧统计(取走语义:第二次调用必为 None)。
Trait Implementations§
Auto Trait Implementations§
impl Forget for Collector
impl Freeze for Collector
impl Move for Collector
impl RefUnwindSafe for Collector
impl Send for Collector
impl Sync for Collector
impl Unpin for Collector
impl UnsafeUnpin for Collector
impl UnwindSafe for Collector
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more