Skip to content

Commit

Permalink
perf(metarepos): avoid copy overhead by removing unnecessary converti…
Browse files Browse the repository at this point in the history
…ng from byte slice to string

This change removes copy overhead caused by unnecessary type casting from byte slice to string.
Refer to [this blog](https://www.sobyte.net/post/2022-09/string-byte-convertion/#performance-testing)
for performance.
  • Loading branch information
ijsong committed Jul 17, 2023
1 parent 9913149 commit cf4c0f4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions internal/metarepos/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
type raftNode struct {
raftConfig

proposeC chan string // proposed messages from app
proposeC chan []byte // proposed messages from app
confChangeC chan raftpb.ConfChange // proposed cluster config changes
commitC chan *raftCommittedEntry // entries committed to app
snapshotC chan struct{} // snapshot trigger
Expand Down Expand Up @@ -98,7 +98,7 @@ var purgeInterval = 30 * time.Second
// current), then new log entries.
func newRaftNode(cfg raftConfig,
snapshotGetter SnapshotGetter,
proposeC chan string,
proposeC chan []byte,
confChangeC chan raftpb.ConfChange,
tmStub *telemetryStub,
logger *zap.Logger) *raftNode {
Expand Down Expand Up @@ -707,7 +707,7 @@ Loop:

// blocks until accepted by raft state machine
// TODO:: handle dropped proposal
err := rc.node.Propose(context.TODO(), []byte(prop))
err := rc.node.Propose(context.TODO(), prop)
if err != nil {
rc.logger.Warn("proposal fail", zap.String("err", err.Error()))
}
Expand Down
6 changes: 3 additions & 3 deletions internal/metarepos/raft_metadata_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type RaftMetadataRepository struct {
proposeC chan *mrpb.RaftEntry
commitC chan *committedEntry
rnConfChangeC chan raftpb.ConfChange
rnProposeC chan string
rnProposeC chan []byte
rnCommitC chan *raftCommittedEntry

// for report
Expand Down Expand Up @@ -110,7 +110,7 @@ func NewRaftMetadataRepository(opts ...Option) *RaftMetadataRepository {
proposeC: make(chan *mrpb.RaftEntry, 4096),
commitC: make(chan *committedEntry, 4096),
rnConfChangeC: make(chan raftpb.ConfChange, 1),
rnProposeC: make(chan string),
rnProposeC: make(chan []byte),
reportQueue: make([]*mrpb.Report, 0, 1024),
runner: runner.New("mr", cfg.logger),
sw: stopwaiter.New(),
Expand Down Expand Up @@ -308,7 +308,7 @@ Loop:
}

select {
case mr.rnProposeC <- string(b):
case mr.rnProposeC <- b:
case <-ctx.Done():
mr.sendAck(e.NodeIndex, e.RequestIndex, ctx.Err())
}
Expand Down
12 changes: 6 additions & 6 deletions internal/metarepos/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type cluster struct {
peers []string
peerToIdx map[uint64]int
commitC []<-chan *raftCommittedEntry
proposeC []chan string
proposeC []chan []byte
confChangeC []chan raftpb.ConfChange
running []bool
stop []stopFunc
Expand All @@ -49,7 +49,7 @@ func newCluster(n int) *cluster {
peers: peers,
peerToIdx: make(map[uint64]int),
commitC: make([]<-chan *raftCommittedEntry, len(peers)),
proposeC: make([]chan string, len(peers)),
proposeC: make([]chan []byte, len(peers)),
confChangeC: make([]chan raftpb.ConfChange, len(peers)),
running: make([]bool, len(peers)),
stop: make([]stopFunc, len(peers)),
Expand All @@ -66,7 +66,7 @@ func newCluster(n int) *cluster {

os.RemoveAll(fmt.Sprintf("raftdata/wal/%d", nodeID)) //nolint:errcheck,revive // TODO:: Handle an error returned.
os.RemoveAll(fmt.Sprintf("raftdata/snap/%d", nodeID)) //nolint:errcheck,revive // TODO:: Handle an error returned.
clus.proposeC[i] = make(chan string, 1)
clus.proposeC[i] = make(chan []byte, 1)
clus.confChangeC[i] = make(chan raftpb.ConfChange, 1)
//logger, _ := zap.NewDevelopment()
logger := zap.NewNop()
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestProposeOnFollower(t *testing.T) {
donec := make(chan struct{})
for i := range clus.peers {
// feedback for "n" committed entries, then update donec
go func(pC chan<- string, cC <-chan *raftCommittedEntry) {
go func(pC chan<- []byte, cC <-chan *raftCommittedEntry) {
Loop:
for range cC {
break Loop
Expand All @@ -165,7 +165,7 @@ func TestProposeOnFollower(t *testing.T) {
}(clus.proposeC[i], clus.commitC[i])

// one message feedback per node
go func(i int) { clus.proposeC[i] <- fmt.Sprintf("%d", i) }(i)
go func(i int) { clus.proposeC[i] <- []byte(fmt.Sprintf("%d", i)) }(i)
}

for range clus.peers {
Expand All @@ -187,7 +187,7 @@ func TestFailoverLeaderElection(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
cancels[i] = cancel
// feedback for "n" committed entries, then update donec
go func(ctx context.Context, idx int, pC chan<- string, cC <-chan *raftCommittedEntry) {
go func(ctx context.Context, _ int, _ chan<- []byte, cC <-chan *raftCommittedEntry) {
defer wg.Done()

Loop:
Expand Down

0 comments on commit cf4c0f4

Please sign in to comment.