Skip to content

Refactor: 消除 _setActiveComponents 中的内部无效遍历 #2954

@GuoLei1990

Description

@GuoLei1990

概述

PR #2948 通过 children-first 遍历 + scene null guard 修复了 #2947 的崩溃问题,用户侧回调行为完全正确。但两阶段设计导致内部无效遍历:被早期回调的 removeChild 清除了 _scene 的组件仍然进入 _setActiveComponents 循环,只是被 guard 跳过:

```typescript
if (!isActive && !component._entity._scene) continue; // 无效遍历
```

约束

  • 不能改变派发时序:当前是 children-first 收集、统一派发,回调顺序为 children → parent,必须保持

方案方向

在保持两阶段架构和派发时序的前提下,减少无效遍历。例如:

  • removeChild_traverseSetOwnerScene(entity, null) 时,同步从 activeChangedComponents 数组中移除对应组件(或置 null 标记跳过)
  • 或在组件上加轻量标记位,让循环体判断更高效

关联

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions