Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Archetype graph #42

Merged
merged 3 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## [[unpublished]](https://github.com/mlange-42/arche/compare/v0.2.0...main)

Nothing
### Other

* Use of an archetype graph to speed up finding the target archetype for component addition/removal (#42)

## [[v0.2.0]](https://github.com/mlange-42/arche/compare/v0.1.4...v0.2.0)

Expand Down
22 changes: 22 additions & 0 deletions ecs/archetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type archetype struct {
indices [MaskTotalBits]uint8
entities storage
components []storage
toAdd map[ID]*archetype
toRemove map[ID]*archetype
}

var entityType = reflect.TypeOf(Entity{})
Expand All @@ -40,6 +42,8 @@ func (a *archetype) init(capacityIncrement int, components ...componentType) {
a.mask = mask
a.components = comps
a.entities = storage{}
a.toAdd = map[ID]*archetype{}
a.toRemove = map[ID]*archetype{}
a.entities.init(entityType, capacityIncrement)
}

Expand Down Expand Up @@ -110,3 +114,21 @@ func (a *archetype) Len() uint32 {
func (a *archetype) Set(index uint32, id ID, comp interface{}) unsafe.Pointer {
return a.components[a.indices[id]].set(index, comp)
}

func (a *archetype) GetTransitionAdd(id ID) (*archetype, bool) {
p, ok := a.toAdd[id]
return p, ok
}

func (a *archetype) GetTransitionRemove(id ID) (*archetype, bool) {
p, ok := a.toRemove[id]
return p, ok
}

func (a *archetype) SetTransitionAdd(id ID, to *archetype) {
a.toAdd[id] = to
}

func (a *archetype) SetTransitionRemove(id ID, to *archetype) {
a.toRemove[id] = to
}
36 changes: 32 additions & 4 deletions ecs/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (w *World) Exchange(entity Entity, add []ID, rem []ID) {
}
}

arch := w.findOrCreateArchetype(mask)
arch := w.findOrCreateArchetype(oldArch, addIDs, rem)

allComps := make([]componentPointer, 0, len(keepIDs)+len(addIDs))
for _, id := range keepIDs {
Expand Down Expand Up @@ -231,11 +231,39 @@ func (w *World) copyTo(entity Entity, id ID, comp interface{}) unsafe.Pointer {
return arch.Set(index.index, id, comp)
}

func (w *World) findOrCreateArchetype(mask bitMask) *archetype {
func (w *World) findOrCreateArchetype(start *archetype, add []ID, rem []ID) *archetype {
curr := start
mask := start.mask
for _, id := range rem {
mask.Set(id, false)
if next, ok := curr.GetTransitionRemove(id); ok {
curr = next
} else {
next, _ := w.findOrCreateArchetypeSlow(mask)
next.SetTransitionAdd(id, curr)
curr.SetTransitionRemove(id, next)
curr = next
}
}
for _, id := range add {
mask.Set(id, true)
if next, ok := curr.GetTransitionAdd(id); ok {
curr = next
} else {
next, _ := w.findOrCreateArchetypeSlow(mask)
next.SetTransitionRemove(id, curr)
curr.SetTransitionAdd(id, next)
curr = next
}
}
return curr
}

func (w *World) findOrCreateArchetypeSlow(mask bitMask) (*archetype, bool) {
if arch, ok := w.findArchetype(mask); ok {
return arch
return arch, false
}
return w.createArchetype(mask)
return w.createArchetype(mask), true
}

func (w *World) findArchetype(mask bitMask) (*archetype, bool) {
Expand Down
22 changes: 22 additions & 0 deletions ecs/world_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,28 @@ func TestRegisterComponents(t *testing.T) {
assert.Equal(t, ID(1), ComponentID[rotation](&world))
}

func TestArchetypeGraph(t *testing.T) {
world := NewWorld()

posID := ComponentID[position](&world)
velID := ComponentID[velocity](&world)
rotID := ComponentID[rotation](&world)

archEmpty := world.archetypes.Get(0)
arch0 := world.findOrCreateArchetype(archEmpty, []ID{posID}, []ID{})
archEmpty2 := world.findOrCreateArchetype(arch0, []ID{}, []ID{posID})

assert.Equal(t, archEmpty, archEmpty2)

arch01 := world.findOrCreateArchetype(arch0, []ID{velID}, []ID{})
arch012 := world.findOrCreateArchetype(arch01, []ID{rotID}, []ID{})

assert.Equal(t, []ID{0, 1, 2}, arch012.ids)

archEmpty3 := world.findOrCreateArchetype(arch012, []ID{}, []ID{posID, rotID, velID})
assert.Equal(t, archEmpty, archEmpty3)
}

func TestTypeSizes(t *testing.T) {
printTypeSize[World]()
printTypeSizeName[pagedArr32[archetype]]("PagedArr32")
Expand Down