bevyengine / bevyengine/bevy

Add a method to bulk insert/remove components in a single archetype move

Open
#16,015 2 comments 0 reactions 0 assignees View on GitHub
A-ECS C-Feature C-Performance D-Complex D-Unsafe S-Ready-For-Implementation
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## What problem does this solve or what need does it fill?

Minimizing archetype moves is a good target for improving performance, but we currently need to do multiple if some components need to be inserted and some need to be removed.

## What solution would you like?

We could provide a method like `world.entity_mut(e1).mutator()`, where `mutator()` would return a RAII-like object that allows the user to queue up operations to be performed, which are applied when the object is dropped:

```rust
impl EntityWorldMut {
pub fn mutator(&mut self) -> EntityMutator<'_>;
}

pub struct EntityMutator<'w, 'a> {
entity: EntityWorldMut<'w>,
// Probably can't store BundleInserters directly, but its to get the idea across
inserts: SmallVec<[BundleInserter; 8]>,
removes: SmallVec<[BundleId; 8]>,
}

impl EntityMutator<'_, '_> {
pub fn insert(&mut self, bundle: T) -> &mut self;
pub fn remove(&mut self) -> &mut Self;
pub fn insert_by_id(&mut self, id: ComponentId, ptr: OwningPtr<'_>) -> &mut Self;
pub fn remove_by_id(&mut self, id: ComponentId) -> &mut Self;
}

impl Drop for EntityMutator<'_, '_> {
fn drop(&mut self) {
// We would actually perform the changes here, on drop
}
}
```

## What alternative(s) have you considered?

Continue generating multiple archetype moves.

## Additional context

[Initial discussion on the Discord server](https://discord.com/channels/691052431525675048/749335865876021248/1297324917666086973).

Contributor guide

Open the contributing guide

Research direction

Start with the proposed EntityWorldMut::mutator API and the EntityMutator, BundleInserter, Bundle, BundleId, ComponentId, and OwningPtr concepts named in the issue. Review the linked Discord discussion before deciding the API and lifecycle semantics. Done means an agreed implementation can queue component inserts and removals and apply them in a single archetype move.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.