Skip to content

Commit

Permalink
fix benchmarks, add kelindar/tile to the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
quasilyte committed Oct 12, 2023
1 parent b7bf376 commit 0370212
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,11 @@ Time - **ns/op**:

| Library | no_wall | simple_wall | multi_wall |
|---|---|---|---|
| quasilyte/pathing BFS | 3525 | 2084 | 2688 |
| quasilyte/pathing A* | 20140 | 3415 | 13310 |
| quasilyte/pathing BFS | 3525 | 6353 | 16927 |
| quasilyte/pathing A* | 20140 | 35846 | 44756 |
| fzipp/astar | 948367 | 1554290 | 1842812 |
| beefsack/go-astar | 453939 | 939300 | 1032581 |
| kelindar/tile | 107632 ns | 169613 ns | 182342 ns |
| s0rg/grid | 1816039 | 1154117 | 1189989 |
| SolarLune/paths | 6588751 | 5158604 | 6114856 |

Expand All @@ -192,6 +193,7 @@ Allocations - **allocs/op**:
| quasilyte/pathing A* | 0 | 0 | 0 |
| fzipp/astar | 2008 | 3677 | 3600 |
| beefsack/go-astar | 529 | 1347 | 1557 |
| kelindar/tile | 3 | 3 | 3 |
| s0rg/grid | 2976 | 1900 | 1759 |
| SolarLune/paths | 7199 | 6368 | 7001 |

Expand All @@ -203,6 +205,7 @@ Allocations - **bytes/op**:
| quasilyte/pathing A* | 0 | 0 | 0 |
| fzipp/astar | 337336 | 511908 | 722690 |
| beefsack/go-astar | 43653 | 93122 | 130731 |
| tile | 123118 | 32950 | 65763 |
| s0rg/grid | 996889 | 551976 | 740523 |
| SolarLune/paths | 235168 | 194768 | 230416 |

Expand Down
16 changes: 16 additions & 0 deletions _bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ func BenchmarkQuasilytePathingAStar(b *testing.B) {
}
}

func BenchmarkKelindarTile(b *testing.B) {
for i := range testCaseList {
tc := testCaseList[i]
b.Run(tc.name, func(b *testing.B) {
lib := newKelindarTileTester()
lib.Init(tc)

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, finish := lib.BuildPath()
validateResult(b, tc, finish)
}
})
}
}

func BenchmarkFzippAstar(b *testing.B) {
for i := range testCaseList {
tc := testCaseList[i]
Expand Down
2 changes: 2 additions & 0 deletions _bench/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ require (
)

require (
github.com/kelindar/iostream v1.3.0 // indirect
github.com/kelindar/tile v1.3.0 // indirect
github.com/s0rg/array2d v1.1.1 // indirect
github.com/s0rg/set v1.0.1 // indirect
github.com/s0rg/vec2d v1.1.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions _bench/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ github.com/beefsack/go-astar v0.0.0-20200827232313-4ecf9e304482 h1:p4g4uok3+r6Tg
github.com/beefsack/go-astar v0.0.0-20200827232313-4ecf9e304482/go.mod h1:Cu3t5VeqE8kXjUBeNXWQprfuaP5UCIc5ggGjgMx9KFc=
github.com/fzipp/astar v0.2.0 h1:vEmeszAm62kB02Qh/oXQNrVR8p6G00WAJIkVRJYzekc=
github.com/fzipp/astar v0.2.0/go.mod h1:1zYE8kcTMuS0g+b9Vh9lfR7t0MleOH6EneyA3esiLEc=
github.com/kelindar/iostream v1.3.0 h1:Bz2qQabipZlF1XCk64bnxsGLete+iHtayGPeWVpbwbo=
github.com/kelindar/iostream v1.3.0/go.mod h1:MkjMuVb6zGdPQVdwLnFRO0xOTOdDvBWTztFmjRDQkXk=
github.com/kelindar/tile v1.3.0 h1:JiAwGeVxUmXL29YRgymoy+WiW6CsDEVxeOguTUaUiWs=
github.com/kelindar/tile v1.3.0/go.mod h1:IpsZux+e7jjsZL5fUZ42IRZ8r08NolXqIVE/LXfm8Ao=
github.com/quasilyte/pathing v0.0.0-20230920175739-f09a6a6e624b h1:FC97/sIYUluRLue9kB0epP788FN4Lc7B/QrbzlN+JQg=
github.com/quasilyte/pathing v0.0.0-20230920175739-f09a6a6e624b/go.mod h1:ixjYf4Sk6dLp/SekkpGSqvSkODKfuZOwwi0nPR0AMTw=
github.com/quasilyte/pathing v0.0.0-20230921143848-ee7e478e4bd6 h1:5YTEsnm/3GxNZpLo+np7vLnLJpnqvaebjibrswUmukA=
Expand Down
55 changes: 55 additions & 0 deletions _bench/kelindar_tile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package bench

import (
"github.com/kelindar/tile"
)

type kelindarTileTester struct {
tc *testCase

grid *tile.Grid[uint8]
}

func newKelindarTileTester() *kelindarTileTester {
return &kelindarTileTester{}
}

func (t *kelindarTileTester) Init(tc *testCase) {
t.tc = tc
t.grid = tile.NewGridOf[uint8](int16(tc.numCols), int16(tc.numRows))

for y, row := range tc.layout {
for x, col := range row {
if col == 'x' {
t.grid.WriteAt(int16(x), int16(y), tile.Value(0))
} else {
t.grid.WriteAt(int16(x), int16(y), tile.Value(1))
}
}
}
}

func (t *kelindarTileTester) BuildPath() ([]tile.Point, gridCoord) {
from := tile.Point{X: int16(t.tc.start.X), Y: int16(t.tc.start.Y)}
to := tile.Point{X: int16(t.tc.finish.X), Y: int16(t.tc.finish.Y)}
result, _, _ := t.grid.Path(from, to, func(v tile.Value) uint16 {
// The simplest mapping: the tile value is its cost.
return uint16(v)
})
last := result[0] // The points in path are reversed

// We need to do 1 more step to complete the path.
finish := gridCoord{X: int(last.X), Y: int(last.Y)}
switch {
case finish.X < t.tc.finish.X:
finish.X++
case finish.X > t.tc.finish.X:
finish.X--
case finish.Y < t.tc.finish.Y:
finish.Y++
case finish.Y > t.tc.finish.Y:
finish.Y--
}

return result, finish
}
1 change: 1 addition & 0 deletions _bench/quasilyte_pathing_astar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (t *quasilytePathingAStarTester) Init(tc *testCase) {
WorldWidth: uint(width),
WorldHeight: uint(height),
})
fillPathingGrid(t.grid, tc)
}

func (t *quasilytePathingAStarTester) BuildPath() (pathing.GridPath, gridCoord) {
Expand Down
12 changes: 12 additions & 0 deletions _bench/quasilyte_pathing_bfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (t *quasilytePathingBFSTester) Init(tc *testCase) {
WorldWidth: uint(width),
WorldHeight: uint(height),
})
fillPathingGrid(t.grid, tc)
}

func (t *quasilytePathingBFSTester) BuildPath() (pathing.GridPath, gridCoord) {
Expand All @@ -37,3 +38,14 @@ func (t *quasilytePathingBFSTester) BuildPath() (pathing.GridPath, gridCoord) {
result := t.bfs.BuildPath(t.grid, from, to, pathingLayer)
return result.Steps, gridCoord{X: result.Finish.X, Y: result.Finish.Y}
}

func fillPathingGrid(g *pathing.Grid, tc *testCase) {
for y, row := range tc.layout {
for x, col := range row {
if col != 'x' {
continue
}
g.SetCellTile(pathing.GridCoord{X: x, Y: y}, 1)
}
}
}

0 comments on commit 0370212

Please sign in to comment.