Skip to content

Commit 4f8e2f6

Browse files
authored
Implement faster copy between tables (#420)
1 parent 92bb92a commit 4f8e2f6

File tree

4 files changed

+10
-12
lines changed

4 files changed

+10
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
- Adds method `World.Shrink` for freeing memory that exceeds current requirements (#323, #417)
1616
- World lock and filters are concurrency-safe, allowing for concurrent query execution (#360. #405)
1717
- Adds methods `ObserverX.New`, `Map.New` and `Resource.New` as shortcuts to avoid repeated parameter listing (#393, #394. #395)
18-
- Adds method `World.CopyEntity` for entity duplication (#415, #418)
18+
- Adds method `World.CopyEntity` for entity duplication (#415, #418, #420)
1919

2020
### Performance
2121

ecs/table.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ func (t *table) Set(component ID, index uint32, src *column, srcIndex uint32) {
114114
t.components[component.id].Set(index, src, srcIndex)
115115
}
116116

117-
// Copy the value of a component to the given row index from another index in the same column.
118-
func (t *table) Copy(component ID, index uint32, srcIndex uint32) {
119-
column := t.components[component.id]
120-
column.Set(index, column, srcIndex)
117+
// CopyAll copies all component values from a row of another table with the same layout into this table.
118+
func (t *table) CopyAll(table *table, index uint32, srcIndex uint32) {
119+
for i := range t.columns {
120+
t.columns[i].Set(index, &table.columns[i], srcIndex)
121+
}
121122
}
122123

123124
// SetEntity sets the entity at the given row index.
@@ -236,7 +237,7 @@ func (t *table) Reset() {
236237
t.len = 0
237238
}
238239

239-
// AddAll adds all entities with components from another table to this table.
240+
// AddAll adds all entities with components from another table with the same layout to this table.
240241
func (t *table) AddAll(from *table, count uint32) {
241242
t.Alloc(count)
242243
t.entities.CopyToEnd(&from.entities, t.len, count)

ecs/world.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,8 @@ func (w *World) CopyEntity(e Entity) Entity {
102102

103103
archetype := &s.archetypes[table.archetype]
104104

105-
for _, id := range archetype.components {
106-
table.Copy(id, idx, index.row)
107-
}
105+
table.CopyAll(table, idx, index.row)
106+
108107
w.storage.observers.FireCreateEntityIfHas(entity, &archetype.mask)
109108
return entity
110109
}

ecs/world_internal.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,7 @@ func (w *World) setRelations(entity Entity, relations []relationID) {
394394

395395
newIndex := newTable.Add(entity)
396396

397-
for _, id := range oldArch.components {
398-
newTable.Set(id, newIndex, oldTable.Column(id), index.row)
399-
}
397+
newTable.CopyAll(oldTable, newIndex, index.row)
400398

401399
swapped := oldTable.Remove(index.row)
402400

0 commit comments

Comments
 (0)