Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/game: remove heap allocation on each time the neighbors are sent message #25

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/game/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ type ParallelUniverse struct {
BottomNeighbor *NeighborConnection
LeftNeighbor *NeighborConnection
RightNeighbor *NeighborConnection

send NeighborData
}

func NewParallelUniverse(height, width uint32) *ParallelUniverse {
p := &ParallelUniverse{
Universe: NewUniverse(height, width),
send: NeighborData{
Generation: 0,
Cells: make([]uint8, height*width),
},
}
p.Rules = p.rules
return p
Expand Down Expand Up @@ -158,38 +164,36 @@ func (p *ParallelUniverse) WaitForNeighborsData() {

func (p *ParallelUniverse) SendDataToNeighbors() {
var wg sync.WaitGroup
data := NeighborData{
Generation: p.Generation,
Cells: make([]uint8, len(p.cells)),
}
copy(data.Cells, p.cells)

p.send.Generation = p.Generation
copy(p.send.Cells, p.cells)

if p.TopNeighbor != nil {
wg.Add(1)
go func() {
defer wg.Done()
p.TopNeighbor.SendCh <- data
p.TopNeighbor.SendCh <- p.send
}()
}
if p.BottomNeighbor != nil {
wg.Add(1)
go func() {
defer wg.Done()
p.BottomNeighbor.SendCh <- data
p.BottomNeighbor.SendCh <- p.send
}()
}
if p.LeftNeighbor != nil {
wg.Add(1)
go func() {
defer wg.Done()
p.LeftNeighbor.SendCh <- data
p.LeftNeighbor.SendCh <- p.send
}()
}
if p.RightNeighbor != nil {
wg.Add(1)
go func() {
defer wg.Done()
p.RightNeighbor.SendCh <- data
p.RightNeighbor.SendCh <- p.send
}()
}

Expand Down