Skip to content

Commit

Permalink
fix architype capacity issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yohamta committed Nov 29, 2023
1 parent 5f480b5 commit 4ac0aa8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func NewStorage() *Storage {

// PushComponent stores the new data of the component in the archetype.
func (cs *Storage) PushComponent(component component.IComponentType, archetypeIndex ArchetypeIndex) {
if len(cs.storages) <= int(archetypeIndex) {
cs.ensureCapacity(archetypeIndex)
}
if v := cs.storages[archetypeIndex]; v == nil {
cs.storages[archetypeIndex] = []unsafe.Pointer{}
}
Expand All @@ -44,6 +47,10 @@ func (cs *Storage) SetComponent(archetypeIndex ArchetypeIndex, componentIndex Co

// MoveComponent moves the pointer to data of the component in the archetype.
func (cs *Storage) MoveComponent(source ArchetypeIndex, index ComponentIndex, dst ArchetypeIndex) {
if len(cs.storages) <= int(dst) {
cs.ensureCapacity(dst)
}

src_slice := cs.storages[source]
dst_slice := cs.storages[dst]

Expand Down Expand Up @@ -74,3 +81,9 @@ func (cs *Storage) Contains(archetypeIndex ArchetypeIndex, componentIndex Compon
}
return cs.storages[archetypeIndex][componentIndex] != nil
}

func (cs *Storage) ensureCapacity(archetypeIndex ArchetypeIndex) {
newStorages := make([][]unsafe.Pointer, len(cs.storages)*2)
copy(newStorages, cs.storages)
cs.storages = newStorages
}
19 changes: 19 additions & 0 deletions world_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,25 @@ func TestDeleteEntity(t *testing.T) {
}
}

func TestArchetypeStorageExpands(t *testing.T) {
world := donburi.NewWorld()
dummy := donburi.NewTag()
entry := world.Entry(world.Create(dummy))
const N = 256
for i := 0; i < N; i++ {
tag := donburi.NewTag()
entry.AddComponent(tag)
}
q := donburi.NewQuery(filter.Contains(dummy))
e, ok := q.First(world)
if !ok {
t.Fatalf("Entity should be found")
}
if len(e.Archetype().Layout().Components()) != N+1 {
t.Fatalf("Archetype should have 301 components")
}
}

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

Expand Down

0 comments on commit 4ac0aa8

Please sign in to comment.