bevyengine / bevyengine/bevy

Change Detection as Components / Opt-in or opt-out of Change Detection

Open
#4,882 18 comments 10 reactions 0 assignees View on GitHub
A-ECS C-Code-Quality C-Performance S-Needs-Design-Doc X-Needs-SME
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## What problem does this solve or what need does it fill?
Currently change detection is hard-coded from userspace all the way to ECS storage. This adds quite a bit of extra effort to maintain it at every level in bevy_ecs. Logically speaking, the change ticks included are always kept parallel to the base storage, and can be seen as a companion component that is always present with every component in the World. If this is the case, it may make sense to actually make the change detection ticks their own component.

This can allow for disabling change detection entirely for types which can remove the memory and CPU overhead for small component types that don't really need it (i.e. usize newtypes or ZSTs).

This would also remove all storage-level special casing required for change detection. For example, moving entities from table to table doesn't require knowledge of the change ticks. It's copied from column to column like any other component.

## What solution would you like?

- Remove all special casing for `ComponentTicks` in storage and add `ChangeTicks` as an engine provided component.
- Add Component hooks that automatically add or remove the ticks if the component is added/removed. This can be done via hooks or be the only hard-coded part of this.
- Update `Mut` to fetch both `&mut T` and `&mut ChangeTicks`.
- Add an associated type to `Component` that changes `WriteFetch` to retrieve `&mut T` when change detection is disabled, and `Mut` when it isn't. Update the derive macro to enable/disable this behavior.
- Update change detection fetches/filters to rely on `Component>`

## What alternative(s) have you considered?

1. Leave change detection as it is.
2. Alter storage to use `Option>>` instead.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.