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

Conditionally allocate WaitGroup memory #2901

Merged
merged 22 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions x/merkledb/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func (v *view) hashChangedNode(n *node) ids.ID {
lastKeyByte byte

// We use [wg] to wait until all descendants of [n] have been updated.
wg sync.WaitGroup
wg waitGroup
)

if bytesForKey > 0 {
Expand Down Expand Up @@ -360,11 +360,16 @@ func (v *view) hashChangedNode(n *node) ids.ID {
// Try updating the child and its descendants in a goroutine.
if ok := v.db.hashNodesSema.TryAcquire(1); ok {
wg.Add(1)
go func(childEntry *child) {

// Passing variables explicitly through the function call rather
// than implicitly passing them through the scope of the function
// definition allows the passed variables to be allocated on the
// stack.
go func(wg *sync.WaitGroup, childEntry *child) {
childEntry.id = v.hashChangedNode(childNodeChange.after)
v.db.hashNodesSema.Release(1)
wg.Done()
}(childEntry)
}(wg.wg, childEntry)
} else {
// We're at the goroutine limit; do the work in this goroutine.
childEntry.id = v.hashChangedNode(childNodeChange.after)
Expand Down
25 changes: 25 additions & 0 deletions x/merkledb/wait_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package merkledb

import "sync"

// waitGroup is a small wrapper of a sync.WaitGroup that avoids performing a
// memory allocation when Add is never called.
type waitGroup struct {
wg *sync.WaitGroup
}

func (wg *waitGroup) Add(delta int) {
if wg.wg == nil {
wg.wg = new(sync.WaitGroup)
}
wg.wg.Add(delta)
}

func (wg *waitGroup) Wait() {
if wg.wg != nil {
wg.wg.Wait()
}
}
29 changes: 29 additions & 0 deletions x/merkledb/wait_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package merkledb

import "testing"

func Benchmark_WaitGroup_Wait(b *testing.B) {
for i := 0; i < b.N; i++ {
var wg waitGroup
wg.Wait()
}
}

func Benchmark_WaitGroup_Add(b *testing.B) {
for i := 0; i < b.N; i++ {
var wg waitGroup
wg.Add(1)
}
}

func Benchmark_WaitGroup_AddDoneWait(b *testing.B) {
for i := 0; i < b.N; i++ {
var wg waitGroup
wg.Add(1)
wg.wg.Done()
wg.Wait()
}
}
Loading