Skip to content

Commit

Permalink
Reduce sprintf overhead in code showing up during sync profiles (#7182)
Browse files Browse the repository at this point in the history
* Reduce sprintf overhead in code showing up during sync profiles

* Raise upperbound to not worry about it
  • Loading branch information
ValarDragon authored Dec 24, 2023
1 parent 00bfc3d commit cb9cb4e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
12 changes: 11 additions & 1 deletion x/poolmanager/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"fmt"
"sort"
"strconv"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -43,7 +44,16 @@ var (

// ModuleRouteToBytes serializes moduleRoute to bytes.
func FormatModuleRouteKey(poolId uint64) []byte {
return []byte(fmt.Sprintf("%s%d", SwapModuleRouterPrefix, poolId))
// Estimate the length of the string representation of poolId
// 11 is a very safe upper bound, (99,999,999,999) pools, and is a 12 byte allocation
length := 11
result := make([]byte, 1, 1+length)
result[0] = SwapModuleRouterPrefix[0]
// Write poolId into the byte slice starting after the prefix
written := strconv.AppendUint(result[1:], poolId, 10)

// Slice result to the actual length used
return result[:1+len(written)]
}

// FormatDenomTradePairKey serializes denom trade pair to bytes.
Expand Down
21 changes: 21 additions & 0 deletions x/poolmanager/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,24 @@ func TestParseDenomTradePairKey(t *testing.T) {
t.Errorf("Expected error, got nil")
}
}

func TestFormatModuleRouteKey(t *testing.T) {
cases := []struct {
id uint64
expectedSansPrefix string
}{0: {id: 0, expectedSansPrefix: "0"},
1: {id: 1, expectedSansPrefix: "1"},
2: {id: 12, expectedSansPrefix: "12"},
3: {id: 122, expectedSansPrefix: "122"},
4: {id: 4522, expectedSansPrefix: "4522"},
5: {id: 54522, expectedSansPrefix: "54522"},
6: {id: 654522, expectedSansPrefix: "654522"},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("id=%d", tc.id), func(t *testing.T) {
key := types.FormatModuleRouteKey(tc.id)
require.Equal(t, types.SwapModuleRouterPrefix[0], key[0])
require.Equal(t, tc.expectedSansPrefix, string(key[1:]))
})
}
}
9 changes: 7 additions & 2 deletions x/twap/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"bytes"
"errors"
"fmt"
time "time"
Expand Down Expand Up @@ -58,13 +59,17 @@ func FormatMostRecentTWAPKey(poolId uint64, denom1, denom2 string) []byte {

// TODO: Replace historical management with ORM, we currently accept 2x write amplification right now.
func FormatHistoricalTimeIndexTWAPKey(accumulatorWriteTime time.Time, poolId uint64, denom1, denom2 string) []byte {
var buffer bytes.Buffer
timeS := osmoutils.FormatTimeString(accumulatorWriteTime)
return []byte(fmt.Sprintf("%s%s%s%d%s%s%s%s", HistoricalTWAPTimeIndexPrefix, timeS, KeySeparator, poolId, KeySeparator, denom1, KeySeparator, denom2))
fmt.Fprintf(&buffer, "%s%s%s%d%s%s%s%s", HistoricalTWAPTimeIndexPrefix, timeS, KeySeparator, poolId, KeySeparator, denom1, KeySeparator, denom2)
return buffer.Bytes()
}

func FormatHistoricalPoolIndexTWAPKey(poolId uint64, denom1, denom2 string, accumulatorWriteTime time.Time) []byte {
var buffer bytes.Buffer
timeS := osmoutils.FormatTimeString(accumulatorWriteTime)
return []byte(fmt.Sprintf("%s%d%s%s%s%s%s%s", HistoricalTWAPPoolIndexPrefix, poolId, KeySeparator, denom1, KeySeparator, denom2, KeySeparator, timeS))
fmt.Fprintf(&buffer, "%s%d%s%s%s%s%s%s", HistoricalTWAPPoolIndexPrefix, poolId, KeySeparator, denom1, KeySeparator, denom2, KeySeparator, timeS)
return buffer.Bytes()
}

func FormatHistoricalPoolIndexTimePrefix(poolId uint64, denom1, denom2 string) []byte {
Expand Down

0 comments on commit cb9cb4e

Please sign in to comment.