Skip to content

Commit

Permalink
add memory per entity to archetype stats (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 authored Mar 17, 2023
1 parent 35a795c commit a827861
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Nothing

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

### Features

* Adds memory per entity to archetype stats (!124)

### Other

* Adds benchmarks of Arche vs. Array of Structs (AoS) and Array of Pointers (AoP), for different memory per entity and number of entities (!123)
Expand Down
18 changes: 10 additions & 8 deletions ecs/archetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,20 @@ func (a *archetype) Stats(reg *componentRegistry) stats.ArchetypeStats {
}

cap := int(a.Cap())
memory := cap * int(entitySize)
memPerEntity := 0
for i := 0; i < len(a.components); i++ {
comp := &a.components[i]
memory += int(comp.itemSize) * cap
memPerEntity += int(comp.itemSize)
}
memory := cap * (int(entitySize) + memPerEntity)

return stats.ArchetypeStats{
Size: int(a.Len()),
Capacity: cap,
Components: aCompCount,
ComponentIDs: ids,
ComponentTypes: aTypes,
Memory: memory,
Size: int(a.Len()),
Capacity: cap,
Components: aCompCount,
ComponentIDs: ids,
ComponentTypes: aTypes,
Memory: memory,
MemoryPerEntity: memPerEntity,
}
}
6 changes: 4 additions & 2 deletions ecs/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type ArchetypeStats struct {
ComponentTypes []reflect.Type
// Total reserved memory for entities and components, in bytes
Memory int
// Memory for components per entity
MemoryPerEntity int
}

func (s *WorldStats) String() string {
Expand Down Expand Up @@ -83,7 +85,7 @@ func (s *ArchetypeStats) String() string {
typeNames[i] = tp.Name()
}
return fmt.Sprintf(
"Archetype -- Components: %2d, Entities: %6d, Capacity: %6d, Memory: %7.1f kB\n Components: %s\n",
s.Components, s.Size, s.Capacity, float64(s.Memory)/1024.0, strings.Join(typeNames, ", "),
"Archetype -- Components: %2d, Entities: %6d, Capacity: %6d, Memory: %7.1f kB, Per entity: %4d B\n Components: %s\n",
s.Components, s.Size, s.Capacity, float64(s.Memory)/1024.0, s.MemoryPerEntity, strings.Join(typeNames, ", "),
)
}

0 comments on commit a827861

Please sign in to comment.