-
Notifications
You must be signed in to change notification settings - Fork 670
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conditionally allocate WaitGroup memory (#2901)
- Loading branch information
1 parent
e7b14e4
commit 5be62de
Showing
3 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |