From 4074debdbf592259ed46bfa6b0b084266b1f4edb Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Tue, 16 Jul 2024 19:07:51 +0200 Subject: [PATCH] using only one dimensional grid by calculating y+STRIDE*x --- src/grid.go | 93 +++++++++++++++++---------------- src/play.go | 19 ++++--- various-tests/perf-1dim/main.go | 11 ++++ 3 files changed, 69 insertions(+), 54 deletions(-) diff --git a/src/grid.go b/src/grid.go index a69bb74..1000bf8 100644 --- a/src/grid.go +++ b/src/grid.go @@ -12,46 +12,42 @@ import ( "github.com/tlinden/golsky/rle" ) -// func (cell *Cell) Count() uint8 { -// var count uint8 - -// for idx := 0; idx < cell.NeighborCount; idx++ { -// count += cell.Neighbors[idx].State -// } - -// return count -// } +// equals grid height, is being used to access grid elements and must be global +var STRIDE int type Neighbor struct { X, Y int } type Grid struct { - Data [][]uint8 - NeighborCount [][]int - Neighbors [][][]Neighbor + Data []uint8 + NeighborCount []int + Neighbors [][]Neighbor Empty bool Config *Config } // Create new empty grid and allocate Data according to provided dimensions func NewGrid(config *Config) *Grid { + STRIDE = config.Height + if config.Width > config.Height { + STRIDE = config.Width + } + + size := STRIDE * STRIDE + grid := &Grid{ - Data: make([][]uint8, config.Height), - NeighborCount: make([][]int, config.Height), - Neighbors: make([][][]Neighbor, config.Height), + Data: make([]uint8, size), + NeighborCount: make([]int, size), + Neighbors: make([][]Neighbor, size), Empty: config.Empty, Config: config, } // first setup the cells for y := 0; y < config.Height; y++ { - 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++ { - grid.Data[y][x] = 0 + grid.Data[y+STRIDE*x] = 0 } } @@ -97,20 +93,25 @@ func (grid *Grid) SetupNeighbors(x, y int) { } neighbors = append(neighbors, Neighbor{X: col, Y: row}) - grid.NeighborCount[y][x]++ + grid.NeighborCount[y+STRIDE*x]++ idx++ } } - grid.Neighbors[y][x] = neighbors + grid.Neighbors[y+STRIDE*x] = neighbors } // count the living neighbors of a cell func (grid *Grid) CountNeighbors(x, y int) uint8 { 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] + pos := y + STRIDE*x + neighbors := grid.Neighbors[pos] + neighborCount := grid.NeighborCount[pos] + + for idx := 0; idx < neighborCount; idx++ { + neighbor := neighbors[idx] + count += grid.Data[neighbor.Y+STRIDE*neighbor.X] } return count @@ -127,30 +128,30 @@ func (grid *Grid) Clone() *Grid { } // copy data -func (grid *Grid) Copy(other *Grid) { - for y := range grid.Data { - for x := range grid.Data[y] { - other.Data[y][x] = grid.Data[y][x] - } - } -} +// func (grid *Grid) Copy(other *Grid) { +// for y := range grid.Data { +// for x := range grid.Data[y] { +// other.Data[y+STRIDE*x] = grid.Data[y+STRIDE*x] +// } +// } +// } // delete all contents -func (grid *Grid) Clear() { - for y := range grid.Data { - for x := range grid.Data[y] { - grid.Data[y][x] = 0 - } - } -} +// func (grid *Grid) Clear() { +// for y := range grid.Data { +// for x := range grid.Data[y] { +// grid.Data[y+STRIDE*x] = 0 +// } +// } +// } // initialize with random life cells using the given density func (grid *Grid) FillRandom() { if !grid.Empty { - for y := range grid.Data { - for x := range grid.Data[y] { + for y := 0; y < grid.Config.Height; y++ { + for x := 0; x < grid.Config.Width; x++ { if rand.Intn(grid.Config.Density) == 1 { - grid.Data[y][x] = 1 + grid.Data[y+STRIDE*x] = 1 } } } @@ -160,7 +161,7 @@ func (grid *Grid) FillRandom() { func (grid *Grid) Dump() { for y := 0; y < grid.Config.Height; y++ { for x := 0; x < grid.Config.Width; x++ { - if grid.Data[y][x] == 1 { + if grid.Data[y+STRIDE*x] == 1 { fmt.Print("XX") } else { fmt.Print(" ") @@ -183,7 +184,7 @@ func (grid *Grid) LoadRLE(pattern *rle.RLE) { x = colIndex + startX y = rowIndex + startY - grid.Data[y][x] = 1 + grid.Data[y+STRIDE*x] = 1 } } } @@ -291,10 +292,10 @@ func (grid *Grid) SaveState(filename, rule string) error { fmt.Fprintf(file, "#Life 1.05\n#R %s\n#D golsky state file\n#P -1 -1\n", rule) - for y := range grid.Data { - for _, cell := range grid.Data[y] { + for y := 0; y < grid.Config.Height; y++ { + for x := 0; x < grid.Config.Width; x++ { row := "." - if cell == 1 { + if grid.Data[y+STRIDE*x] == 1 { row = "o" } diff --git a/src/play.go b/src/play.go index 39ee913..2a8f3a1 100644 --- a/src/play.go +++ b/src/play.go @@ -156,21 +156,24 @@ func (scene *ScenePlay) UpdateCells() { var wg sync.WaitGroup wg.Add(scene.Config.Height) + width := scene.Config.Width + height := scene.Config.Height + // compute life status of cells - for y := 0; y < scene.Config.Height; y++ { + for y := 0; y < height; y++ { go func() { defer wg.Done() - for x := 0; x < scene.Config.Width; x++ { - state := scene.Grids[scene.Index].Data[y][x] // 0|1 == dead or alive + for x := 0; x < width; x++ { + state := scene.Grids[scene.Index].Data[y+STRIDE*x] // 0|1 == dead or alive neighbors := scene.Grids[scene.Index].CountNeighbors(x, y) // actually apply the current rules nextstate := scene.RuleCheckFunc(state, neighbors) // change state of current cell in next grid - scene.Grids[next].Data[y][x] = nextstate + scene.Grids[next].Data[y+STRIDE*x] = nextstate if scene.Config.ShowEvolution { // set history to current generation so we can infer the @@ -418,7 +421,7 @@ func (scene *ScenePlay) SaveRectRLE() { grid[y] = make([]uint8, width) for x := 0; x < width; x++ { - grid[y][x] = scene.Grids[scene.Index].Data[y+starty][x+startx] + grid[y][x] = scene.Grids[scene.Index].Data[(y+starty)+STRIDE*(x+startx)] } } @@ -471,7 +474,7 @@ func (scene *ScenePlay) ToggleCellOnCursorPos() { y := int(worldY) / scene.Config.Cellsize if x > -1 && y > -1 && x < scene.Config.Width && y < scene.Config.Height { - scene.Grids[scene.Index].Data[y][x] ^= 1 + scene.Grids[scene.Index].Data[y+STRIDE*x] ^= 1 scene.History.Age[y][x] = 1 } } @@ -497,7 +500,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) { if scene.Config.ShowEvolution { scene.DrawEvolution(screen, x, y, op) } else { - if scene.Grids[scene.Index].Data[y][x] == 1 { + if scene.Grids[scene.Index].Data[y+STRIDE*x] == 1 { scene.World.DrawImage(scene.Theme.Tile(ColLife), op) } } @@ -514,7 +517,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) { func (scene *ScenePlay) DrawEvolution(screen *ebiten.Image, x, y int, op *ebiten.DrawImageOptions) { age := scene.Generations - scene.History.Age[y][x] - switch scene.Grids[scene.Index].Data[y][x] { + switch scene.Grids[scene.Index].Data[y+STRIDE*x] { case Alive: if age > 50 && scene.Config.ShowEvolution { scene.World.DrawImage(scene.Theme.Tile(ColOld), op) diff --git a/various-tests/perf-1dim/main.go b/various-tests/perf-1dim/main.go index a8a89b1..a9f8b74 100644 --- a/various-tests/perf-1dim/main.go +++ b/various-tests/perf-1dim/main.go @@ -87,6 +87,17 @@ func Loop(grid []bool) { } func main() { + grid := make([]int, 50*50) + + for y := 0; y < 50; y++ { + for x := 0; x < 50; x++ { + grid[y+50*x] = 1 + fmt.Printf("%d,%d => %d\n", x, y, x+50*y) + } + } +} + +func xmain() { // enable cpu profiling. Do NOT use q to stop the game but // close the window to get a profile fd, err := os.Create("cpu.profile")