ByteArena / ecs

Go implementation of the Entity/Component/System paradigm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Performance and memory allocation issues

Leopotam opened this issue · comments

Nice framework, but I have some issues with performance and total memory consuming:

package bench1

import (
	"testing"

	"github.com/bytearena/ecs"
)

const IT uint32 = 10_000_000

type float3 [3]float32

type movable struct {
	Position float3
}

type directable struct {
	Direction float3
}

var manager *ecs.Manager
var aMovable *ecs.Component
var aDirectable *ecs.Component
var aFilter ecs.Tag

func init() {
	manager = ecs.NewManager()
	aMovable = manager.NewComponent()
	aDirectable = manager.NewComponent()
	aFilter = ecs.BuildTag(aMovable, aDirectable)

	for i := 0; i < int(IT); i++ {
		e := manager.NewEntity()
		e.AddComponent(aMovable, &movable{
			Position: float3{1, 2, 3},
		})
		e.AddComponent(aDirectable, &directable{
			Direction: float3{1, 2, 3},
		})
	}
	run()
}

func run() {
	for _, result := range manager.Query(aFilter) {
		m := result.Components[aMovable].(*movable)
		m.Position[0] += 0.01
		d := result.Components[aDirectable].(*directable)
		d.Direction[0] += 0.01
	}
}

// BenchmarkArena ...
func BenchmarkArena(bench *testing.B) {
	for i := 0; i < bench.N; i++ {
		run()
	}
}
go test -gcflags "-B" -ldflags "-w -s" -bench=. -benchmem -memprofile memprofile.out ./

BenchmarkArena-16 1 5600180817 ns/op 3143503096 B/op 30000050 allocs/op

go tool pprof memprofile.out

flat flat% sum% cum cum%
5.85GB 60.27% 60.27% 5.85GB 60.27% github.com/bytearena/ecs.(*Manager).Query
1.55GB 15.98% 76.24% 1.55GB 15.98% github.com/bytearena/ecs.(*Entity).AddComponent
1.13GB 11.61% 87.85% 1.13GB 11.61% github.com/bytearena/ecs.(*Manager).NewEntity

Its too bad for high performance things like games. Any suggestions how to fix that?