游戏编程模式18-优化模式-脏标识模式

优化模式-脏标识模式

参考章节:https://gpp.tkchu.me/dirty-flag.html

脑内画面

脏标识模式给"派生数据"贴一张小纸条:源数据改了,派生数据先标记为脏,不立刻重算;等真的有人需要它时,再一次性更新。它像说"这份报表过期了,下次打开时再刷新"。
#mermaid-svg-egffTiVmdvLSTB9c{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-egffTiVmdvLSTB9c .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-egffTiVmdvLSTB9c .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-egffTiVmdvLSTB9c .error-icon{fill:#552222;}#mermaid-svg-egffTiVmdvLSTB9c .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-egffTiVmdvLSTB9c .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-egffTiVmdvLSTB9c .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-egffTiVmdvLSTB9c .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-egffTiVmdvLSTB9c .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-egffTiVmdvLSTB9c .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-egffTiVmdvLSTB9c .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-egffTiVmdvLSTB9c .marker{fill:#333333;stroke:#333333;}#mermaid-svg-egffTiVmdvLSTB9c .marker.cross{stroke:#333333;}#mermaid-svg-egffTiVmdvLSTB9c svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-egffTiVmdvLSTB9c p{margin:0;}#mermaid-svg-egffTiVmdvLSTB9c .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-egffTiVmdvLSTB9c .cluster-label text{fill:#333;}#mermaid-svg-egffTiVmdvLSTB9c .cluster-label span{color:#333;}#mermaid-svg-egffTiVmdvLSTB9c .cluster-label span p{background-color:transparent;}#mermaid-svg-egffTiVmdvLSTB9c .label text,#mermaid-svg-egffTiVmdvLSTB9c span{fill:#333;color:#333;}#mermaid-svg-egffTiVmdvLSTB9c .node rect,#mermaid-svg-egffTiVmdvLSTB9c .node circle,#mermaid-svg-egffTiVmdvLSTB9c .node ellipse,#mermaid-svg-egffTiVmdvLSTB9c .node polygon,#mermaid-svg-egffTiVmdvLSTB9c .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-egffTiVmdvLSTB9c .rough-node .label text,#mermaid-svg-egffTiVmdvLSTB9c .node .label text,#mermaid-svg-egffTiVmdvLSTB9c .image-shape .label,#mermaid-svg-egffTiVmdvLSTB9c .icon-shape .label{text-anchor:middle;}#mermaid-svg-egffTiVmdvLSTB9c .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-egffTiVmdvLSTB9c .rough-node .label,#mermaid-svg-egffTiVmdvLSTB9c .node .label,#mermaid-svg-egffTiVmdvLSTB9c .image-shape .label,#mermaid-svg-egffTiVmdvLSTB9c .icon-shape .label{text-align:center;}#mermaid-svg-egffTiVmdvLSTB9c .node.clickable{cursor:pointer;}#mermaid-svg-egffTiVmdvLSTB9c .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-egffTiVmdvLSTB9c .arrowheadPath{fill:#333333;}#mermaid-svg-egffTiVmdvLSTB9c .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-egffTiVmdvLSTB9c .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-egffTiVmdvLSTB9c .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-egffTiVmdvLSTB9c .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-egffTiVmdvLSTB9c .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-egffTiVmdvLSTB9c .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-egffTiVmdvLSTB9c .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-egffTiVmdvLSTB9c .cluster text{fill:#333;}#mermaid-svg-egffTiVmdvLSTB9c .cluster span{color:#333;}#mermaid-svg-egffTiVmdvLSTB9c div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-egffTiVmdvLSTB9c .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-egffTiVmdvLSTB9c rect.text{fill:none;stroke-width:0;}#mermaid-svg-egffTiVmdvLSTB9c .icon-shape,#mermaid-svg-egffTiVmdvLSTB9c .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-egffTiVmdvLSTB9c .icon-shape p,#mermaid-svg-egffTiVmdvLSTB9c .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-egffTiVmdvLSTB9c .icon-shape .label rect,#mermaid-svg-egffTiVmdvLSTB9c .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-egffTiVmdvLSTB9c .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-egffTiVmdvLSTB9c .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-egffTiVmdvLSTB9c :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 局部坐标改变
标记 worldPosition 脏
暂不计算
有人读取世界坐标
重新计算并清除脏标记

它解决的问题

有些数据由其他数据推导而来,例如世界变换、导航缓存、合批结果。源数据可能一帧内改很多次,如果每次都立刻重算,会浪费。脏标识把重算延迟到"确实需要"的时候。

C# 示例

csharp 复制代码
public readonly record struct Vec2(float X, float Y)
{
    public static Vec2 operator +(Vec2 a, Vec2 b) => new(a.X + b.X, a.Y + b.Y);
}

public sealed class SceneNode
{
    private readonly List<SceneNode> _children = new();
    private Vec2 _localPosition;
    private Vec2 _worldPosition;
    private bool _dirty = true;

    public SceneNode? Parent { get; private set; }

    public Vec2 LocalPosition
    {
        get => _localPosition;
        set
        {
            _localPosition = value;
            MarkDirty();
        }
    }

    public Vec2 WorldPosition
    {
        get
        {
            if (_dirty)
            {
                RecalculateWorldPosition();
            }

            return _worldPosition;
        }
    }

    public void AddChild(SceneNode child)
    {
        child.Parent = this;
        _children.Add(child);
        child.MarkDirty();
    }

    private void MarkDirty()
    {
        if (_dirty)
        {
            return;
        }

        _dirty = true;

        foreach (var child in _children)
        {
            child.MarkDirty();
        }
    }

    private void RecalculateWorldPosition()
    {
        _worldPosition = Parent == null
            ? _localPosition
            : Parent.WorldPosition + _localPosition;

        _dirty = false;
    }
}

什么时候用

  • 派生数据计算昂贵。
  • 源数据变化频繁,但派生数据不一定马上被读取。
  • 多次修改可以合并成一次重算。

使用时的锋利边

脏标记必须沿依赖链传播。父节点移动时,子节点世界坐标也脏了。如果遗漏传播,读到的就是过期数据。另一方面,过度传播会让延迟计算失去意义。