next try to enhance performance: now using uint9 again and no pointers

This commit is contained in:
2024-07-15 14:21:21 +02:00
parent 89d25db9e7
commit e8ed283233
2 changed files with 49 additions and 34 deletions

View File

@@ -12,24 +12,24 @@ import (
"github.com/tlinden/golsky/rle" "github.com/tlinden/golsky/rle"
) )
type Cell struct { // func (cell *Cell) Count() uint8 {
State uint8 // 1==life, 0==dead // var count uint8
Neighbors [8]*Cell // all neighbors, max 8
NeighborCount int // number of neighbors, might be less than 8 on edges
}
func (cell *Cell) Count() uint8 { // for idx := 0; idx < cell.NeighborCount; idx++ {
var count uint8 // count += cell.Neighbors[idx].State
// }
for idx := 0; idx < cell.NeighborCount; idx++ { // return count
count += cell.Neighbors[idx].State // }
}
return count type Neighbor struct {
X, Y int
} }
type Grid struct { type Grid struct {
Data [][]*Cell Data [][]uint8
NeighborCount [][]int
Neighbors [][][]Neighbor
Empty bool Empty bool
Config *Config Config *Config
} }
@@ -37,20 +37,25 @@ type Grid struct {
// Create new empty grid and allocate Data according to provided dimensions // Create new empty grid and allocate Data according to provided dimensions
func NewGrid(config *Config) *Grid { func NewGrid(config *Config) *Grid {
grid := &Grid{ grid := &Grid{
Data: make([][]*Cell, config.Height), Data: make([][]uint8, config.Height),
NeighborCount: make([][]int, config.Height),
Neighbors: make([][][]Neighbor, config.Height),
Empty: config.Empty, Empty: config.Empty,
Config: config, Config: config,
} }
// first setup the cells // first setup the cells
for y := 0; y < config.Height; y++ { for y := 0; y < config.Height; y++ {
grid.Data[y] = make([]*Cell, config.Width) grid.Data[y] = make([]uint8, config.Width)
grid.Neighbors[y] = make([][]Neighbor, config.Width)
grid.NeighborCount[y] = make([]int, config.Width)
for x := 0; x < config.Width; x++ { for x := 0; x < config.Width; x++ {
grid.Data[y][x] = &Cell{} grid.Data[y][x] = 0
} }
} }
// in a second pass, collect pointers to the neighbors of each cell // in a second pass, collect positions to the neighbors of each cell
for y := 0; y < config.Height; y++ { for y := 0; y < config.Height; y++ {
for x := 0; x < config.Width; x++ { for x := 0; x < config.Width; x++ {
grid.SetupNeighbors(x, y) grid.SetupNeighbors(x, y)
@@ -63,6 +68,8 @@ func NewGrid(config *Config) *Grid {
func (grid *Grid) SetupNeighbors(x, y int) { func (grid *Grid) SetupNeighbors(x, y int) {
idx := 0 idx := 0
var neighbors []Neighbor
for nbgY := -1; nbgY < 2; nbgY++ { for nbgY := -1; nbgY < 2; nbgY++ {
for nbgX := -1; nbgX < 2; nbgX++ { for nbgX := -1; nbgX < 2; nbgX++ {
var col, row int var col, row int
@@ -89,16 +96,24 @@ func (grid *Grid) SetupNeighbors(x, y int) {
continue continue
} }
grid.Data[y][x].Neighbors[idx] = grid.Data[row][col] neighbors = append(neighbors, Neighbor{X: col, Y: row})
grid.Data[y][x].NeighborCount++ grid.NeighborCount[y][x]++
idx++ idx++
} }
} }
grid.Neighbors[y][x] = neighbors
} }
// count the living neighbors of a cell // count the living neighbors of a cell
func (grid *Grid) CountNeighbors(x, y int) uint8 { func (grid *Grid) CountNeighbors(x, y int) uint8 {
return grid.Data[y][x].Count() var count uint8
for idx := 0; idx < grid.NeighborCount[y][x]; idx++ {
count += grid.Data[grid.Neighbors[y][x][idx].Y][grid.Neighbors[y][x][idx].X]
}
return count
} }
// Create a new 1:1 instance // Create a new 1:1 instance
@@ -124,7 +139,7 @@ func (grid *Grid) Copy(other *Grid) {
func (grid *Grid) Clear() { func (grid *Grid) Clear() {
for y := range grid.Data { for y := range grid.Data {
for x := range grid.Data[y] { for x := range grid.Data[y] {
grid.Data[y][x].State = 0 grid.Data[y][x] = 0
} }
} }
} }
@@ -135,7 +150,7 @@ func (grid *Grid) FillRandom() {
for y := range grid.Data { for y := range grid.Data {
for x := range grid.Data[y] { for x := range grid.Data[y] {
if rand.Intn(grid.Config.Density) == 1 { if rand.Intn(grid.Config.Density) == 1 {
grid.Data[y][x].State = 1 grid.Data[y][x] = 1
} }
} }
} }
@@ -145,7 +160,7 @@ func (grid *Grid) FillRandom() {
func (grid *Grid) Dump() { func (grid *Grid) Dump() {
for y := 0; y < grid.Config.Height; y++ { for y := 0; y < grid.Config.Height; y++ {
for x := 0; x < grid.Config.Width; x++ { for x := 0; x < grid.Config.Width; x++ {
if grid.Data[y][x].State == 1 { if grid.Data[y][x] == 1 {
fmt.Print("XX") fmt.Print("XX")
} else { } else {
fmt.Print(" ") fmt.Print(" ")
@@ -168,7 +183,7 @@ func (grid *Grid) LoadRLE(pattern *rle.RLE) {
x = colIndex + startX x = colIndex + startX
y = rowIndex + startY y = rowIndex + startY
grid.Data[y][x].State = 1 grid.Data[y][x] = 1
} }
} }
} }
@@ -279,7 +294,7 @@ func (grid *Grid) SaveState(filename, rule string) error {
for y := range grid.Data { for y := range grid.Data {
for _, cell := range grid.Data[y] { for _, cell := range grid.Data[y] {
row := "." row := "."
if cell.State == 1 { if cell == 1 {
row = "o" row = "o"
} }

View File

@@ -163,14 +163,14 @@ func (scene *ScenePlay) UpdateCells() {
defer wg.Done() defer wg.Done()
for x := 0; x < scene.Config.Width; x++ { for x := 0; x < scene.Config.Width; x++ {
state := scene.Grids[scene.Index].Data[y][x].State // 0|1 == dead or alive state := scene.Grids[scene.Index].Data[y][x] // 0|1 == dead or alive
neighbors := scene.Grids[scene.Index].CountNeighbors(x, y) neighbors := scene.Grids[scene.Index].CountNeighbors(x, y)
// actually apply the current rules // actually apply the current rules
nextstate := scene.RuleCheckFunc(state, neighbors) nextstate := scene.RuleCheckFunc(state, neighbors)
// change state of current cell in next grid // change state of current cell in next grid
scene.Grids[next].Data[y][x].State = nextstate scene.Grids[next].Data[y][x] = nextstate
if scene.Config.ShowEvolution { if scene.Config.ShowEvolution {
// set history to current generation so we can infer the // set history to current generation so we can infer the
@@ -418,7 +418,7 @@ func (scene *ScenePlay) SaveRectRLE() {
grid[y] = make([]uint8, width) grid[y] = make([]uint8, width)
for x := 0; x < width; x++ { for x := 0; x < width; x++ {
grid[y][x] = scene.Grids[scene.Index].Data[y+starty][x+startx].State grid[y][x] = scene.Grids[scene.Index].Data[y+starty][x+startx]
} }
} }
@@ -471,7 +471,7 @@ func (scene *ScenePlay) ToggleCellOnCursorPos() {
y := int(worldY) / scene.Config.Cellsize y := int(worldY) / scene.Config.Cellsize
if x > -1 && y > -1 && x < scene.Config.Width && y < scene.Config.Height { if x > -1 && y > -1 && x < scene.Config.Width && y < scene.Config.Height {
scene.Grids[scene.Index].Data[y][x].State ^= 1 scene.Grids[scene.Index].Data[y][x] ^= 1
scene.History.Age[y][x] = 1 scene.History.Age[y][x] = 1
} }
} }
@@ -497,7 +497,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
if scene.Config.ShowEvolution { if scene.Config.ShowEvolution {
scene.DrawEvolution(screen, x, y, op) scene.DrawEvolution(screen, x, y, op)
} else { } else {
if scene.Grids[scene.Index].Data[y][x].State == 1 { if scene.Grids[scene.Index].Data[y][x] == 1 {
scene.World.DrawImage(scene.Theme.Tile(ColLife), op) scene.World.DrawImage(scene.Theme.Tile(ColLife), op)
} }
} }
@@ -514,7 +514,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
func (scene *ScenePlay) DrawEvolution(screen *ebiten.Image, x, y int, op *ebiten.DrawImageOptions) { func (scene *ScenePlay) DrawEvolution(screen *ebiten.Image, x, y int, op *ebiten.DrawImageOptions) {
age := scene.Generations - scene.History.Age[y][x] age := scene.Generations - scene.History.Age[y][x]
switch scene.Grids[scene.Index].Data[y][x].State { switch scene.Grids[scene.Index].Data[y][x] {
case Alive: case Alive:
if age > 50 && scene.Config.ShowEvolution { if age > 50 && scene.Config.ShowEvolution {
scene.World.DrawImage(scene.Theme.Tile(ColOld), op) scene.World.DrawImage(scene.Theme.Tile(ColOld), op)