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

Replace weighted semaphore with channel #2900

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 2 additions & 3 deletions x/merkledb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/attribute"
"golang.org/x/exp/maps"
"golang.org/x/sync/semaphore"

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -218,7 +217,7 @@ type merkleDB struct {

// calculateNodeIDsSema controls the number of goroutines inside
// [calculateNodeIDsHelper] at any given time.
calculateNodeIDsSema *semaphore.Weighted
calculateNodeIDsSema semaphore

tokenSize int
}
Expand Down Expand Up @@ -274,7 +273,7 @@ func newDatabase(
debugTracer: getTracerIfEnabled(config.TraceLevel, DebugTrace, config.Tracer),
infoTracer: getTracerIfEnabled(config.TraceLevel, InfoTrace, config.Tracer),
childViews: make([]*view, 0, defaultPreallocationSize),
calculateNodeIDsSema: semaphore.NewWeighted(int64(rootGenConcurrency)),
calculateNodeIDsSema: make(semaphore, rootGenConcurrency),
tokenSize: BranchFactorToTokenSize[config.BranchFactor],
}

Expand Down
27 changes: 27 additions & 0 deletions x/merkledb/semaphore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package merkledb

type semaphore chan struct{}

func (s semaphore) Acquire() {
s <- struct{}{}
}

func (s semaphore) TryAcquire() bool {
select {
case s <- struct{}{}:
return true
default:
return false
}
}

func (s semaphore) Release() {
select {
case <-s:
default:
panic("release of unacquired semaphore")
}
}
46 changes: 46 additions & 0 deletions x/merkledb/semaphore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package merkledb

import "testing"

func Benchmark_Semaphore_Acquire(b *testing.B) {
s := make(semaphore, b.N)

b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Acquire()
}
}

func Benchmark_Semaphore_Release(b *testing.B) {
s := make(semaphore, b.N)
for i := 0; i < b.N; i++ {
s.Acquire()
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Release()
}
}

func Benchmark_Semaphore_TryAcquire_Success(b *testing.B) {
s := make(semaphore, b.N)

b.ResetTimer()
for i := 0; i < b.N; i++ {
s.TryAcquire()
}
}

func Benchmark_Semaphore_TryAcquire_Failure(b *testing.B) {
s := make(semaphore, 1)
s.Acquire()

b.ResetTimer()
for i := 0; i < b.N; i++ {
s.TryAcquire()
}
}
8 changes: 4 additions & 4 deletions x/merkledb/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ func (v *view) calculateNodeIDs(ctx context.Context) error {
}

if !v.root.IsNothing() {
_ = v.db.calculateNodeIDsSema.Acquire(context.Background(), 1)
v.db.calculateNodeIDsSema.Acquire()
v.changes.rootID = v.calculateNodeIDsHelper(v.root.Value())
v.db.calculateNodeIDsSema.Release(1)
v.db.calculateNodeIDsSema.Release()
} else {
v.changes.rootID = ids.Empty
}
Expand Down Expand Up @@ -282,11 +282,11 @@ func (v *view) calculateNodeIDsHelper(n *node) ids.ID {
childEntry.hasValue = childNodeChange.after.hasValue()

// Try updating the child and its descendants in a goroutine.
if ok := v.db.calculateNodeIDsSema.TryAcquire(1); ok {
if v.db.calculateNodeIDsSema.TryAcquire() {
wg.Add(1)
go func() {
childEntry.id = v.calculateNodeIDsHelper(childNodeChange.after)
v.db.calculateNodeIDsSema.Release(1)
v.db.calculateNodeIDsSema.Release()
wg.Done()
}()
} else {
Expand Down
Loading