Skip to content

Commit 2c610be

Browse files
committed
perf(block): use sync/atomic instead of mutexes
1 parent 55914d9 commit 2c610be

3 files changed

Lines changed: 37 additions & 40 deletions

File tree

block/internal/executing/executor.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"sync"
9+
"sync/atomic"
910
"time"
1011

1112
"github.com/ipfs/go-datastore"
@@ -50,8 +51,7 @@ type Executor struct {
5051
options common.BlockOptions
5152

5253
// State management
53-
lastState types.State
54-
lastStateMtx *sync.RWMutex
54+
lastState *atomic.Pointer[types.State]
5555

5656
// Channels for coordination
5757
txNotifyCh chan struct{}
@@ -112,7 +112,7 @@ func NewExecutor(
112112
headerBroadcaster: headerBroadcaster,
113113
dataBroadcaster: dataBroadcaster,
114114
options: options,
115-
lastStateMtx: &sync.RWMutex{},
115+
lastState: &atomic.Pointer[types.State]{},
116116
txNotifyCh: make(chan struct{}, 1),
117117
errorCh: errorCh,
118118
logger: logger.With().Str("component", "executor").Logger(),
@@ -152,16 +152,17 @@ func (e *Executor) Stop() error {
152152

153153
// GetLastState returns the current state
154154
func (e *Executor) GetLastState() types.State {
155-
e.lastStateMtx.RLock()
156-
defer e.lastStateMtx.RUnlock()
157-
return e.lastState
155+
state := e.lastState.Load()
156+
if state == nil {
157+
// Return zero value if not initialized
158+
return types.State{}
159+
}
160+
return *state
158161
}
159162

160163
// SetLastState updates the current state
161164
func (e *Executor) SetLastState(state types.State) {
162-
e.lastStateMtx.Lock()
163-
defer e.lastStateMtx.Unlock()
164-
e.lastState = state
165+
e.lastState.Store(&state)
165166
}
166167

167168
// NotifyNewTransactions signals that new transactions are available

block/internal/submitting/submitter.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/binary"
77
"fmt"
88
"sync"
9+
"sync/atomic"
910
"time"
1011

1112
"github.com/rs/zerolog"
@@ -45,8 +46,7 @@ type Submitter struct {
4546
signer signer.Signer
4647

4748
// DA state
48-
daIncludedHeight uint64
49-
daStateMtx *sync.RWMutex
49+
daIncludedHeight *atomic.Uint64
5050

5151
// Submission state to prevent concurrent submissions
5252
headerSubmissionMtx sync.Mutex
@@ -78,17 +78,17 @@ func NewSubmitter(
7878
errorCh chan<- error,
7979
) *Submitter {
8080
return &Submitter{
81-
store: store,
82-
exec: exec,
83-
cache: cache,
84-
metrics: metrics,
85-
config: config,
86-
genesis: genesis,
87-
daSubmitter: daSubmitter,
88-
signer: signer,
89-
daStateMtx: &sync.RWMutex{},
90-
errorCh: errorCh,
91-
logger: logger.With().Str("component", "submitter").Logger(),
81+
store: store,
82+
exec: exec,
83+
cache: cache,
84+
metrics: metrics,
85+
config: config,
86+
genesis: genesis,
87+
daSubmitter: daSubmitter,
88+
signer: signer,
89+
daIncludedHeight: &atomic.Uint64{},
90+
errorCh: errorCh,
91+
logger: logger.With().Str("component", "submitter").Logger(),
9292
}
9393
}
9494

@@ -269,16 +269,12 @@ func (s *Submitter) setFinalWithRetry(nextHeight uint64) error {
269269

270270
// GetDAIncludedHeight returns the DA included height
271271
func (s *Submitter) GetDAIncludedHeight() uint64 {
272-
s.daStateMtx.RLock()
273-
defer s.daStateMtx.RUnlock()
274-
return s.daIncludedHeight
272+
return s.daIncludedHeight.Load()
275273
}
276274

277275
// SetDAIncludedHeight updates the DA included height
278276
func (s *Submitter) SetDAIncludedHeight(height uint64) {
279-
s.daStateMtx.Lock()
280-
defer s.daStateMtx.Unlock()
281-
s.daIncludedHeight = height
277+
s.daIncludedHeight.Store(height)
282278
}
283279

284280
// initializeDAIncludedHeight loads the DA included height from store

block/internal/submitting/submitter_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"encoding/binary"
66
"errors"
77
"fmt"
8-
"sync"
8+
"sync/atomic"
99
"testing"
1010
"time"
1111

@@ -210,7 +210,7 @@ func TestSubmitter_initializeDAIncludedHeight(t *testing.T) {
210210
binary.LittleEndian.PutUint64(bz, 7)
211211
require.NoError(t, st.SetMetadata(ctx, store.DAIncludedHeightKey, bz))
212212

213-
s := &Submitter{store: st, daStateMtx: &sync.RWMutex{}, logger: zerolog.Nop()}
213+
s := &Submitter{store: st, daIncludedHeight: &atomic.Uint64{}, logger: zerolog.Nop()}
214214
require.NoError(t, s.initializeDAIncludedHeight(ctx))
215215
assert.Equal(t, uint64(7), s.GetDAIncludedHeight())
216216
}
@@ -324,16 +324,16 @@ func TestSubmitter_daSubmissionLoop(t *testing.T) {
324324

325325
// Provide a minimal signer implementation
326326
s := &Submitter{
327-
store: st,
328-
exec: exec,
329-
cache: cm,
330-
metrics: metrics,
331-
config: cfg,
332-
genesis: genesis.Genesis{},
333-
daSubmitter: fakeDA,
334-
signer: &fakeSigner{},
335-
daStateMtx: &sync.RWMutex{},
336-
logger: zerolog.Nop(),
327+
store: st,
328+
exec: exec,
329+
cache: cm,
330+
metrics: metrics,
331+
config: cfg,
332+
genesis: genesis.Genesis{},
333+
daSubmitter: fakeDA,
334+
signer: &fakeSigner{},
335+
daIncludedHeight: &atomic.Uint64{},
336+
logger: zerolog.Nop(),
337337
}
338338

339339
// Make there be pending headers and data by setting store height > last submitted

0 commit comments

Comments
 (0)