Skip to content

Commit

Permalink
run gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
unitoftime committed Oct 14, 2024
1 parent 03fbb2d commit 7da6e1a
Show file tree
Hide file tree
Showing 20 changed files with 1,468 additions and 1,178 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build
name: build
on:
push:
branches: [ master ]
Expand All @@ -18,7 +18,7 @@ jobs:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@v3

- name: Make All
- name: make All
run: make all

# Maybe one day
Expand Down
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
all: build test benchmark
all: fmt build test benchmark

build:
fmt:
go fmt ./...

build: fmt
go build -v ./...

test:
test: fmt
# go test -v -race -coverprofile=coverage.out -covermode=atomic ./...
go test -v -coverprofile=coverage.out -covermode=count ./...

Expand Down
28 changes: 14 additions & 14 deletions arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

// This is the identifier for entities in the world
//
//cod:struct
type Id uint32

Expand All @@ -28,10 +29,10 @@ func (s *componentSlice[T]) Write(index int, val T) {

// TODO: Rename, this is kind of like an archetype header
type lookupList struct {
index *internalMap[Id,int] // A mapping from entity ids to array indices
id []Id // An array of every id in the arch list (essentially a reverse mapping from index to Id)
holes []int // List of indexes that have ben deleted
mask archetypeMask
index *internalMap[Id, int] // A mapping from entity ids to array indices
id []Id // An array of every id in the arch list (essentially a reverse mapping from index to Id)
holes []int // List of indexes that have ben deleted
mask archetypeMask
}

func (l *lookupList) Len() int {
Expand All @@ -42,7 +43,7 @@ func (l *lookupList) Len() int {
// Returns the index
func (l *lookupList) addToEasiestHole(id Id) int {
if len(l.holes) > 0 {
lastHoleIndex := len(l.holes)-1
lastHoleIndex := len(l.holes) - 1
index := l.holes[lastHoleIndex]
l.id[index] = id
l.index.Put(id, index)
Expand All @@ -58,7 +59,6 @@ func (l *lookupList) addToEasiestHole(id Id) int {
}
}


type storage interface {
ReadToEntity(*Entity, archetypeId, int) bool
ReadToRawEntity(*RawEntity, archetypeId, int) bool
Expand Down Expand Up @@ -109,23 +109,23 @@ func (s *componentSliceStorage[T]) print(amount int) {

// Provides generic storage for all archetypes
type archEngine struct {
generation int
generation int
// archCounter archetypeId

lookup []*lookupList // Indexed by archetypeId
compSliceStorage []storage // Indexed by componentId
dcr *componentRegistry
lookup []*lookupList // Indexed by archetypeId
compSliceStorage []storage // Indexed by componentId
dcr *componentRegistry

// TODO - using this makes things not thread safe inside the engine
archCount map[archetypeId]int
}

func newArchEngine() *archEngine {
return &archEngine{
generation: 1, // Start at 1 so that anyone with the default int value will always realize they are in the wrong generation
generation: 1, // Start at 1 so that anyone with the default int value will always realize they are in the wrong generation

lookup: make([]*lookupList, 0, DefaultAllocation),
compSliceStorage: make([]storage, maxComponentId + 1),
compSliceStorage: make([]storage, maxComponentId+1),
dcr: newComponentRegistry(),
archCount: make(map[archetypeId]int),
}
Expand All @@ -137,10 +137,10 @@ func (e *archEngine) newArchetypeId(archMask archetypeMask) archetypeId {
archId := archetypeId(len(e.lookup))
e.lookup = append(e.lookup,
&lookupList{
index: newMap[Id,int](0),
index: newMap[Id, int](0),
id: make([]Id, 0, DefaultAllocation),
holes: make([]int, 0, DefaultAllocation),
mask: archMask,
mask: archMask,
},
)

Expand Down
68 changes: 45 additions & 23 deletions bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ package main
import (
"fmt"
"log"
"time"
"math/rand"
"strconv"
"time"

"runtime"
"runtime/pprof"
"flag"
"os"
"runtime"
"runtime/pprof"

"github.com/unitoftime/ecs"
)
Expand Down Expand Up @@ -76,12 +76,15 @@ func main() {

program := os.Args[1]
size, err := strconv.Atoi(os.Args[2])
if err != nil { panic(err) }
if err != nil {
panic(err)
}
colLimitArg, err := strconv.Atoi(os.Args[3])
if err != nil { panic(err) }
if err != nil {
panic(err)
}
collisionLimit := int32(colLimitArg)


// ballast := make([]byte, 10<<30)

fmt.Println("Iter", "Time")
Expand All @@ -108,7 +111,7 @@ func main() {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
Expand All @@ -135,9 +138,13 @@ func createWorld(size int) *ecs.World {

func moveCircles(query *ecs.View2[Position, Velocity], fixedTime float64, maxPosition float64) {
query.MapSlices(func(ids []ecs.Id, pos []Position, vel []Velocity) {
if len(ids) != len(pos) || len(ids) != len(vel) { panic("ERR") }
if len(ids) != len(pos) || len(ids) != len(vel) {
panic("ERR")
}
for i := range ids {
if ids[i] == ecs.InvalidEntity { continue }
if ids[i] == ecs.InvalidEntity {
continue
}
pos[i].X += vel[i].X * fixedTime
pos[i].Y += vel[i].Y * fixedTime

Expand All @@ -159,17 +166,27 @@ func checkCollisions(world *ecs.World,

query.MapSlices(func(aId []ecs.Id, aPos []Position, aCol []Collider, aCnt []Count) {
innerQuery.MapSlices(func(bId []ecs.Id, bPos []Position, bCol []Collider) {
if len(aId) != len(aPos) || len(aId) != len(aCol) { panic("ERR") }
if len(bId) != len(bPos) || len(bId) != len(bCol) { panic("ERR") }
if len(aId) != len(aPos) || len(aId) != len(aCol) {
panic("ERR")
}
if len(bId) != len(bPos) || len(bId) != len(bCol) {
panic("ERR")
}
for i := range aId {
if aId[i] == ecs.InvalidEntity { continue }
if aId[i] == ecs.InvalidEntity {
continue
}
aPos_i := &aPos[i]
aCol_i := &aCol[i]
for j := range bId {
if bId[i] == ecs.InvalidEntity { continue }
if bId[i] == ecs.InvalidEntity {
continue
}
bPos_j := &bPos[j]
bCol_j := &bCol[j]
if aId[i] == bId[j] { continue } // Skip if entity is the same
if aId[i] == bId[j] {
continue
} // Skip if entity is the same

dx := aPos_i.X - bPos_j.X
dy := aPos_i.Y - bPos_j.Y
Expand Down Expand Up @@ -197,7 +214,6 @@ func checkCollisions(world *ecs.World,
})
}


func benchPhysicsOptimized(size int, collisionLimit int32) {
world := createWorld(size)

Expand Down Expand Up @@ -258,7 +274,7 @@ func benchPhysicsOptimized(size int, collisionLimit int32) {
// })
}

/*
/*
974 1031
975 625
976 787
Expand Down Expand Up @@ -384,7 +400,7 @@ func benchPhysicsAlt(size int, collisionLimit int32) {

// Update positions
posVelQuery.MapId(func(id ecs.Id, position *Position, velocity *Velocity) {
// ecs.Map2(world, func(id ecs.Id, position *Position, velocity *Velocity) {
// ecs.Map2(world, func(id ecs.Id, position *Position, velocity *Velocity) {
position.X += velocity.X * fixedTime
position.Y += velocity.Y * fixedTime

Expand All @@ -401,7 +417,9 @@ func benchPhysicsAlt(size int, collisionLimit int32) {
deathCount := 0
posColCntQuery.MapId(func(aId ecs.Id, aPos *Position, aCol *Collider, aCnt *Count) {
posColQuery.MapId(func(bId ecs.Id, bPos *Position, bCol *Collider) {
if aId == bId { return } // Skip if entity is the same
if aId == bId {
return
} // Skip if entity is the same

dx := aPos.X - bPos.X
dy := aPos.Y - bPos.Y
Expand Down Expand Up @@ -665,7 +683,7 @@ func benchNativeComponents(size int, collisionLimit int32) {
cnt := make([]Count, size, size)

for i := 0; i < size; i++ {
ids[i] = ecs.Id(i+2)
ids[i] = ecs.Id(i + 2)
pos[i] = Position{maxPosition * rand.Float64(), maxPosition * rand.Float64()}
vel[i] = Velocity{maxSpeed * rand.Float64(), maxSpeed * rand.Float64()}
col[i] = Collider{
Expand Down Expand Up @@ -706,7 +724,9 @@ func benchNativeComponents(size int, collisionLimit int32) {
bPos := &pos[j]
bCol := &col[j]

if aId == bId { continue } // Skip if entity is the same
if aId == bId {
continue
} // Skip if entity is the same

dx := aPos.X - bPos.X
dy := aPos.Y - bPos.Y
Expand Down Expand Up @@ -760,7 +780,6 @@ func benchNativeComponents(size int, collisionLimit int32) {
// ColRadius [float64] [float64] ...
// ColCount [int32] [int32] ...


// Test with this new memory layout
// [uint64]
// PosX [float64]
Expand All @@ -779,7 +798,7 @@ func benchNativeSplit(size int, collisionLimit int32) {
cnt := make([]int32, size, size)

for i := 0; i < size; i++ {
ids[i] = ecs.Id(i+2)
ids[i] = ecs.Id(i + 2)
posX[i] = maxPosition * rand.Float64()
posY[i] = maxPosition * rand.Float64()
velX[i] = maxSpeed * rand.Float64()
Expand Down Expand Up @@ -818,7 +837,9 @@ func benchNativeSplit(size int, collisionLimit int32) {
bPosY := &posY[j]
bCol := &col[j]

if aId == bId { continue } // Skip if entity is the same
if aId == bId {
continue
} // Skip if entity is the same

dx := *aPosX - *bPosX
dy := *aPosY - *bPosY
Expand All @@ -845,6 +866,7 @@ func benchNativeSplit(size int, collisionLimit int32) {
// fmt.Println(ids[i], cnt[i])
// }
}

// [ id, id , id ]
// [ pos, pos, pos ]
// [ vel, vel, ]
Expand Down
30 changes: 14 additions & 16 deletions bundle.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ecs

type Bundle[T any] struct {
compId componentId
compId componentId
storage *componentSliceStorage[T]
// world *ecs.World //Needed?
}
Expand All @@ -11,14 +11,14 @@ func NewBundle[T any](world *World) Bundle[T] {
var t T
compId := name(t)
return Bundle[T]{
compId: compId,
compId: compId,
storage: getStorageByCompId[T](world.engine, compId),
}
}

func (c Bundle[T]) New(comp T) Box[T] {
return Box[T]{
Comp: comp,
Comp: comp,
compId: c.compId,
// storage: c.storage,
}
Expand All @@ -34,11 +34,11 @@ func (b Bundle[T]) id() componentId {

type Bundle4[A, B, C, D any] struct {
compId componentId
boxA *Box[A]
boxB *Box[B]
boxC *Box[C]
boxD *Box[D]
comps []Component
boxA *Box[A]
boxB *Box[B]
boxC *Box[C]
boxD *Box[D]
comps []Component
}

// Createst the boxed component type
Expand All @@ -56,16 +56,15 @@ func NewBundle4[A, B, C, D any]() Bundle4[A, B, C, D] {
}

return Bundle4[A, B, C, D]{
boxA: boxA,
boxB: boxB,
boxC: boxC,
boxD: boxD,
boxA: boxA,
boxB: boxB,
boxC: boxC,
boxD: boxD,
comps: comps,
}
}


func (bun Bundle4[A,B,C,D]) Write(world *World, id Id, a A, b B, c C, d D) {
func (bun Bundle4[A, B, C, D]) Write(world *World, id Id, a A, b B, c C, d D) {
// bun.boxA.Comp = a
// bun.boxB.Comp = b
// bun.boxC.Comp = c
Expand All @@ -86,7 +85,6 @@ func (bun Bundle4[A,B,C,D]) Write(world *World, id Id, a A, b B, c C, d D) {
bun.boxD.Comp = d

Write(world, id,
bun.comps...
bun.comps...,
)
}

Loading

0 comments on commit 7da6e1a

Please sign in to comment.