Skip to content

Commit

Permalink
optimisation: Reduce sprintf usage (#21)
Browse files Browse the repository at this point in the history
* wip

* remove unwanted files
  • Loading branch information
andrewwormald authored Aug 27, 2024
1 parent f8782e6 commit e871546
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 40 deletions.
3 changes: 1 addition & 2 deletions adapters/kafkastreamer/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kafkastreamer

import (
"context"
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -58,7 +57,7 @@ func (p *Producer) Send(ctx context.Context, recordID int64, statusType int, hea
})
}

key := fmt.Sprintf("%v", recordID)
key := strconv.FormatInt(int64(recordID), 10)
msg := kafka.Message{
Key: []byte(key),
Value: []byte(strconv.FormatInt(int64(statusType), 10)),
Expand Down
16 changes: 10 additions & 6 deletions adapters/memrecordstore/memrecordstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func (s *Store) Store(ctx context.Context, record *workflow.Record, maker workfl
CreatedAt: s.clock.Now(),
})

snapshotKey := fmt.Sprintf("%v-%v-%v", record.WorkflowName, record.ForeignID, record.RunID)
s.snapshots[snapshotKey] = append(s.snapshots[snapshotKey], record)
skey := snapShotKey(record.WorkflowName, record.ForeignID, record.RunID)
s.snapshots[skey] = append(s.snapshots[skey], record)

return nil
}
Expand Down Expand Up @@ -250,26 +250,30 @@ func (s *Store) Snapshots(workflowName, foreignID, runID string) []*workflow.Rec
s.mu.Lock()
defer s.mu.Unlock()

key := fmt.Sprintf("%v-%v-%v", workflowName, foreignID, runID)
key := snapShotKey(workflowName, foreignID, runID)
return s.snapshots[key]
}

func (s *Store) SetSnapshotOffset(workflowName, foreignID, runID string, offset int) {
s.mu.Lock()
defer s.mu.Unlock()

key := fmt.Sprintf("%v-%v-%v", workflowName, foreignID, runID)
key := snapShotKey(workflowName, foreignID, runID)
s.snapshotsOffsets[key] = offset
}

func (s *Store) SnapshotOffset(workflowName, foreignID, runID string) int {
s.mu.Lock()
defer s.mu.Unlock()

key := fmt.Sprintf("%v-%v-%v", workflowName, foreignID, runID)
key := snapShotKey(workflowName, foreignID, runID)
return s.snapshotsOffsets[key]
}

func snapShotKey(workflowName, foreignID, runID string) string {
return workflowName + "-" + foreignID + "-" + runID
}

func uniqueKey(s1, s2 string) string {
return fmt.Sprintf("%v-%v", s1, s2)
return s1 + "-" + s2
}
3 changes: 1 addition & 2 deletions adapters/reflexstreamer/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package reflexstreamer

import (
"fmt"
"strconv"

"github.com/luno/reflex"
Expand All @@ -21,7 +20,7 @@ func DefaultReflexTranslator(e *reflex.Event) (*workflow.ConnectorEvent, error)
return &workflow.ConnectorEvent{
ID: e.ID,
ForeignID: e.ForeignID,
Type: fmt.Sprintf("%v", e.Type.ReflexType()),
Type: strconv.FormatInt(int64(e.Type.ReflexType()), 10),
Headers: map[string]string{
HeaderMeta: string(e.MetaData),
},
Expand Down
4 changes: 2 additions & 2 deletions await.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package workflow

import (
"context"
"fmt"
"strconv"
"time"

"github.com/luno/jettison/errors"
Expand All @@ -19,7 +19,7 @@ func (w *Workflow[Type, Status]) Await(ctx context.Context, foreignID, runID str
pollFrequency = opt.pollFrequency
}

role := makeRole("await", w.Name, fmt.Sprintf("%v", int(status)), foreignID)
role := makeRole("await", w.Name, strconv.FormatInt(int64(status), 10), foreignID)
return awaitWorkflowStatusByForeignID[Type, Status](ctx, w, status, foreignID, runID, role, pollFrequency)
}

Expand Down
3 changes: 1 addition & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package workflow

import (
"context"
"fmt"
"time"

"k8s.io/utils/clock"
Expand Down Expand Up @@ -42,7 +41,7 @@ type Builder[Type any, Status StatusType] struct {

func (b *Builder[Type, Status]) AddStep(from Status, c ConsumerFunc[Type, Status], allowedDestinations ...Status) *stepUpdater[Type, Status] {
if _, exists := b.workflow.consumers[from]; exists {
panic(fmt.Sprintf("'AddStep(%v,' already exists. Only one Step can be configured to consume the status", from.String()))
panic("'AddStep(" + from.String() + ",' already exists. Only one Step can be configured to consume the status")
}

for _, to := range allowedDestinations {
Expand Down
6 changes: 3 additions & 3 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package workflow

import (
"context"
"fmt"
"strconv"
"time"

"github.com/luno/workflow/internal/metrics"
Expand Down Expand Up @@ -37,9 +37,9 @@ func connectorConsumer[Type any, Status StatusType](w *Workflow[Type, Status], c
"to",
w.Name,
"consumer",
fmt.Sprintf("%v", shard),
strconv.FormatInt(int64(shard), 10),
"of",
fmt.Sprintf("%v", totalShards),
strconv.FormatInt(int64(totalShards), 10),
)

errBackOff := w.defaultOpts.errBackOff
Expand Down
14 changes: 7 additions & 7 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package workflow

import (
"context"
"fmt"
"strconv"
"time"

"github.com/luno/jettison/errors"
Expand Down Expand Up @@ -32,21 +32,21 @@ type consumerConfig[Type any, Status StatusType] struct {
func consumer[Type any, Status StatusType](w *Workflow[Type, Status], currentStatus Status, p consumerConfig[Type, Status], shard, totalShards int) {
role := makeRole(
w.Name,
fmt.Sprintf("%v", int(currentStatus)),
strconv.FormatInt(int64(currentStatus), 10),
"consumer",
fmt.Sprintf("%v", shard),
strconv.FormatInt(int64(shard), 10),
"of",
fmt.Sprintf("%v", totalShards),
strconv.FormatInt(int64(totalShards), 10),
)

// processName can change in value if the string value of the status enum is changed. It should not be used for
// storing in the record store, event streamer, timeoutstore, or offset store.
processName := makeRole(
fmt.Sprintf("%v", currentStatus.String()),
currentStatus.String(),
"consumer",
fmt.Sprintf("%v", shard),
strconv.FormatInt(int64(shard), 10),
"of",
fmt.Sprintf("%v", totalShards),
strconv.FormatInt(int64(totalShards), 10),
)

topic := Topic(w.Name, int(currentStatus))
Expand Down
10 changes: 5 additions & 5 deletions outbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package workflow

import (
"context"
"fmt"
"strconv"
"time"

"github.com/luno/jettison/errors"
Expand All @@ -18,19 +18,19 @@ func outboxConsumer[Type any, Status StatusType](w *Workflow[Type, Status], conf
w.Name,
"outbox",
"consumer",
fmt.Sprintf("%v", shard),
strconv.FormatInt(int64(shard), 10),
"of",
fmt.Sprintf("%v", totalShards),
strconv.FormatInt(int64(totalShards), 10),
)

// processName can change in value if the string value of the status enum is changed. It should not be used for
// storing in the record store, event streamer, timeoutstore, or offset store.
processName := makeRole(
"outbox",
"consumer",
fmt.Sprintf("%v", shard),
strconv.FormatInt(int64(shard), 10),
"of",
fmt.Sprintf("%v", totalShards),
strconv.FormatInt(int64(totalShards), 10),
)

errBackOff := w.outboxConfig.errBackOff
Expand Down
6 changes: 3 additions & 3 deletions runstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package workflow

import (
"context"
"fmt"
"strconv"

"github.com/luno/jettison/errors"
"github.com/luno/jettison/j"
Expand Down Expand Up @@ -41,7 +41,7 @@ func (rs RunState) String() string {
case RunStateRequestedDataDeleted:
return "RequestedDataDeleted"
default:
return fmt.Sprintf("RunState(%d)", rs)
return "RunState(" + strconv.FormatInt(int64(rs), 10) + ")"
}
}

Expand Down Expand Up @@ -142,7 +142,7 @@ func validateRunStateTransition(record *Record, runState RunState, sentinelErr e
}

if !valid[runState] {
msg := fmt.Sprintf("current run state cannot transition to %v", runState.String())
msg := "Current run state cannot transition to " + runState.String()
return errors.Wrap(sentinelErr, msg, j.MKV{
"record_id": record.ID,
"workflow_name": record.WorkflowName,
Expand Down
3 changes: 2 additions & 1 deletion schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package workflow
import (
"context"
"fmt"
"strconv"
"time"

"github.com/luno/jettison/errors"
Expand All @@ -29,7 +30,7 @@ func (w *Workflow[Type, Status]) Schedule(foreignID string, startingStatus Statu
return err
}

role := makeRole(w.Name, fmt.Sprintf("%v", int(startingStatus)), foreignID, "scheduler", spec)
role := makeRole(w.Name, strconv.FormatInt(int64(startingStatus), 10), foreignID, "scheduler", spec)
processName := makeRole(startingStatus.String(), foreignID, "scheduler", spec)

w.run(role, processName, func(ctx context.Context) error {
Expand Down
4 changes: 2 additions & 2 deletions state.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package workflow

import (
"fmt"
"strconv"

"github.com/luno/workflow/internal/metrics"
)
Expand All @@ -28,7 +28,7 @@ func (s State) String() string {
return val
}

return fmt.Sprintf("State(%d)", s)
return "State(" + strconv.FormatInt(int64(s), 10) + ")"
}

func (w *Workflow[Type, Status]) updateState(processName string, s State) {
Expand Down
6 changes: 3 additions & 3 deletions timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package workflow

import (
"context"
"fmt"
"strconv"
"time"

"github.com/luno/jettison/errors"
Expand Down Expand Up @@ -187,7 +187,7 @@ type timeout[Type any, Status StatusType] struct {
}

func timeoutPoller[Type any, Status StatusType](w *Workflow[Type, Status], status Status, timeouts timeouts[Type, Status]) {
role := makeRole(w.Name, fmt.Sprintf("%v", int(status)), "timeout-consumer")
role := makeRole(w.Name, strconv.FormatInt(int64(status), 10), "timeout-consumer")
// readableRole can change in value if the string value of the status enum is changed. It should not be used for
// storing in the record store, event streamer, timeout store, or offset store.
processName := makeRole(status.String(), "timeout-consumer")
Expand Down Expand Up @@ -222,7 +222,7 @@ func timeoutAutoInserterConsumer[Type any, Status StatusType](
status Status,
timeouts timeouts[Type, Status],
) {
role := makeRole(w.Name, fmt.Sprintf("%v", int(status)), "timeout-auto-inserter-consumer")
role := makeRole(w.Name, strconv.FormatInt(int64(status), 10), "timeout-auto-inserter-consumer")
processName := makeRole(status.String(), "timeout-auto-inserter-consumer")

pauseAfterErrCount := w.defaultOpts.pauseAfterErrCount
Expand Down
4 changes: 2 additions & 2 deletions topic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package workflow

import (
"fmt"
"strconv"
"strings"
)

Expand All @@ -14,7 +14,7 @@ func Topic(workflowName string, statusType int) string {
name := strings.ReplaceAll(workflowName, " ", emptySpaceReplacement)
return strings.Join([]string{
name,
fmt.Sprintf("%v", statusType),
strconv.FormatInt(int64(statusType), 10),
}, topicSeparator)
}

Expand Down

0 comments on commit e871546

Please sign in to comment.